Mr_Green
Mr_Green

Reputation: 41822

new row doesnot add to the datagridview

I am a beginner in c#. I have a XML file (xmlfile.xml) which has some text in it.

On Form Load Event I am reading the XML file and showing it in datagridview1 of mainForm Form.

I am using below code to do this

DataSet ds = new DataSet();
ds.ReadXml(@"D:\xmlfile.xml");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dataGridView1.Tag = ds;   /* TAG */

I have another form noteForm in which I have textBox1 and btnSub button. Whenever the user clicks on btnSub button textBox1.Text should be added to the dataGridView1 of the mainForm

I am trying the below code to do this

            string strTitle;
            string[] row = new string[] { strTitle, DateTime.Now.ToString("M/d/y"), "checked" };
            _parent.dataGridView1.Tag.Tables[0].Rows.Add(row);
            _parent.dataGridView1.Rows.Add();
            _parent.dataGridView1.DataSource = _parent.dataGridView1.Tag.Tables[0].DefaultView; 

The above code is showing error -- "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

I cant understand what is this error. Please help.

_parent is just mainForm form in noteForm

Upvotes: 0

Views: 569

Answers (1)

L.B
L.B

Reputation: 116098

Add new row to your DataSet ds not to dataGridView1

ds.Tables[0].Rows.Add(row)

EDIT

string strTitle;
string[] row = new string[] { strTitle, DateTime.Now.ToString("M/d/y"), "checked" };
DataSet ds = (DataSet)_parent.dataGridView1.Tag;
ds.Tables[0].Rows.Add(row);

Upvotes: 2

Related Questions