0

i try to add column in datatable :

dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool)));

and try to set value - false:

DataRow dr = dt.NewRow();
dr["BoolProperty"] = false;

But it does not work! Here is code:

try
{
    con.server = this.server;
    con.user = this.user;
    con.password = this.password;
    con.OpenConnection();
    con.SqlQuery(Properties.Resources.databaseCatalogResource);
    DataTable dt = con.QueryEx();
    con.da.Fill(dt);
    dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool)));
    DataRow dr = dt.NewRow();
    dr["BoolProperty"] = false;
    dataGrid.ItemsSource = dt.DefaultView;

}
catch (Exception ex)
{
    MessageBox.Show("Грешка във връзката.", "Грешка");
}

This is what happens : :)

but i need this column to be unchecked.

5
  • 1
    what exception is thrown? Commented Aug 17, 2017 at 7:30
  • Remove the try catch (or actually show the error in the catch) so you can see what's going wrong Commented Aug 17, 2017 at 7:31
  • Column in datagrid with unchecked chekbox. Commented Aug 17, 2017 at 7:32
  • 1
    Do you need to add this boolean values for all rows in the table or need to add a new(last) row with this boolean? Commented Aug 17, 2017 at 7:35
  • 1
    Visit this link dotnetfunda.com/codes/show/7432/… Commented Aug 17, 2017 at 7:36

5 Answers 5

4

create a boolean dataColumn with a default value false before filling data. (A default value is the value that is automatically assigned to the column when a DataRow is created)

DataTable dt = con.QueryEx();
var column = new DataColumn("BoolProperty", typeof(bool));
column.DefaultValue = false;
dt.Columns.Add(column);
con.da.Fill(dt);


lines

DataRow dr = dt.NewRow();
dr["BoolProperty"] = false;

are basically useless. dr is a new empty row, isn't added to a table and doesn't change flags in other rows

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool)));
for (int i=0;i<dt.Rows.Count;i++)
{
  dt.Rows[i]["BoolProperty"] = "Split";
}

Comments

1

So the DataTable dt is already filled with some values from the database using con.da.Fill(dt);, and you are adding a new column to this populated table. If you need to add value to this column means you have to do something like this:

// Your code
dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool)));
foreach(DataRow dr in dt.Rows)
{
    dr["BoolProperty"] = false;
}

Or simply include false as BoolProperty in your query that fills the datatable, Which will add a last column in the DataTable in all fetched rows with false

Comments

0

Most of the answers here loop through the rows to set the default value, but it can also be done inline by using the override of Columns.Add that takes an expression parameter

For example, to set all rows to boolean true:

dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool), "true"));

You can also use the expression parameter to set the column value based on other values in the row, which might look something like

dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool), "price > 50"));

Worth noting though, that adding an expression based column like this also makes the column read-only, as it is intended not to be altered based on (eg) user input.

Comments

0

Use:

DataTable.Columns.Add(new DataColumn("Column Name", typeof(bool), true.ToString())

DataColumn has a constructor overload as above. Here it accepts the third parameter as default value for that DataColumn. But the parameter is of string type. So make sure that the default value is typecasted to string.

P.S. I solved this same problem for me using above way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.