3

I've a datagridview. I read an xml file and bind the data to the gridview. I change the xml and save it in another form. So i reread the xml file and i bind it to the gridview. The datatable is getting the updated values. But the grid is not getting updated till i close and open the application again. How to do it?

Thank you.

Regards, Raghavendra

#1

Is there a Databind() or Rebind() for DataGridView? I set the datasource like this -

dvMoviesList.DataSource = dtMovies;

If i add any new row to the dtMovies table and set the datasource again, it is getting reflected. But if i edit the values of any of the existing rows and re assign the datasource it is not getting reflected till i close and open the application again. Any ideas?

Thank you.

NLV
  • 21,141
  • 40
  • 118
  • 183

5 Answers5

9

I think you need to place a BindingSource in between the DataGridView and DataTable.

DataGridView _dgv = new DataGridView();
BindingSource _bs = new BindingSource();
DataTable _dt = new DataTable();
.
.
.
_dgv.DataSource = _bs;
_bs.DataSource = _dt;

Now whenever _dt gets updated, the BindingSource should take care of refreshing the DataGridView and you shouldn't have to worry about resetting any DataSource property. The DataSource properties can be set in the InitializeComponent() method (the designer), the Form's constructor, or Form.Load event handler.

Ken
  • 1,830
  • 3
  • 20
  • 32
  • *ANSWER*! - Sorry for failing to mark it before the bounty ended :( – NLV Feb 23 '10 at 04:54
  • Omg dude, that had 2 upvotes for a while which would have given me half the bounty automatically! And somebody took it away! I'm glad you found your answer....please mark it the accepted answer so I get an extra 15 at least. – Ken Feb 23 '10 at 15:23
  • I'm not able to mark it as an answer now. I'm not getting the tick mark. How to mark it as the answer? – NLV Feb 24 '10 at 14:10
  • Nevermind, you can't accept the answer after the bounty is done. – Ken Feb 24 '10 at 14:47
  • Is there a decent MSDN article regarding the recommendation to use a `BindingSource`? – Paul C Feb 04 '13 at 11:19
1

I am sure what I am about to suggest is not the right way but I ran into the same issue and being an ASP.NET developer mostly and not having done much work in winforms the only solution I found was to set the datasource to NULL then rebind the data.

Example: this.dataGridView.DataSource = null; this.dataGridView.DataSource = myDataSource;

Going to verify the solution recommened by Nasser as the solution I have just does not seem right even though it works.

Jim Scott
  • 2,493
  • 1
  • 18
  • 16
0

FYI: I've noticed a difference between setting the DataSource to a DataSet vs. a DataTable. Setting it to a DataSet does not cause the grid to refresh, even when it contains only one table. However, setting it to the DataTable inside the DataSet causes the grid to refresh correctly.

By the way, I was setting the DataSource property in an intermediate BindingSource object that was associated with the DataGridView, not in the DataGridView itself. Not sure if this matters.

This may only be tangentially related to the question, since the DataSource in the question was XML, not DataSet. However, others with my issue will probably bump into this topic (as I did), so I posted this anyway.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Joe
  • 1
0

Have you tried this?

dvMoviesList.Refresh();
TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
Rajagopalk
  • 31
  • 1
  • 1
  • 5
0

All you need is to Databind your datasource just like this

ArrayList list = new ArrayList();
list = YourXMLContent.GetItems(); 
GridView1.datasource = list;  // you can use you DataTable instead of list
GridView1.update();
Gridview1.invalidate();

its all.

Nasser Hadjloo
  • 12,312
  • 15
  • 69
  • 100