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

>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.

Leave a Reply