banner



How To Update Cells In Data Grid

  • Updated date May 21, 2020
  • 2m
  • 49

This article shows how to insert, update and delete records in a DataGridView in a C# Windows Forms awarding.

Introduction

This article shows how to insert, update, and delete records in a DataGridView in a C# Windows Forms awarding. In a previous article, we saw how to use a Chart Control in Windows Forms Application.

Let's Begin

  1. Create a new Windows Forms application.

  2. Create a database (named Sample). Add together a table tbl_Record. The following is the tabular array schema for creating tbl_Record.

  3. Create a class (named frmMain) and drop a Characterization, TextBox, Push, and DataGridView control from the ToolBox.

Now, go to frmMain.cs code and add the Arrangement.Data and Organization.Data.SqlClient namespaces.

frmMain.cs Code

  1. using  Arrangement;
  2. using  System.Data;
  3. using  Organization.Windows.Forms;
  4. using  Organization.Data.SqlClient;
  5. namespace  InsertUpdateDeleteDemo
  6. {
  7. public  partial course  frmMain : Form
  8.     {
  9.         SqlConnection con=new  SqlConnection( "Data Source=.;Initial Catalog=Sample;Integrated Security=truthful;" );
  10.         SqlCommand cmd;
  11.         SqlDataAdapter adapt;
  12. int  ID = 0;
  13. public  frmMain()
  14.         {
  15.             InitializeComponent();
  16.             DisplayData();
  17.         }
  18. private void  btn_Insert_Click( object  sender, EventArgs e)
  19.         {
  20. if  (txt_Name.Text != ""  && txt_State.Text != "" )
  21.             {
  22.                 cmd =new  SqlCommand( "insert into tbl_Record(Proper noun,Land) values(@name,@country)" , con);
  23.                 con.Open();
  24.                 cmd.Parameters.AddWithValue("@proper name" , txt_Name.Text);
  25.                 cmd.Parameters.AddWithValue("@state" , txt_State.Text);
  26.                 cmd.ExecuteNonQuery();
  27.                 con.Close();
  28.                 MessageBox.Show("Tape Inserted Successfully" );
  29.                 DisplayData();
  30.                 ClearData();
  31.             }
  32. else
  33.             {
  34.                 MessageBox.Show("Please Provide Details!" );
  35.             }
  36.         }
  37. private void  DisplayData()
  38.         {
  39.             con.Open();
  40.             DataTable dt=new  DataTable();
  41.             adapt=new  SqlDataAdapter( "select * from tbl_Record" ,con);
  42.             adapt.Fill(dt);
  43.             dataGridView1.DataSource = dt;
  44.             con.Shut();
  45.         }
  46. private void  ClearData()
  47.         {
  48.             txt_Name.Text ="" ;
  49.             txt_State.Text ="" ;
  50.             ID = 0;
  51.         }
  52. private void  dataGridView1_RowHeaderMouseClick( object  sender, DataGridViewCellMouseEventArgs e)
  53.         {
  54.             ID = Catechumen.ToInt32(dataGridView1.Rows[eastward.RowIndex].Cells[0].Value.ToString());
  55.             txt_Name.Text = dataGridView1.Rows[due east.RowIndex].Cells[1].Value.ToString();
  56.             txt_State.Text = dataGridView1.Rows[due east.RowIndex].Cells[2].Value.ToString();
  57.         }
  58. private void  btn_Update_Click( object  sender, EventArgs e)
  59.         {
  60. if  (txt_Name.Text != ""  && txt_State.Text != "" )
  61.             {
  62.                 cmd =new  SqlCommand( "update tbl_Record ready Proper name=@name,State=@state where ID=@id" , con);
  63.                 con.Open();
  64.                 cmd.Parameters.AddWithValue("@id" , ID);
  65.                 cmd.Parameters.AddWithValue("@name" , txt_Name.Text);
  66.                 cmd.Parameters.AddWithValue("@state" , txt_State.Text);
  67.                 cmd.ExecuteNonQuery();
  68.                 MessageBox.Show("Tape Updated Successfully" );
  69.                 con.Close();
  70.                 DisplayData();
  71.                 ClearData();
  72.             }
  73. else
  74.             {
  75.                 MessageBox.Show("Please Select Record to Update" );
  76.             }
  77.         }
  78. private void  btn_Delete_Click( object  sender, EventArgs east)
  79.         {
  80. if (ID!=0)
  81.             {
  82.                 cmd =new  SqlCommand( "delete tbl_Record where ID=@id" ,con);
  83.                 con.Open();
  84.                 cmd.Parameters.AddWithValue("@id" ,ID);
  85.                 cmd.ExecuteNonQuery();
  86.                 con.Close();
  87.                 MessageBox.Show("Tape Deleted Successfully!" );
  88.                 DisplayData();
  89.                 ClearData();
  90.             }
  91. else
  92.             {
  93.                 MessageBox.Evidence("Please Select Record to Delete" );
  94.             }
  95.         }
  96.     }
  97. }

In the preceding lawmaking, I created a dataGridView1_RowHeaderMouseClick Event for updating and deleting the selected tape. When the user clicks on the Row Header of a row then the data present in the prison cell of the row is stored into the TextBoxes. The DisplayData() method fills in the data in the DataGridView.

The Clear() method clears the data nowadays in the TextBox as well as in the ID(int) variable.

Final Preview

I hope you like information technology. Thanks.

How To Update Cells In Data Grid,

Source: https://www.c-sharpcorner.com/UploadFile/1e050f/insert-update-and-delete-record-in-datagridview-C-Sharp/

Posted by: myerstimentep.blogspot.com

0 Response to "How To Update Cells In Data Grid"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel