hello,
i am having a datagrid n a checkbox colmn in it.. I get the data in datagrid from the database ...after that when i check specific checkbox those row should be deleted from the datagrid / database ....i was writing the code for the same but am confused as to how to proceed further.....help would be appreciated....
My datagrid name is dg2
My checkbox column name is clm.

private void cmddelete_Click(object sender, EventArgs e)
        {
            string data = String.Empty;
            foreach (DataGridViewRow row in dg2.Rows)
            {
             
                
            }

you can add an if statement which controls clm is checked or not

if the statement is true

Removing specific datagrid row code is

dg2.Rows.Remove(dataGridView1.Rows[RowIndex]);

where RowIndex is an integer that specifies your row's index :)

dg2.Rows.Remove(dataGridView1.Rows[RowIndex]);

Wouldn't that simply remove the row from the gridview, and not the object from the database itself?

If your gridview is databound and has a datakey set up like the following:

<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="False" 
			DataSourceID="ObjectNewsletter" DataKeyNames="Id"/>

You could get the selected ID of objects quite simply:

private List<string> GetSelectedItems()
    {
        List<string> Selected = new List<string>();
		
        foreach (GridViewRow row in gridView1.Rows)
        {
            CheckBox chx = (CheckBox)row.Cells[0].FindControl("checkBox1");
            if (chx == null || !chx.Checked) continue;

            if (gridView1.DataKeys != null)
            {
                string id = gridView1.DataKeys[row.RowIndex].Value.ToString();
				Selected.Add(id);
            }
        }
		return Selected;
    }

With your ID, you could probably do whatever you want with it. ;-)

Thanks for the reply, however its a windows application, so can i use the same piece of code ? excluding the xml part of it ? i think it should work for me...what's say ?

If your gridview is databound and has a datakey set up like the following:

<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="False" 
			DataSourceID="ObjectNewsletter" DataKeyNames="Id"/>

You could get the selected ID of objects quite simply:

private List<string> GetSelectedItems()
    {
        List<string> Selected = new List<string>();
		
        foreach (GridViewRow row in gridView1.Rows)
        {
            CheckBox chx = (CheckBox)row.Cells[0].FindControl("checkBox1");
            if (chx == null || !chx.Checked) continue;

            if (gridView1.DataKeys != null)
            {
                string id = gridView1.DataKeys[row.RowIndex].Value.ToString();
				Selected.Add(id);
            }
        }
		return Selected;
    }

With your ID, you could probably do whatever you want with it. ;-)[/QUOTE]

No, you cant do that. The code is different for the forms.
You can try this
1) Detect the click event on the datagrid

dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);

2)Handle check for the right control do do the action required

void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 2)//check the column of the checkbox
                return;
            // your action go here
           //get the id for row, delete from database, delete from the  datatable that is source of your database            
        }

I dont know if there is a better way.

Please help me to proceed....i want to just delete the selected rows from the database on the click of delete button : Please bear wid me as i am new to coding.....:)

here is my code :

<div>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
       <asp:TemplateField >
        <ItemTemplate>
        <asp:CheckBox ID="chk" runat="server"/>
        </ItemTemplate>
        </asp:TemplateField>
            <asp:CheckBoxField />
        </Columns>
    </asp:GridView>

</div>

