开发者

Update datagrid view with updated data

开发者 https://www.devze.com 2022-12-16 03:40 出处:网络
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 getti

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 开发者_JAVA技巧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.


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.


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.


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.


Have you tried this?

dvMoviesList.Refresh();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消