I am new to programming and I have created the following button to search the dataGridView
private void btnInCollection1_Click(object sender, EventArgs e)
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM dbo.SS1 WHERE InCollection = 1 ", con);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}
I want to edit the data from dataGridView and then save the changes using another button. Could you please help me with the code for the saving?
I tried the following code but it does not save the data back to the database.
private void btnSaveGridView_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try
{
sS1BindingSource2.EndEdit();
sS1TableAdapter1.Update(this.sSDataSet1.SS1);
MessageBox.Show("You have been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Cursor.Current = Cursors.Default;
}
You've done something wrong with this setup
This line:
sS1TableAdapter1.Update(this.sSDataSet1.SS1);
Implies you have a strongly typed dataset, with tableadapters and datatables set up, but your btnInCollection1_Click
code implies you've bypassed all that, made a weakly typed datatable, filled it from a data adapter and assigned it to the datasource of the grid. It works, but it's a bit bit buying a car and then pushing it to and from work rather than driving it
This alsomeans your datagridview is showing data in one datatable but you're trying to save data located in a different datatable
I'd recommend you throw all that code in the btnInCollection1_Click
away; VS already wrote it for you when you made the tableadapter so having bought that dog you don't need to bark yourself.
Replace the code with this:
sS1TableAdapter1.Fill(this.sSDataSet1.SS1);
Elsewhere in the code, if you don't already have it, put this one time (eg in the constructor):
dataGridView1.DataSource = sS1BindingSource2;
sS1BindingSource2.DataSOurce = this.sSDataSet1.SS1;
You might already have these lines in the .Designer.cs - you don't need them again if you do.
It's probably not optimal but I can't easily determine the necessary extra info from what you've posted
ss1bindingsource
bound to your sSDataSet
(the grid is bound to the bindingsource. In the code behind you should have a Fill command that uses your tablaeadapter to fill the grid. Also present is a bindingnavigator bearing a save buttonYou don't need to write any code to fill your grid, or save it - if you look in the code file of the new form you'll see that VS wrote it all for you
If you want to perform parameterized searching using tableadapters, open your dataset, right click the adapter, choose to add a query and input eg SELECT * FROM dbo.SS1 WHERE InCollection = @inColl
. Call the method something relevant like FillByInCollection
(don't call it FillBy1 - endlessly appending numbers to method and variable names just to make them unique is a silly idea and makes for highly unreadable programs) then in your relevant button click call it like:
sS1TableAdapter1.Fill(this.sSDataSet1.SS1, 1); //fill collectionID 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.