Click here to Skip to main content
11,705,451 members (47,290 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 29-Jun-12 4:02am
Comments
Sandeep Mewara at 29-Jun-12 11:10am
   
What have you tried so far? Any effort?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
  }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
madanbhanu at 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
_A_K_ at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.



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[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 286
1 OriginalGriff 269
2 ppolymorphe 78
3 Inimicos 60
4 ProgramFOX 50
0 OriginalGriff 9,053
1 Sergey Alexandrovich Kryukov 8,347
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 13 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100