Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to delete data from datagridview in windows application c#
Posted
Comments
ridoy 17-Aug-13 2:58am    
not a question at all.

The first option:
C#
myDataGridView.Rows.Clear() 


The second option:
C#
//Here we have to check if we are bound  to a data source
if (myDataGridView.DataSource != null)                                  
{
     myDataGridView.DataSource = null;
}
else
{
    myDataGridView.Rows.Clear();
}


The third option:
C#
datatbl.Rows.Clear() // If DataGridView is bound to DataTable
myDataGridView.DataBind();
 
Share this answer
 
datagridview-in-wndows-form.html[^]

Check this link..hope it will help..
 
Share this answer
 
Here showdata is my datagridview name
First Fire CellClick Event of your datagridview
the use it


if (e.ColumnIndex == "ur button column index")
 {
 if (DialogResult.Yes == MessageBox.Show("Are you sure You Want to delete?",MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                          {
                              int ch = 0;
                              int id = ShowData.CurrentRow.Index;
                              int dd = convert.ToInt32(ShowData.Rows[id].Cells["ID"].Value.ToString());
                              string Delete = "Delete from ReceivePayment where ID=" + dd + "";
                              OleDbCommand cmd = new OleDbCommand(Delete, Ut.con);
                              Ut.con.Open();
                              int x = cmd.ExecuteNonQuery();
                              Ut.con.Close();
                              if (x != 0)
                              {
                                MessageBox.Show("data delete seccesfully ", ,MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                              }
                              
                          }
                      }


select as answer.....
 
Share this answer
 
v2
Write this code on Button_Click Event
SQL
if (this.dataGridView1.SelectedRows.Count > 0)
            {                        dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
            }

Thanks & Regard
Sham :)
 
Share this answer
 
That depends on how you wants to delete your data from datagridview.Here i mentioned some links that describes different delete approaches:
Remove Rows from DataGridView C#[^]
Deleting all the data from a datagridview[^]
How to clear data in DataGridView?[^]
Insert, Delete, Update in DataGridView with DataTable in C#[^]
 
Share this answer
 
Following is solution Deleting a record via Delete button or by clicking keyboard delete button for selected row

C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.dgview.CellClick += new DataGridViewCellEventHandler(this.dgview_CellClick);
            this.dgview.UserDeletingRow += dgview_UserDeletingRow;
            loadData();
        }


C#
private void loadData()
        {
            try
            {                
                   DataTable dt =new DataTable();
                   DataColumn dc;
                   dc=new DataColumn("ID", typeof(int));
                   dt.Columns.Add(dc);
                   dc=new DataColumn("FirstName", typeof(string));
                   dt.Columns.Add(dc);
                   dc=new DataColumn("LastName", typeof(string));
                   dt.Columns.Add(dc);                  
                 
                   dt.Rows.Add(1,"Ali","khan");
                   dt.Rows.Add(1,"Affan","raja");
                   dt.Rows.Add(1,"Amjad","marwat");
                   dt.Rows.Add(1,"Zohaib","awan");

                    dgview.DataSource = dt;
                
                    foreach (DataGridViewColumn column in dgview.Columns)
                    {
                        column.SortMode = DataGridViewColumnSortMode.NotSortable;
                        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    }

                    if (dt.Rows.Count > 0)
                    {
                        DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
                        dgview.Columns.Add(btn);
                        btn.HeaderText = "Delete";
                        btn.Text = "Delete";
                        btn.Name = "btnDelete";
                        btn.UseColumnTextForButtonValue = true;
                    }
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

// this can work when you press delete button in gridview at columIndex=3
C#
private void dgview_CellClick(object sender, DataGridViewCellEventArgs e)
       {
           if (e.ColumnIndex == 3)
           {
               dgview.Rows.RemoveAt(e.RowIndex);
               int ID = Convert.ToInt32(dgview.Rows[e.RowIndex].Cells["ID"].Value.ToString());
               //Send call to you db for updated the ISDelete bit where yourPrimaryID=ID
           }
       }


// This can work when select a row and clicking delete key from keyboard

C#
private void dgview_UserDeletingRow(object sender,DataGridViewRowCancelEventArgs e)
       {
           DialogResult response = MessageBox.Show("Are you sure you want to delete this row?", "Delete row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
           if ((response == DialogResult.No))
           {
               e.Cancel = true;
           }
           else
           {
               //int ID = e.Row.Cells["yourPrimaryID"].Value.ToString();
               //Send call to you db for updated the ISDelete bit where yourPrimaryID=ID
           }
       }
 
Share this answer
 
v3
HTML


fgdfg
C++
<pre lang="CSS"><pre lang="c#"><pre lang="Delphi"><pre lang="F#"><pre lang="HTML"><pre lang="java"><pre lang="Javascript"><pre lang="objc"><pre lang="Perl"><pre lang="PHP"><pre lang="vb"><pre lang="xml">
 
Share this answer
 

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