C#aps DataGrid 控件自定义、编辑、删除

 <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?')");
        }

    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值