Save changes to a datagridview record when the change has been made from a form
-
Wednesday, May 09, 2012 8:17 AM
I am new at C# and would like to doubleclick on a datagridview and open a form. which i have done opening the correct record but I can't seem to save the record to the table. If I close the window it shows the correct data in the datagridview but when I close the program and open it again the old data remains no changes have taken place.
Here is what i have: in the Main
private void gridControl1_DoubleClick_1(object sender, EventArgs e)
{
BindingSource UpClient = new BindingSource();
UpdClient UpdClnt = new UpdClient(this.crmClientBindingSource);
UpdClnt.ShowDialog();
}This is in the form:
public partial class UpdClient : Form
{
public UpdClient(BindingSource UpClient) // UpClient is the data in binding source needed to fill the form 5/7/12 fak
{
InitializeComponent();
this.ItemForClientID.DataBindings.Add("Text", UpClient, "ClientID");
this.ContactNameTextEdit.DataBindings.Add("Text", UpClient, "ContactName");
this.FirstNameTextEdit.DataBindings.Add("Text", UpClient, "FirstName");
this.LastNameTextEdit.DataBindings.Add("Text", UpClient, "LastName");}
Thanks
Frank
- Moved by CoolDadTxMVP Monday, May 14, 2012 1:52 PM Winforms related (From:Visual C# General)
All Replies
-
Thursday, May 10, 2012 6:57 AMModerator
You will need the EndEdit method call to the DataGridView to make it commit the changes to the DataSource.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
Thursday, May 10, 2012 7:25 AM
Frank,
You are talking about a datagridview but shows code meant for databinding to textboxes and nothing for a datagridview
What are we missing?
Success
Cor -
Friday, May 11, 2012 5:23 AM
what i would like to happen is double click on the datagrid, which then open the form (using the same table that the grid is using) make the changes to the form. Click a Save button and update the table on the backend.
On the return from the form the datagrid shows the changes but the Table does not. I checked it in Management Studio.
Thanks
Frank
-
Friday, May 11, 2012 5:47 AM
I think you need to commit the changes from the datasource behind the datagrid
How to: Commit Changes in a Dataset
http://msdn.microsoft.com/en-us/library/ceab2k93(v=vs.80).aspx
Regards,
Ahmed Ibrahim
SQL Server Setup Team
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
This can be beneficial to other community members reading the thread. -
Friday, May 11, 2012 6:32 AM
I added the cRMDataSet.crmClient.AcceptChanges(); to the form being called save button click event. Blanks the current row that i was editing. close the program open it up and the data is in the same state before any change were made. Would I have to pass the data from UpClient to the table if so this is what i do not know what to do.
This is the code in my form:
public partial class UpdClient : Form
{
public UpdClient(BindingSource UpClient) // UpClient is the data in binding source needed to fill the form 5/7/12 fak
{
InitializeComponent();this.ContactNameTextEdit.DataBindings.Add("Text", UpClient, "ContactName");
this.FirstNameTextEdit.DataBindings.Add("Text", UpClient, "FirstName");
this.LastNameTextEdit.DataBindings.Add("Text", UpClient, "LastName");
this.AddressTextEdit.DataBindings.Add("Text", UpClient, "Address");
this.Address2TextEdit.DataBindings.Add("Text", UpClient, "Address2");
}private void simpleButton1_Click(object sender, EventArgs e)
{
try
{
cRMDataSet.crmClient.AcceptChanges(); // this blank the record when returning to the datagrid view.the code below does not work updating the table.// this.Validate();
// this.crmClientBindingSource.EndEdit();
// this.crmClientTableAdapter.Update(this.cRMDataSet.crmClient);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
}Close();
}
-
Friday, May 11, 2012 6:48 AM
Can you try
public partial class UpdClient : Form { public UpdClient(BindingSource UpClient) // UpClient is the data in binding source needed to fill the form 5/7/12 fak { InitializeComponent(); this.ContactNameTextEdit.DataBindings.Add("Text", UpClient, "ContactName"); this.FirstNameTextEdit.DataBindings.Add("Text", UpClient, "FirstName"); this.LastNameTextEdit.DataBindings.Add("Text", UpClient, "LastName"); this.AddressTextEdit.DataBindings.Add("Text", UpClient, "Address"); this.Address2TextEdit.DataBindings.Add("Text", UpClient, "Address2"); } private void simpleButton1_Click(object sender, EventArgs e) { try { cRMDataSet.crmClient.AcceptChanges(); // this blank the record when returning to the datagrid view.the code below does not work updating the table. this.Validate(); this.crmClientBindingSource.EndEdit(); this.crmClientTableAdapter.Update(this.cRMDataSet.crmClient); MessageBox.Show("Update successful"); } catch (System.Exception ex) { MessageBox.Show("Update failed"); } Close(); }
Regards,
Ahmed Ibrahim
SQL Server Setup Team
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
This can be beneficial to other community members reading the thread.- Proposed As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Friday, May 11, 2012 7:01 AM
-
Friday, May 11, 2012 7:02 AM
Hi Ahmed,
Does the samething as before. Blanks the row i'm editing in the datagrid, and does not change the record in the table. Open to other suggestions.
Thank,
Frank
-
Friday, May 11, 2012 7:07 AMCan you make sure before hitting the button that focus is not in any cell of the edited row.
Regards,
Ahmed Ibrahim
SQL Server Setup Team
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
This can be beneficial to other community members reading the thread. -
Friday, May 11, 2012 7:11 AMModerator
Table in Database -> DataTable in DataSet -> the data displayed on DataGridView -> row data filled by a Form(just named FormA)
When you close the FormA, what about the data on the DataGridView, does the data updated.
Please point out which step data has not been updated after you close the FormA, so that we can narrow down which step is the probable root cause place.
If the DataGridView is updated, then use a button click event to execute a print method, to print out the DataTable data, then you can ensure if the DataTable is updated.
For the Table in Database, please go to your Sql Server and check the data.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
Friday, May 11, 2012 7:28 AM
Hi Ahmed,
Can you make sure before hitting the button that focus is not in any cell of the edited row. Did try it still the same results. If i remove
cRMDataSet.crmClient.AcceptChanges(); then it does not blank out the row.
Hi Mike,
When you close the FormA, what about the data on the DataGridView, does the data updated. Yes without AcceptChanges();
Please point out which step data has not been updated after you close the FormA, so that we can narrow down which step is the probable root cause place.
If the DataGridView is updated, then use a button click event to execute a print method, to print out the DataTable data, then you can ensure if the DataTable is updated.
For the Table in Database, please go to your Sql Server and check the data. Checked the table and the data has not been updated.
I double click on Datagrid row to be updated - form opens to the correct record make changes - Click save button returns to Datagrid with the update looks good update is reflected on the datagrid - Close the app - Open the app and the data has not been changed.
Thanks
Frank
-
Friday, May 11, 2012 7:36 AMModeratorIf all the DataTable, DataGridView are updated(I have not seen that if you checked the DataTable is updated), and only the Database did not updated, then the only thing I can think out is the Adapter.Update method call.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
Friday, May 11, 2012 7:41 AM
Please keep in mind that i am new at C# can you please give me an example.
Thanks
Frank
-
Monday, May 14, 2012 10:56 AMModerator
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { LoadTable(); } BindingSource bs; SqlDataAdapter sqladapter; DataTable dataTable; SqlConnection connection; public void LoadTable() { connection = new SqlConnection("Data Source=.;Initial Catalog=Students;Integrated Security=True"); string selectCommandText = "select * from Table_1"; sqladapter = new SqlDataAdapter(selectCommandText, connection); dataTable = new DataTable(); sqladapter.Fill(dataTable); bs = new BindingSource(); bs.DataSource = dataTable; this.dataGridView1.DataSource = bs; } private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { new Form2(bs).ShowDialog(); sqladapter.UpdateCommand = new SqlCommand( "UPDATE Table_1 SET stuid = @stuid, stuname = @stuname " + "WHERE stuid = @oldstuid", connection); SqlParameter param0 = new SqlParameter("stuid", dataTable.Rows[bs.Position]["stuid"]); sqladapter.UpdateCommand.Parameters.Add(param0); SqlParameter param1 = new SqlParameter("stuname", dataTable.Rows[bs.Position]["stuname"]); sqladapter.UpdateCommand.Parameters.Add(param1); SqlParameter param2 = new SqlParameter("oldstuid", dataTable.Rows[bs.Position]["stuid"]); sqladapter.UpdateCommand.Parameters.Add(param2); sqladapter.Update(dataTable); } } public partial class Form2 : Form { public Form2() { InitializeComponent(); } BindingSource bs; public Form2(BindingSource bs):this() { this.bs = bs; this.textBox1.DataBindings.Add("Text", bs, "stuid"); this.textBox2.DataBindings.Add("Text", bs, "stuname"); } private void Form2_FormClosed(object sender, FormClosedEventArgs e) { bs.EndEdit(); } }If you have any more ADO.NET questions, please use the ADO.NET forum.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/threads
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Tuesday, May 15, 2012 9:09 AM
-
Monday, May 14, 2012 9:15 PM
Hi Mike,
Thanks for providing me information to look at.
Frank
-
Tuesday, May 15, 2012 9:08 AMModerator


