wpf 在带有值的 DataTable 中添加 bool 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45729037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding bool column in DataTable with value
提问by Димитър Грудев
i try to add column in datatable :
我尝试在数据表中添加列:
dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool)));
and try to set value - false:
并尝试设置值 - 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("Грешка във връзката.", "Грешка");
}
but i need this column to be unchecked.
但我需要取消选中此列。
回答by ASh
create a boolean dataColumn with a default value falsebefore filling data. (A
default value is the value that is automatically assigned to the column when a DataRow is created)
false在填充数据之前创建一个带有默认值的布尔数据列。(默认值是创建 DataRow 时自动分配给列的值)
DataTable dt = con.QueryEx();
var column = new DataColumn("BoolProperty", typeof(bool));
column.DefaultValue = false;
dt.Columns.Add(column);
con.da.Fill(dt);
线
DataRow dr = dt.NewRow();
dr["BoolProperty"] = false;
are basically useless. dris a new empty row, isn't added to a table and doesn't change flags in other rows
基本没用。dr是一个新的空行,不会添加到表中,也不会更改其他行中的标志
回答by Ravikumar
Try this
尝试这个
dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool)));
for (int i=0;i<dt.Rows.Count;i++)
{
dt.Rows[i]["BoolProperty"] = "Split";
}
回答by sujith karivelil
So the DataTable dtis 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:
因此 DataTabledt已经使用 填充了来自数据库的一些值con.da.Fill(dt);,并且您正在向这个填充的表中添加一个新列。如果您需要为此列添加值意味着您必须执行以下操作:
// Your code
dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool)));
foreach(DataRow dr in dt.Rows)
{
dr["BoolProperty"] = false;
}
Or simply include false as BoolPropertyin your query that fills the datatable, Which will add a last column in the DataTable in all fetched rows with false
或者简单地包含false as BoolProperty在填充数据表的查询中,这将在所有获取的行中添加数据表中的最后一列 false
回答by PaulG
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.Addthat takes an expressionparameter
这里的大多数答案都会遍历行以设置默认值,但也可以通过使用Columns.Add带有表达式参数的覆盖来内联完成
For example, to set all rows to boolean true:
例如,要将所有行设置为布尔值 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.
但值得注意的是,添加这样的基于表达式的列也会使该列成为只读的,因为它不会根据(例如)用户输入进行更改。
回答by user3789482
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.
DataColumn 具有如上所述的构造函数重载。这里它接受第三个参数作为该 DataColumn 的默认值。但是参数是字符串类型的。因此,请确保将默认值类型转换为字符串。
P.S. I solved this same problem for me using above way.
PS我使用上述方式为我解决了同样的问题。


