Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add more details to a datagrid View .(add more data rows). there are 3 columns of the dataGrid View.Data are not support from a data base source. I get the data from a xml file. I'm trying to do this by using following code.


C#
DataTable dt = new DataTable();


 public void SyncWIthConfigFile()
        {
 initialXmlDoc();
            XmlElement rootElement = xmlCommand.DocumentElement;
            UFCCommand command = new UFCCommand();
            foreach (XmlNode childNode in rootElement.ChildNodes)
            {
                List<ufccommand> commandList = new List<ufccommand>();
                DataRow dr = dt.NewRow();
             
                if (childNode.Name != "_ERD" && childNode.Name!="_DLY")
                {
                    
                    command.Name = childNode.Name;
                    dr["ColName"] = command.Name.ToString();
                    
                     if (childNode.FirstChild.Name == "SET_RESPONSE")
                    {  
                        command.SetValue = childNode.FirstChild.InnerText;
                        dr["ColSetValue"] = command.SetValue.ToString();
                       
                    }
                     if(childNode.LastChild.Name=="GET_RESPONSE")
                    {  
                        command.GetValue = childNode.LastChild.InnerText;
                        dr["ColGetValue"] = command.GetValue.ToString();
                    
                    }
                    dataGridView1.DataSource = dt;
                    
                     }
}
}</ufccommand></ufccommand>


When run this code there was an exception.
"Column 'ColName' does not belong to table ." But my first colunm name is"ColName". I can't understand the wrong.
Posted
Updated 12-Sep-11 17:49pm
v2
Comments
Prerak Patel 12-Sep-11 23:50pm    
Unchecked 'Treat my content as plain text, not as HTML'
Jeremy Shin Woo 12-Sep-11 23:53pm    
ok..thanks for the advise :)
Prerak Patel 12-Sep-11 23:53pm    
Check what it shows as dt.Columns[0].ColumnName?
Jeremy Shin Woo 13-Sep-11 0:00am    
it doesn't work properly. thanx
manoharank5 12-Sep-11 23:56pm    
Check the dt.Columns.Count, if you couldn't find any columns add it dt.Columns.Add

1 solution

I didn't find where you are adding columns to the DataTable.
Try to Add columns to DataTable and try again it will work.
For Example:

DataTable table = new DataTable();
table.Columns.Add("ColName", typeof(string));
table.Columns.Add("ColSetValue", typeof(string));

etc
 
Share this answer
 

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



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