<asp:datagrid id="datagrid" runat="server"
DataKeyField="ID" Font-Size="11px" Font-Names="Verdana"
AutoGenerateColumns="False" HorizontalAlign="Center" CellPadding="4" GridLines="None" ForeColor="#333333" OnSelectedIndexChanged="datagrid_SelectedIndexChanged" OnCancelCommand="datagrid_CancelCommand" OnDeleteCommand="datagrid_DeleteCommand" OnEditCommand="datagrid_EditCommand" OnItemDataBound="datagrid6_ItemDataBound" OnUpdateCommand="datagrid6_UpdateCommand">
<HeaderStyle Font-Size="11px" Font-Names="Verdana" Font-Bold="True" Wrap="False" HorizontalAlign="Center"
ForeColor="White" BackColor="#507CD1"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="DELETE" CommandName="Delete">
<HeaderStyle Width="2%"></HeaderStyle>
<ItemStyle Font-Size="12px" ForeColor="DarkBlue"></ItemStyle>
</asp:ButtonColumn>
<asp:EditCommandColumn UpdateText="EDIT" CancelText="CANCEL" EditText="Edit">
<HeaderStyle Width="2%"></HeaderStyle>
<ItemStyle Font-Size="12px" ForeColor="DarkBlue"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="ID" HeaderText="ID">
<HeaderStyle Wrap="False" Width="2%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="字段1" HeaderText="字段1">
<HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="字段2" HeaderText="字段2">
<HeaderStyle Width="8%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="字段3" HeaderText="字段3">
<HeaderStyle Width="8%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
</Columns>
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<ItemStyle BackColor="#EFF3FB" />
<EditItemStyle BackColor="#2461BF" />
<AlternatingItemStyle BackColor="White" />
</asp:datagrid>
//后端操作
protected void datagrid_EditCommand(object source, DataGridCommandEventArgs e)
{
datagrid.EditItemIndex = e.Item.ItemIndex;
//....
}
protected void datagrid6_CancelCommand(object source, DataGridCommandEventArgs e)
{
//string value = e.Item.Cells[1].Text.Trim();
datagrid.EditItemIndex = -1;
//....
}
protected void datagrid_DeleteCommand(object source, DataGridCommandEventArgs e)
{
int id = (int)datagrid.DataKeys[e.Item.ItemIndex];
//....
}
protected void datagrid_UpdateCommand(object source, DataGridCommandEventArgs e)
{
TextBox tb1 = new TextBox();
tb1 = (TextBox)e.Item.Cells[0].Controls[0];
//read the qty in the textbox.
int current_qty = Convert.ToInt32(tb1.Text.ToString());
//....
}
protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lk = new LinkButton();
lk = (LinkButton)e.Item.Cells[0].Controls[0];
lk.Attributes.Add("onclick", "javascript:return confirm('Do You Really want to execute this delete action?')");
}
}