Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
delete selected row from datagridview and database in c#
Posted
Updated 26-Oct-17 0:41am

C#
for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chk = new CheckBox();
            chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (!chk.Checked)
            {
              
                GridView1.DeleteRow(i);  // and also you can fire database query
                            
            }
        }
 
Share this answer
 
v2
Comments
TrushnaK 7-Jun-14 3:51am    
you are late see the question asked on 16-Aug-13.
This code will delete selected items of a dataGridView.
C#
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
    {
        dataGridView1.Rows.RemoveAt(item.Index);
    }

For rest, you need to refresh your datagridview source.Check..
Delete row from Datagridview and same updates in database[^]
How to delete a selected row from datagridview and database[^]
How to Delete selected row in data grid view and database by C#[^]
 
Share this answer
 
Use this code to Delete Row From DataGridView

C#
if (this.dataGridView1.SelectedRows.Count > 0)
            {
                dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
            }


and This code for Delete data from Database

first of all you find and store primary field data in a Temp variable and pass this variable in where clause

Ex:-

if your primary field name is Emp_Id and it's it's position in Grid is cell[0]
then
C#
int Primary_Field_Value =dataGridView1[Column_Index,Row_Index].value.Tostring();

SqlCommand cmd = new SqlCommand ("Delete from table where id='"+ Primary_Field_Value +"'",ConnectionObject); 
cmd.ExecutenonQuery(); 


Thanks & Regard
Sham :)
 
Share this answer
 
In source part do this:

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="User Name">
                <ItemTemplate>
                    <%#Eval("userName")%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Password">
                <ItemTemplate>
                    <%#Eval("pass")%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Test">
                <ItemTemplate>
                    <%#Eval("test")%>
                </ItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField >
                <ItemTemplate>
                    <a href="Default.aspx?id=<%#Eval("userId") %>&act=del">Delete</a>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

// Then in code behind:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string qact = Request.QueryString["act"];
            if (qact == "del")
            {
                string id = Request.QueryString["id"];
                string str = ConfigurationManager.ConnectionStrings["class28"].ToString();
                SqlConnection con = new SqlConnection(str);
                string sql = "delete from t_User where userId=" + int.Parse(id) + "";
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900