CODE BEHIND:

 protected void cmddel_Click(object sender, EventArgs e)
    {
        foreach(GridViewRow row in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)row.Cells[0].FindControl("chk");
            if(chk == null || !chk.Checked) continue;
            else

please help me out to proceed further.....please

cya
Rohan

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Is it a ASP.NET application or windows forms. Form post 4 it seems like a windows forms.
If its an ASP.NET thing then post 3 answers your question.If you need more detailed help provide more code or project as 'adatapost' sugested.

this is what I used,

<asp:GridView ID="GridViewCustomers" runat="server" AutoGenerateColumns="false" CssClass="grid2"
                                Width="100%" OnRowDataBound="GridViewCustomers_RowDataBound"
                                 EmptyDataText="No Customers Meet your Search Criteria">
                                <Columns>
                                    <asp:BoundField DataField="CustomerID" HeaderText="ID" />
                                    <asp:BoundField DataField="LastName" HeaderText="LastName" />
                                    <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                                    <asp:BoundField DataField="CustomerIsCompany" HeaderText="Is Company" />
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="CheckBox1" runat="server" Checked="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
foreach (GridViewRow objDr in GridViewCustomers.Rows)
        {
            CheckBox checkBox = (CheckBox)objDr.Cells[4].Controls[1];
            if (checkBox.Checked == true)
            {
                DataSet objDs = objCd.GetRecordSet("EXEC sproc '" +
                    objDr.Cells[0].Text + "'");
//Do stuff with record set
            }
        }

Hope this hepls where I get record set you can delete your record, "objDr.Cells[0].Text" points to the ID feild of the gridview

its a web applc...I just want to know what will be the delete statement like ? i mean what will be the syntax??? please let me know soon...am stuck badly!

cya
Rohan

Is it a ASP.NET application or windows forms. Form post 4 it seems like a windows forms.
If its an ASP.NET thing then post 3 answers your question.If you need more detailed help provide more code or project as 'adatapost' sugested.

the delete statement will be
"delete from <TabkeName> where <Conditions>"

When binding to the grid try and have a unique identifier on the grid. For instance let us say that on the grid column 4 contains the identifier and column 1 contains the checkbox. The follow code should be sufficient.

private void cmddelete_Click(object sender, EventArgs e)
        {
            string data = String.Empty;
            CheckBox chkdelete=new CheckBox();
            foreach (DataGridViewRow row in dg2.Rows)
            {
             // where i is the index of the cell
                chkdelete=(CheckBox) row.Cells[1].FindControl("clm");
                if(chkdelete.checked)
                 {
              //where conn is any Data connection object
               conn.ExecutNonQuery("delete from "table"    where "field"= " + row.cell[4].Text);

                  }
              
            }
               //Refresh the content of the grid
               BindGrid();
   }
        BindGrid()
         {
            //fecth data afresh into the datatable
              DataTable list;
              list=conn.ExecutNonQuery("select * from "table");
              dg2.DataSource=list;
             dg2.DataBind();
          }

If this solution solves the problem let me know.wish you good luck.

thanks,.........i think am almost der...however i am getting a silly error ; it says : Incorrect Syntax near '=", however i tried running the same query in the sqlserver and it had no problem..so plz check the code n do let me know..One more doubt, i have a checkbox clm followed by id , name etc....so my id column becomes cell[0] or cell[1] ?? am confused....plz let me know.....thanks in adv....

protected void cmddel_Click(object sender, EventArgs e)
    {
        string data = string.Empty;
       
        CheckBox chkdel = new CheckBox();
        foreach (GridViewRow row in GridView1.Rows)
        {
            chkdel = (CheckBox)row.Cells[0].FindControl("chk");
            if (chkdel.Checked)
            {
                con.Open(); 
                SqlCommand cmd = new SqlCommand("delete from myproducts where id= "+ row.Cells[1].Text, con);
                cmd.ExecuteNonQuery();

            }
        }
    }

cya
Rohan

When binding to the grid try and have a unique identifier on the grid. For instance let us say that on the grid column 4 contains the identifier and column 1 contains the checkbox. The follow code should be sufficient.

private void cmddelete_Click(object sender, EventArgs e)
        {
            string data = String.Empty;
            CheckBox chkdelete=new CheckBox();
            foreach (DataGridViewRow row in dg2.Rows)
            {
             // where i is the index of the cell
                chkdelete=(CheckBox) row.Cells[1].FindControl("clm");
                if(chkdelete.checked)
                 {
              //where conn is any Data connection object
               conn.ExecutNonQuery("delete from "table"    where "field"= " + row.cell[4].Text);

                  }
              
            }
               //Refresh the content of the grid
               BindGrid();
   }
        BindGrid()
         {
            //fecth data afresh into the datatable
              DataTable list;
              list=conn.ExecutNonQuery("select * from "table");
              dg2.DataSource=list;
             dg2.DataBind();
          }

If this solution solves the problem let me know.wish you good luck.

hey folks.....finally its done..........:) thanks a ton....
cya
Rohan

When binding to the grid try and have a unique identifier on the grid. For instance let us say that on the grid column 4 contains the identifier and column 1 contains the checkbox. The follow code should be sufficient.

private void cmddelete_Click(object sender, EventArgs e)
        {
            string data = String.Empty;
            CheckBox chkdelete=new CheckBox();
            foreach (DataGridViewRow row in dg2.Rows)
            {
             // where i is the index of the cell
                chkdelete=(CheckBox) row.Cells[1].FindControl("clm");
                if(chkdelete.checked)
                 {
              //where conn is any Data connection object
               conn.ExecutNonQuery("delete from "table"    where "field"= " + row.cell[4].Text);

                  }
              
            }
               //Refresh the content of the grid
               BindGrid();
   }
        BindGrid()
         {
            //fecth data afresh into the datatable
              DataTable list;
              list=conn.ExecutNonQuery("select * from "table");
              dg2.DataSource=list;
             dg2.DataBind();
          }

If this solution solves the problem let me know.wish you good luck.

This question has already been answered. Start a new discussion instead.