I have bound my MSSQL datatable to a datagridview using the designer (the datatable has atleast 30 columns), it fills the data like this:
this.produktaiTableAdapter.Fill(this.veiklosDuomenysDataSet.Produktai);
this.produktaiBindingSource.Filter = advancedDataGridView1.Columns[1].HeaderText + " = " + Veiklos_ID;
It is necessary for me to bind it like this, as I have a lot of code concerning every single column in the DGV (for example, width, currencies, and so on).
Now I want to save all the new, updated, and deleted data in datatagridview to my MSSQL datatable by clicking a Save button. How can I do that? How can I convert my edited datagridview to the datatable?
I also tried this code on my save button click:
this.Validate();
this.produktaiBindingSource.EndEdit();
this.produktaiTableAdapter.Update(this.veiklosDuomenysDataSet.Produktai);
And.. It doesn't work. I am fairly new with datagridview and datatables overall as this problem I think should have an easy fix.
EDIT:
Also, could this line help somehow?
DataTable Produktai = advancedDataGridView1.DataSource as DataTable;
EDIT2: This doesn't work either.
veiklosDuomenysDataSet.Naudotojai.AcceptChanges();
this.Validate();
this.produktaiBindingSource.EndEdit();
this.produktaiTableAdapter.Update(this.veiklosDuomenysDataSet.Produktai);
MessageBox.Show("Data is saved!");
EDIT3:
This is a debug picture of my main form: Here
As you can see I have no textbox or buttons like INSERT, DELETE and UPDATE. The user can edit the DGV simply by clicking on a cell and editing the value, add new rows simply by pressing ENTER, deleting the row by clicking Delete (Ištrinti in picture) on the same selected row (NOTE: delete button column is the only unbound column in DGV).
I use MSSQL datatable to bound it with the datagridview, and I want all the rows that are new, deleted or edited in the datagridview to be renewed/updated in the datatable that it is bounded too by save button.
Maybe there should be something different with this information, a wild guess?
There's nothing wrong with the posted code in terms of "that's how you save" so the diagnosis needs to methodically chase things through:
var x = tableadapter.update(..)
) and step the debugger over the call to updateStart over - this will generate a basis for comparison
I didn't encounter any problems saving when I did:
Your form code will now just be something like:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myTableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.myTableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.newDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'newDataSet.MyTable' table. You can move, or remove it, as needed.
this.myTableTableAdapter.Fill(this.newDataSet.MyTable);
}
}
And you won't have written any of it. It's all that's required to load, show, edit and save data using a datagridview. The call to tableAdapterManager.UpdateAll
is equivalent to someTableAdapter.Update
- a tableadaptermanager is a convenience device that calls Update in the correct order to ensure that parent rows are created before related child rows
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.