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
if (File.Exists(@"D:\HostAdded.xml") && new FileInfo(@"D:\HostAdded.xml").Length != 0)
{
dataGridView1.DefaultCellStyle.ForeColor = Color.Red;
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"D:\HostAdded.xml");
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dataSet;
dataGridView1.DataMember = "Table1";
this.Cursor = Cursors.Default;
}
XML file
<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
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);
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!! :)