Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Datatable with more than 1 type of data on a column c# winform

I`m trying to dynamically generate a table based on a treeview node i click. I want it to look like this [2 rows of a table where on the 2nd row i can use either bool or int][1]

if (ModelIerarhic.SelectedNode.Text == "1")
{
  DataTable dtdiag = new DataTable();
  dtdiag.Columns.Add("Config Options", typeof(string));
  dtdiag.Columns.Add(e.Node.Parent.Name, typeof(bool | string)); //this is where i need to change so the below lines will work
  dtdiag.Rows.Add(new object[] { "a", "abc" });
  dtdiag.Rows.Add(new object[] { "a", true });
  dataGridView1.DataSource = dtdiag;
}

from what i read i cannot change the datatable column type after i put one value in a row.
[1]: https://i.stack.imgur.com/Z1uFo.png

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

All cells of a column need to have the same data type. You can declare the column to be of type object. This is the base type that all .NET types share:

dtdiag.Columns.Add(e.Node.Parent.Name, typeof(object));

Be aware, that this is a very loose declaration; you can put any data, not only strings and boolean values into this column.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading