views:

398

answers:

6

Hello,

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.

A: 

Are you calling DataBind() after you update the DataTable?

Bryan
I think there is no DataBind() available for DataGridView.
NLV
Is this really an ASP.NET app? DataGridView is a Windows Forms object. Do you have this question tagged incorrectly?
Bryan
Sorry Bryan. You're right. My bad. Tagged it incorrectly.
NLV
A: 

hi, please try this, dvMoviesList.Refresh();

Rajagopalk
I've already tried this with no luck.
NLV
A: 

It seems like a previously asked question:
http://stackoverflow.com/questions/253843/simple-datagridview-refresh-question

Look at the answer given by Alan, maybe it is the solution you are searching for.

Regards
Thomas

Thomas
+2  A: 

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
*ANSWER*! - Sorry for failing to mark it before the bounty ended :(
NLV
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
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
Nevermind, you can't accept the answer after the bounty is done.
Ken
A: 

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
+1  A: 

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