Reputation: 16407
In my application, I show database table in a datagridview. Now I want to modify and change some records and save this changes on database. How can I do this?
Upvotes: 4
Views: 7267
Reputation: 1397
Here is one example with 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 your data in dataGridView1 , update it:
private void button1_Click(object sender, EventArgs e)
{
da.Update(dt);
}
Upvotes: 3
Reputation: 16407
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 on DataGridView , i want to after doing some work on DatagridView the data gets inserted back to db
Upvotes: 0