Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refreshing a DataGridView after using ds.Tables["Table1"].Rows.Add?

Status
Not open for further replies.

bind

IS-IT--Management
Dec 13, 2004
25
Greetings all,

So I have this issue I can't figure out and I'm hopeful one of you will be able to assist me with this problem.

I have a Windows form with a DataGridView and a "dataSet1" dataset.

The DataGridView is bound to the DataSet1 and in the DataSet1 properties under tables I added a table called "Table1" and under that table added 4 columns which are bound to my columns in my datagridview.

I am programatically adding a row of data to the dataset with the following:

dataSet1.Tables["Table1"].Rows.Add(0.Text, 1.Text,2.Text,3.Text);

I have a button that saves the dataset table into a XML file using:

dataSet1.WriteXml(Environment.CurrentDirectory + "\\points.xml",XmlWriteMode.WriteSchema);

When opening the xml file all of my data is indeed populating but the issue I'm having is after adding a row to the dataset, I can't get the datagridview to actually show the data i've added...

I've tried dataGridView1.Refresh(); with no luck, I've searched countless threads on this matter and have tried a few examples but still can't get the datagridview to populate the additions from the dataset.

Can someone please explain to me what I'm missing here?

Thanks a lot! ;)

 
I also forgot to add, I have a load button that will load the contents of the XML file to the datagridview using the following code:

XmlReader xmlFile = default(XmlReader);

xmlFile = XmlReader.Create(Environment.CurrentDirectory + "\\points.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
dataGridView1.DataSource = ds.Tables[0];

When using this functionality the datagridview populates all of the data from the XML properly!!! I just can't figure out what the heck the issue is that's preventing the datagridview from populating the information I've added to the dataset.

Thanks again all!
 
There may be a few issues here. First. When adding a row to a datatable the row could be in a "Dirty state". After adding changes are you calling dtTtable.AcceptChanges() (if memory serves)?

Age is a consequence of experience
 
Tried, dataSet1.AcceptChanges();

Nothing, still doesn't populate the changes in the datagridview.

Thanks for the info though.
 
what happens if you try to reset the datasource?

Code:
   dataSet1.AcceptChanges();
  dataGridView1.DataSource = ds.Tables[0];

Age is a consequence of experience
 
The dataGridView1.DataSource = ds.Tables[0]; line worked!

I don't understand though, why would you have to refresh the datasource after entering data, wouldn't it make sense that after binding a datagridview to a datasource, anything entered into the datasource after would automatically populate the datagrid?
 
It would and it should. If you add a new like this, does it update?

Code:
DataRow dr=this.dataSet1.Tables["Table1"].NewRow();
dr["column1"] = 0.Text;
dr["column2"] = 1.Text;
dr["column3"] = 2.Text;
dr["column4"] = 3.Text;
this.dataSet1.Tables["Table1"].Rows.Add(dr);

Age is a consequence of experience
 
hi.

been a while since i worked with winforms, but if i remember correctly, the grid control binds to a dataview (via the defaultView property) if it finds out it's binding to a table (if a dataset, picks the first table then to the default view).

So add your rows via this view.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top