Join 136,937 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,849 people online right now. Registration is fast and FREE... Join Now!
I have a DataGridview which I want to fill from my dataset when I click "View" on a ToolStripMenu. I have an "Update" button on the ToolStrip to update current rows and insert new rows, and I have a "Clear" button to clear the datagrid.
When I click "View" the grid loads fine. I then edit a row and click "Update". When I click "Clear" the grid clears. When I click "View" again the grid loads - minus my edit (the edit is not getting updated to the DB)
It updates the edit only if I first click in another row then click "Update",
ALL the rows are then altered to my edit and overwritten (not good at all!!)
csharp
//I'm not sure whether my "Clear" event here is correct - it clears //the dataset.table not just the dataGridView (not sure how to do it differently)
That will simply remove all the rows from your DataGridView. As far as updating, your DataSet is filled with DataTables (in this case it looks like just one), so try updating the DataTable not the DataSet
Im just taking stabs at this, Ive never worked with the grids using the drag & drop controls, Ive always did the binding and such at runtime not design time.
EDIT: I made a modification to the code I posted, I just throw the DataSet into the Update command. By the way what database are you using?
This post has been edited by PsychoCoder: 20 Mar, 2008 - 05:49 AM
No, setting the DataSource to null clears the gridView, but when I click on "View" again the DataSource is still null and so the gridView does not display anything. I think my original this.dsTracking.tracking.Clear(); is better for my uses here. (unless someone knows better )
As to the this.adapter.Update(this.dsTracking); it reacts with the exact same problem, ie:
It updates the gridView only if I first click in another row then click "Update",
Every row's value is then changed to my updated value, instead of just the one edited row
Here is an example of the ouptut in my gridView in order to try and explain better:
CODE
Customer: ---------------- George Jane Joe <---I edit this to "Allan" and then click Update
I've finally come back to this project (after been busy on a few others). I've tried creating this from scratch again but I hit the same weird Update behaviour. Anyone know why this is happening?
The reason why all your rows are updating in the database is because your Update statement doesn't include a WHERE statement. So instead of specifically telling the DB which row to update, you are saying update all rows.
You need to include a where statement that references the old value of the row you want to change.
Something along the following lines, where "?Customer" is the value of the row to change:
As for why you have to leave the row before it will update, I believe that is expected behavior. Same thing happens in SQL Server, you have to leave the row you are editing before it will update the DB with the changes. You just need to tab out of the row before hitting the update button.
I have a Form with a datagridview on it. With that i have a update and reload button. Everything works fine, Butt!! when i go to my server explorer and view the database, it go backs to normal. When I run the program after that, the datagridview is back to normal aswell..
Please help me out...
Here is my Code..
CODE
public partial class Form3 : Form {
private BindingSource bindingSource1 = new BindingSource(); private SqlDataAdapter dataAdapter = new SqlDataAdapter(); DataSet ds = new DataSet();
private void GetData(string selectCommand) { try { // Specify a connection string. Replace the given value with a // valid connection string for a Northwind SQL Server sample // database accessible to your system. String connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FS.mdf;Integrated Security=True;User Instance=True";
// Create a new data adapter based on the specified query. dataAdapter = new SqlDataAdapter(selectCommand, connectionString); dataAdapter.Fill(ds, "Client"); // Create a command builder to generate SQL update, insert, and // delete commands based on selectCommand. These are used to // update the database. SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
dataAdapter.Fill(ds); // Populate a new data table and bind it to the BindingSource. DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; dataAdapter.Fill(table); bindingSource1.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content. dataGridView1.AutoResizeColumns( DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); } catch (SqlException) { MessageBox.Show("To run this example, replace the value of the " + "connectionString variable with a connection string that is " + "valid for your system."); } }