How to save datagridview records in a database table?

In my application, I am showing a database table in a datagridview. Now I want to change and modify some records and save these changes to the database. How can i do this?

+3
source share
3 answers

TableAdapter is one way to do this.

+2
source

Here is an example with a BindingSource:

      string query = "SELECT * FROM dbo.bimar";
      da = new SqlDataAdapter(query, connectionString);
      SqlCommandBuilder cBuilder = new SqlCommandBuilder(da);
      dt = new DataTable();

      da.Fill(dt);

      BindingSource bSource = new BindingSource();
      bSource.DataSource = dt;

      dataGridView1.DataSource = bSource;

when you change the data in dataGridView1, update it:

 private void button1_Click(object sender, EventArgs e)
    {
        da.Update(dt);
    }
+2
source
 objDataAdapter.SelectCommand = new SqlCommand();
        objDataAdapter.SelectCommand.Connection = objConnection;
        objDataAdapter.SelectCommand.CommandText = "select code,name,family,fatherName,age from bimar";
        objDataAdapter.SelectCommand.CommandType = CommandType.Text;
        objConnection.Open();
        objDataAdapter.Fill(objDataSet, "bimar");
        objConnection.Close();
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = objDataSet;
        dataGridView1.DataMember = "bimar";

I use this code to show my table in a DataGridView, I want after some work on DatagridView the data is inserted back into db

0
source

Source: https://habr.com/ru/post/1789250/


All Articles