Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,,

I have datagridview and have two buttons DELETE AND EDIT... i want to code like if i select one row from datagridview and click on delete button that selected row gets deleted and updated in database also, same for the edit row also.

Please suggest code for this button click event.

Thank you.
Posted
Comments
Sandeep Mewara 29-Jun-12 11:10am    
What have you tried so far? Any effort?

Hi Teena

I hope this can help you:

private void datagridview _CellClick(object sender, DataGridViewCellEventArgs e)
{

  // Ignore others clicks
  if (e.RowIndex < 0 || e.ColumnIndex = datagridview.Column"Col_DeButton"].Index)
    return;
  else
  {
    // Delete from database
    int iRtnVal = 0;

    SqlConnection sqlConn = new SqlConnection(Conexion);
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandText = "DELETE FROM ...";
    sqlCmd.Connection = sqlConn;

    try
    {
      sqlCmd.Connection.Open();
      iRtnVal = sqlCmd.ExecuteNonQuery();
    }
    catch (Exception Ex)
    {
      Thread.CurrentThread.Abort();
    }
    finally
    {
      sqlCmd.Connection.Close();
    }

    // Reload the datagridview
    SqlDataAdapter sqlAdapt = new SqlDataAdapter("SELECT * FROM ...", sqlConn);
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();

    try
    {
      sqlConn.Open();
      sqlAdapt.Fill(ds, "Datas");
    }
    catch (Exception Ex)
    {
      Thread.CurrentThread.Abort();
    }
    finally
    {
      sqlConn.Close();
    }

    if (ds.Tables["Datos"] != null)
      dt = ds.Tables["Data"];
    else
      dt = null;


    datagridview.datasource = dt;
  }
}
 
Share this answer
 
 
Share this answer
 
Comments
madanbhanu 17-Jul-12 6:48am    
hello friends,
I am new at windows application.I started from base,i want to do an application in which i have to save,update and delete record from the datagridview in front end and it automatically at the back end(sql server 2008).I am succeded to save record in database using textboxes..but now i want to edit my datagridview and update each record.i want to edit every coloumn in datagridview itself and update each record using ado.net in C#.
Can any one please help me out fast,its urgent...
thanks in advance
_Amy 17-Jul-12 7:30am    
Hi,
You can use the above links which I'hv given to teena. For better performance in gridview I wrote an article here:
http://www.codeproject.com/Articles/412802/Sending-a-DataTable-to-a-Stored-Procedure
You can also use it for your datagrid.
madanbhanu 18-Jul-12 8:33am    
thanks sir,but i don't want that when i clik on edit button the records are come into textboxes and through this i update the records.
Actually i want that we only have the datagridview with some records and in datagridview itself i cn edit it's cells and update into the datagridview without using textboxes.
I am definitely sure that u will help me ,thanks in advance sir.
hello teena,

go to grid-view properties->events-> select rowdelete /rowedit event

then go to c# code

and in that function follow the billow example.



C#
void CustomersGridView_RowDeleting
       (Object sender, GridViewDeleteEventArgs e)
   {
       TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
       if (cell.Text == "Beaver")
       {
           e.Cancel = true;
           Message.Text = "You cannot delete customer Beaver.";
       }
       else
       {
           Message.Text = "";
       }
   }




for further reference refer this link

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx[^]
 
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