简体   繁体   English

从ASP GridView和数据表中删除行

[英]Delete Row From asp GridView and From Datatable

I have asp gridview which is bind with a datatable. 我有与数据表绑定的asp gridview。 Only one template column that is checkbox is bind with source datatable column ( sel ). 仅一个复选框复选框模板与源数据表列( sel )绑定。

Here is markup example: 这是标记示例:

<asp:GridView  ID="testGrid"  
                CssClass="ObjSelection" 
                AutoGenerateColumns="false" 
                OnRowDataBound="testGrid_RowDataBound"
                runat="server">
 <Columns>
     <asp:TemplateField HeaderText="&nbsp">
        <HeaderTemplate>
           <asp:CheckBox ID="chkAll" runat="server" Enabled="true" AutoPostBack="true" />
        </HeaderTemplate>
        <ItemTemplate>
            <asp:CheckBox AutoPostBack="true" ID="chkRow" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "sel")%>' OnCheckedChanged="ChkRow_OnCheckChange" />
        </ItemTemplate>
     </asp:TemplateField>                                   

 </Columns>
</asp:GridView>

As you see, I am not using RowCommand to delete a row. 如您所见,我没有使用RowCommand删除行。 I have a toolbar in a separate div , which shows delete button. 我在单独的div有一个工具栏,其中显示删除按钮。

Can you guide how could I delete a row both from DataSource and GridView on a button click which exists in another div ? 您能指导我如何在另一个div中存在的单击按钮时both from DataSource and GridView删除一行吗?

Use below code to remove selected row from datatable which is either in ViewState or in Session and assign data source 使用以下代码从ViewState或Session中的数据表中删除所选行,并分配数据源

Aslo assign datakey to GridView Aslo将数据键分配给GridView

<asp:GridView  ID="testGrid"    DataKeyNames="PrimaryKey" 
                CssClass="ObjSelection" 
                AutoGenerateColumns="false" 
                OnRowDataBound="testGrid_RowDataBound"
                runat="server">

Code Behind: 背后的代码:

protected void btnRemove(object sender, EventArgs e)
    {
        // Check session exists 
        if (Session["Key"] != null)
        {
            // Opening / Retreiving DataTable.
            DataTable dt = (DataTable)Session["Key"];

            foreach (GridViewRow row in testGrid.Rows)
            {
                CheckBox chkRow= row.Cells[0].FindControl("chkRow") as CheckBox;

                if (chkRow!= null && chkRow.Checked)
                {                    
                    int Id = Convert.ToInt32(testGrid.DataKeys[row.RowIndex].Value); 

                    DataRow[] drs = dt.Select("PrimaryKey = '" + Id + "'"); // replace with your criteria as appropriate

                    if (drs.Length > 0)
                    {
                        dt .Rows.Remove(drs[0]);
                    }
                }
            }

            Session["Key"] = dt ;
            testGrid.DataSource = dt ;
            testGrid.DataBind();
        }
    }

you can use hidden field to put id of row by javascript and Jquery 您可以使用隐藏字段通过javascript和jQuery放置行ID

<asp:HiddenField runat="server" ID="IDField"/>

jQuery jQuery的

<script>
    $(document).ready(function(){
              $("#testGrid tr").click(function(){
              $("#IDField").val($(this).children("td.IDColumn").text());
              $(this).Index(); // will give you row index
              });
});
</script>

click delete button 点击删除按钮

protected void delete_Click(object sender,EventArg e)
{
 //delete from datasource use this.IDField.Value   

//to refresh data in grid 
this.testGrid.DataSource = dataTable;
this.testGrid.DataBind();

}
gridview_RowDataBound(Sender sender,eventArgs e)
{
CheckBox lblChkRow = (CheckBox)e.Row.FindControl("IDField");
if(lblChkRow.Checked)
{
// Delete Value here and bind with datasource

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM