Thursday 13 March 2014

Insert, Update, Delete with DataGridView Control in C# (Windows Application)

DataGridView control is one of the coolest features of Dot Net Framework. You can use this control in Windows Form using Wizard or programmatically.
Add a DataGridView control and two Buttons on a Form. One Button is for deleting the selected row and other one is for updating or inserting new row.
Declare those variables as shown below.
public partial class DataTrialForm : Form
    {
        private String connectionString = null;
        private SqlConnection sqlConnection = null;
        private SqlDataAdapter sqlDataAdapter = null;
        private SqlCommandBuilder sqlCommandBuilder = null;
        private DataTable dataTable = null;
        private BindingSource bindingSource = null;
        private String selectQueryString = null;

        public DataTrialForm()
        {
            InitializeComponent();
        }
In the Form Load event set data source for the DataGridView control.
private void DataTraiForm_Load(object sender, EventArgs e)
        {
            connectionString = ConfigurationManager.AppSettings["connectionString"];
            sqlConnection = new SqlConnection(connectionString);
            selectQueryString = "SELECT * FROM t_Bill";

            sqlConnection.Open();

            sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
            sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

            dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);
            bindingSource = new BindingSource();
            bindingSource.DataSource = dataTable;

            dataGridViewTrial.DataSource = bindingSource;
            
            // if you want to hide Identity column
            dataGridViewTrial.Columns[0].Visible = false;
        }

To update, insert or delete data in database from DataGridView have a look at this code snippet.
private void addUpadateButton_Click(object sender, EventArgs e)
        {
            try
            {
                sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
                MessageBox.Show(exceptionObj.Message.ToString());
            }
        }


        private void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
               dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
               sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
               MessageBox.Show(exceptionObj.Message.ToString());
            }
        }
Download Source Code and start exploring DataGridView control.

2 comments:

  1. my query is str="select* from tablename where field1=' "+ textbox1.text +" ', then how can i update this dataset after adding new row in datagridview

    ReplyDelete
    Replies
    1. You can use below sample code for refresh GridView:

      dataGridViewTrial.Refresh();

      And second approach,

      dataGridViewTrial.DataSource = null;
      dataGridViewTrial.DataSource = src1;

      Delete