1

I have asp gridview which is bind with a datatable. Only one template column that is checkbox is bind with source datatable column (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. I have a toolbar in a separate div, which shows delete button.

Can you guide how could I delete a row both from DataSource and GridView on a button click which exists in another div?

3 Answers 3

1

Use below code to remove selected row from datatable which is either in ViewState or in Session and assign data source

Aslo assign datakey to 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();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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

}

Comments

0

you can use hidden field to put id of row by javascript and Jquery

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

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();

}

1 Comment

How would get the index of the row that is clicked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.