This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Sunday, February 5, 2023

How to Insert, Update and Delete Records in DataGridView in C#

 How to Insert, Update and Delete Records in DataGridView in C#


Are you finding it difficult to control the records in the C# DataGridView? Do you wish to use your DataGridView control to carry out fundamental CRUD operations? If so, you've come to the correct place. This tutorial will walk you through adding, updating, and deleting records in a DataGridView using C#.


Introduction:


A sophisticated control called DataGridView is used to present data in a tabular fashion. To interact with the data and manage it properly, it offers a wide range of functions. Inserting new records, updating current records, and removing records are the three most frequent activities on a DataGridView. We'll demonstrate how to carry out these procedures in C# in this article.




Inserting a Record

To insert a record in the DataGridView control, you need to follow the steps mentioned below:


First, you need to create a DataGridView control in your form.

Next, you need to bind the DataGridView control to a data source. This data source can be either a database, XML file, or an array.

Once the DataGridView control is bound to the data source, you can insert a new record by calling the Add method of the data source.

After adding the record, you can save the changes to the data source by calling the SaveChanges method.

Here's a code example of how to insert a new record in the DataGridView control:




using System;

using System.Data;

using System.Data.SqlClient;


private void btnInsert_Click(object sender, EventArgs e)

{

   // Create a new DataRow

   DataRow row = dt.NewRow();


   // Add data to the DataRow

   row["ID"] = 1;

   row["Name"] = "John Doe";

   row["Age"] = 30;


   // Add the DataRow to the DataTable

   dt.Rows.Add(row);


   // Save changes to the database

   adapter.Update(dt);

}





Updating a Record

To update a record in the DataGridView control, you need to follow the steps mentioned below:


First, you need to locate the record that you want to update.

Next, you need to update the values of the columns in the record.

Finally, you need to save the changes to the data source by calling the SaveChanges method.

Here's a code example of how to update a record in the DataGridView control:





using System;

using System.Data;

using System.Data.SqlClient;


private void btnUpdate_Click(object sender, EventArgs e)

{

   // Locate the record that you want to update

   DataRow[] rows = dt.Select("ID = 1");


   // Update the values of the columns in the record

   rows[0]["Name"] = "Jane Doe";

   rows[0]["Age"] = 35;


   // Save changes to the database

   adapter.Update(dt);

}





Deleting a Record

To delete a record in the DataGridView control, you need to follow the steps mentioned below:


First, you need to locate the record that you want to delete.

Next, you need to call the Delete




method on the DataRow object that represents the record.

3. Finally, you need to save the changes to the data source by calling the SaveChanges method.


Here's a code example of how to delete a record in the DataGridView control:




using System;

using System.Data;

using System.Data.SqlClient;


private void btnDelete_Click(object sender, EventArgs e)

{

   // Locate the record that you want to delete

   DataRow[] rows = dt.Select("ID = 1");


   // Call the Delete method on the DataRow object

   rows[0].Delete();


   // Save changes to the database

   adapter.Update(dt);

}


Conclusion

In this article, we have shown you how to insert, update and delete records in DataGridView in C#. With these basic CRUD operations, you can effectively manage the records in your DataGridView control. We hope this article has been helpful in guiding you in your journey to master DataGridView in C#.

No comments:

Post a Comment