Datagrid and updating
-
Hi all I have a table that contains four attributes (testVersion, qnum, answer, weight). Now I want to display this table in a datagrid using C#. The user then should be able to change the value of the weight attribute of any record and then that value should be updated in the table. I am using MS Access database. How can I code it easily? I hope that u understand my question. please help me I should finish the project very soooooooooooooooon. thanks
-
Hi all I have a table that contains four attributes (testVersion, qnum, answer, weight). Now I want to display this table in a datagrid using C#. The user then should be able to change the value of the weight attribute of any record and then that value should be updated in the table. I am using MS Access database. How can I code it easily? I hope that u understand my question. please help me I should finish the project very soooooooooooooooon. thanks
I think you have 2 ways to do waht you want : 1) make a button in the corner of the window whitch has the dataGrid that updates the database with the contnets of the grid or 2) catch every changed row in the dataGrid and update it in the database after the complete change I would recommand option 1 because when a mistake is made it can be undo. I hope you know something about asp.net in order to do what I wrote... ps: when a value is changed in a dataGrid, the dataTable that is the source is modified too. I hope you understand... By the way... visit http://nehe.gamedev.net[^]
-
I think you have 2 ways to do waht you want : 1) make a button in the corner of the window whitch has the dataGrid that updates the database with the contnets of the grid or 2) catch every changed row in the dataGrid and update it in the database after the complete change I would recommand option 1 because when a mistake is made it can be undo. I hope you know something about asp.net in order to do what I wrote... ps: when a value is changed in a dataGrid, the dataTable that is the source is modified too. I hope you understand... By the way... visit http://nehe.gamedev.net[^]
-
would u provide me the code here. I visited the site but did not find any thing related. it is better if someone provide me the C# code with the MS Access.
This is my code to display the table into the datagrid. private void btnShow_Click(object sender, System.EventArgs e) { try { //set SQL query oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM KeyAnswers"; //clear dataset dataSet1.Clear(); //Fill dataset with information oleDbDataAdapter1.Fill(dataSet1,"KeyAnswers"); //bind datagrid dataGrid1.SetDataBinding(dataSet1,"KeyAnswers"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { } } This is when I click the show button. Now I want to create a "Update" button . what should be the code? Should I update the whole table wheras only one record has been changed?!! waitting..... thanks
-
This is my code to display the table into the datagrid. private void btnShow_Click(object sender, System.EventArgs e) { try { //set SQL query oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM KeyAnswers"; //clear dataset dataSet1.Clear(); //Fill dataset with information oleDbDataAdapter1.Fill(dataSet1,"KeyAnswers"); //bind datagrid dataGrid1.SetDataBinding(dataSet1,"KeyAnswers"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { } } This is when I click the show button. Now I want to create a "Update" button . what should be the code? Should I update the whole table wheras only one record has been changed?!! waitting..... thanks
-
I think that when you click the show button the call of
SetDataBindingis
is not necesarry. And at the update you code should look like this :private void btnShow_Click(object sender, System.EventArgs e) { ... oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM KeyAnswers"; // This builds automaticly INSERT and UPDATE statement from the SELECT one // Use this once in the lifetime of the DataAdapter OleDbCommandBuilder cb = new OleDbCommandBuilder(oleDbDataAdapter1); ... } private void btnShow_Click(object sender, System.EventArgs e) { try { //Execute the command oleDbDataAdapter1.Update(dataSet1,"KeyAnswers"); MessageBox.Show("Changes saved successfully!"); } catch (Exception ex) { MessageBox.Show("There was an error while saving the changes : ex.ToString()); } finally { } }
PS: my code is not 100% correc in the way i haven't got an .net compiler right now, but that is the algorith you shoul use. I hope you understand... By the way... visit http://nehe.gamedev.net[^]