0

I have a visual basic program with a datagridview that is bound to a mysql database upon loading... I also have fields where you can update selected rows, add, delete, etc. and I'd like to have an auto-refresh upon updating... However, I cannot figure out how to do this as the table just adds the new rows underneath the old rows. Is there a way to have a complete table refresh for the mysql bound datagridview without having to re-launch the program?

Thanks in advance!

2 Answers 2

1

The grid is not bound to MySQL. It's bound to a DataTable. If you want a complete refresh of the data then simply clear that table and then repopulate it.

myDataTable.Rows.Clear()
3
  • If you've cleared the DataTable and that's bound to the grid then there are no current rows. I think you need to show us what you're actually doing rather than describing what you think you're doing. Sep 24, 2014 at 12:34
  • The datagridview is populated from a mysql query from the database, and when I tried that code, it errors out saying "Cannot clear this list." Sep 24, 2014 at 19:47
  • Are you sure that you cleared the DataTable and not the DataGridView? Someone else took it upon themselves to edit my answer and provide code that was incorrect. I deleted that code and replaced it with code that actually does as I described. You cannot remove rows from a bound grid; you must work with the underlying data source, i.e. the DataTable. There is no reason that you can't clear the DataTable, which is exactly what I said to do in the first place. Sep 24, 2014 at 22:41
0

As jmcilhinney suggested, datagridviews are bound to DataTables (or sometimes lists in my own case), so what he said sounds like it should work. Since, apparently, it didn't do as you wanted, here's my suggestion:

You said you have "fields" where you can update, add, delete, etc. a selected row, correct? It also sounds like you're doing something with new rows. As you've suggested that it's a databound gridview, I doubt that you're actually adding them in the typical way but instead have some sort of programmatic alternative.

So, try implementing your solution as less of a "rewrite" and more of a SQL update. When the user enters the data for the row that they wish to update or the new row they'd wish to create, search the rows for a given primary key.

In the case of a datatable, the code would look something like this:

string s = "primaryKeyValue";
DataRow foundRow = dataSet1.Tables["AnyTable"].Rows.Find(s);
foundRow.Item["DesiredColumn"] = newValue;

source: https://msdn.microsoft.com/en-us/library/y06xa2h1.aspx

In the case of a list bound as a DataSource, I believe you'd have to do something like this:

List<Thing> things = new List<Thing>(); // in case this wasn't obvious
Thing tempThing;
tempThing = things.Find(x => x.PartName.Contains("seat")));
tempThing.Value = newValue;

After you've modified the underlying datasource, then, theoretically, you should be able to simply call DataGridView.Refresh(), but you may have to do something trickier like set the datasource to null and then rebind it in order to force EndEdit.

I know this is late, but I hope it still helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.