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
- Create a new Windows Forms application.
- Create a database (named Sample). Add together a table tbl_Record. The following is the tabular array schema for creating tbl_Record.
- 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
- using Arrangement;
- using System.Data;
- using Organization.Windows.Forms;
- using Organization.Data.SqlClient;
- namespace InsertUpdateDeleteDemo
- {
- public partial course frmMain : Form
- {
- SqlConnection con=new SqlConnection( "Data Source=.;Initial Catalog=Sample;Integrated Security=truthful;" );
- SqlCommand cmd;
- SqlDataAdapter adapt;
- int ID = 0;
- public frmMain()
- {
- InitializeComponent();
- DisplayData();
- }
- private void btn_Insert_Click( object sender, EventArgs e)
- {
- if (txt_Name.Text != "" && txt_State.Text != "" )
- {
- cmd =new SqlCommand( "insert into tbl_Record(Proper noun,Land) values(@name,@country)" , con);
- con.Open();
- cmd.Parameters.AddWithValue("@proper name" , txt_Name.Text);
- cmd.Parameters.AddWithValue("@state" , txt_State.Text);
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("Tape Inserted Successfully" );
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please Provide Details!" );
- }
- }
- private void DisplayData()
- {
- con.Open();
- DataTable dt=new DataTable();
- adapt=new SqlDataAdapter( "select * from tbl_Record" ,con);
- adapt.Fill(dt);
- dataGridView1.DataSource = dt;
- con.Shut();
- }
- private void ClearData()
- {
- txt_Name.Text ="" ;
- txt_State.Text ="" ;
- ID = 0;
- }
- private void dataGridView1_RowHeaderMouseClick( object sender, DataGridViewCellMouseEventArgs e)
- {
- ID = Catechumen.ToInt32(dataGridView1.Rows[eastward.RowIndex].Cells[0].Value.ToString());
- txt_Name.Text = dataGridView1.Rows[due east.RowIndex].Cells[1].Value.ToString();
- txt_State.Text = dataGridView1.Rows[due east.RowIndex].Cells[2].Value.ToString();
- }
- private void btn_Update_Click( object sender, EventArgs e)
- {
- if (txt_Name.Text != "" && txt_State.Text != "" )
- {
- cmd =new SqlCommand( "update tbl_Record ready Proper name=@name,State=@state where ID=@id" , con);
- con.Open();
- cmd.Parameters.AddWithValue("@id" , ID);
- cmd.Parameters.AddWithValue("@name" , txt_Name.Text);
- cmd.Parameters.AddWithValue("@state" , txt_State.Text);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Tape Updated Successfully" );
- con.Close();
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please Select Record to Update" );
- }
- }
- private void btn_Delete_Click( object sender, EventArgs east)
- {
- if (ID!=0)
- {
- cmd =new SqlCommand( "delete tbl_Record where ID=@id" ,con);
- con.Open();
- cmd.Parameters.AddWithValue("@id" ,ID);
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("Tape Deleted Successfully!" );
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Evidence("Please Select Record to Delete" );
- }
- }
- }
- }
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