Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to add rows to a datagridview which contains 4 columns and the rows are added at run-time and also added to the XML file .The columns are as follows :
HOSTNAME IPADDRESS USERNAME PASSWORD

Whenever i am loading the form at run-time with some value in the xml file, it is getting populated .
But when i am trying to add data again by clicking on ADD button, it shows an "INVALIDOPERATOREXCEPTION- Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

My code while form loading
C#
if (File.Exists(@"D:\HostAdded.xml") && new FileInfo(@"D:\HostAdded.xml").Length != 0)
                {
                    dataGridView1.DefaultCellStyle.ForeColor = Color.Red;
                    DataSet dataSet = new DataSet(); 
                    //DataView teamView = new DataView();
                    dataSet.ReadXml(@"D:\HostAdded.xml");
                    //teamView = dataSet.Tables[0].DefaultView;
                    //dataGridView1.DataSource = dataSet.Tables["Table1"];
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = dataSet;
                    dataGridView1.DataMember = "Table1";
                    this.Cursor = Cursors.Default;
                }


XML file

XML
<NewDataSet>
  <Table1>
    <Host-Name>ixin-varsha</Host-Name>
    <Ip_Address>10.205.26.42</Ip_Address>
    <User_Name>wefwe</User_Name>
    <Password>wwqefwe</Password>
  </Table1>
</NewDataSet>


Adding client at runtime by clicking on add button
C#
if (File.Exists(@"D:\HostAdded.xml"))
{
     List<string> Items = GetItemsFromXmlUsingTagNames(@"D:\HostAdded.xml", "Host-Name");
     if (!Items.Contains(txthostname.Text))
     {
         int returnValue = CreateConnection(txthostname.Text);
         if (returnValue == 1)
         {
             ipAddr = DoGetHostAddresses(txthostname.Text);
             dataGridView1.DefaultCellStyle.ForeColor = Color.Red;
             dataGridView1.Rows.Add(txthostname.Text, ipAddr, txtusername.Text, txtpassword.Text);//Error occurs here
             txtLog.AppendText("\nRow added successfully");
             createXML();
             txthostname.Text = "";
             txtusername.Text = "";
             txtpassword.Text = "";
             populateComboBox();
          }
       }
}

private void createXML(string filename)
        {
            DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dgvIsland.Columns)
            {
                dt.Columns.Add(col.HeaderText);
            }
            foreach (DataGridViewRow row in dgvIsland.Rows)
            {
                DataRow dRow = dt.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                dt.Rows.Add(dRow);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.WriteXml(filename);
        }



Please help me to identify the error!!
Thyanks a lot!! :)
Posted

1 solution

do as below
C#
DataSet dataSet = new DataSet(); 
dataSet.ReadXml(@"D:\HostAdded.xml");
DataRow row = dataSet.Tables[0].NewRow();
row.ItemArray = new object[] { txthostname.Text, ipAddr, txtusername.Text, txtpassword.Text };
dataSet.Tables[0].Rows.Add(row);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dataSet;
dataGridView1.DataMember = "Table1";
 
Share this answer
 
v2
Comments
sovit agarwal 20-May-14 5:15am    
Thanx Once a again mate!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900