vb.net 如何将复选框添加到绑定到数据源的数据网格视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29494460/
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
how to add a checkbox to a datagridview that is bound to a datasource?
提问by Alfred Waligo
I am new to c#. I need to have a checkbox field in the "selected" column of my datagridview instead of the "False"(or "True") text that is showing currently. This datagridview is databound and the data is obtained by reading an xml file. How can I achieve this?
我是 C# 的新手。我需要在 datagridview 的“selected”列中有一个复选框字段,而不是当前显示的“False”(或“True”)文本。这个datagridview是数据绑定的,数据是通过读取一个xml文件得到的。我怎样才能做到这一点?
before writing to the xml file, this is what i did.
在写入 xml 文件之前,这就是我所做的。
DataTable dtGens = new DataTable(); //creates a new Datatable object for the Gens
dtGens.TableName = "Gen Types";
DataColumn dc1 = new DataColumn("Generator");
DataColumn dc2 = new DataColumn("alpha");
DataColumn dc3 = new DataColumn("beta");
DataColumn dc4 = new DataColumn("circuit breaker");
DataColumn dc5 = new DataColumn("description");
DataColumn dc6 = new DataColumn("Selected",System.Type.GetType("System.Boolean"));
dtGens.Columns.Add(dc1); //associates the columns to the dtGens datatable
dtGens.Columns.Add(dc2);
dtGens.Columns.Add(dc3);
dtGens.Columns.Add(dc4);
dtGens.Columns.Add(dc5);
dtGens.Columns.Add(dc6);
DataRow drow;
for (int i = 0; i < 50; i++)
{
drow = dtGens.NewRow();
drow["Generator"] = "Gen " + (i + 1).ToString();
drow["alpha"] = 0.0;
drow["beta"] = 0.0;
drow["circuit breaker"] = 0.0;
drow["description"] = "myGen";
drow["Selected"] = false;
dtGens.Rows.Add(drow);
}
//creates a new DataSet Object that will help write generator data to XML
DataSet feederProject = new DataSet();
feederProject.Tables.Add(dtGens);
feederProject.WriteXml("Generators.xml");
//preview
DataSet feederProject = new DataSet();
feederProject.ReadXml("Generators.xml");
dataGridViewLoadsDGs.DataSource = feederProject.Tables[0];
回答by Angel.Chorbadzhiev
To be able to represent a bound boolean data as a check box column you need to set AutoGenerateColumns property of the DataGridView to false. Then add columns manually and for the column that must be checkbox column set a instance of DataGridViewCheckBoxColumn:
为了能够将绑定的布尔数据表示为复选框列,您需要将 DataGridView 的 AutoGenerateColumns 属性设置为 false。然后手动添加列,并为必须是复选框列的列设置一个 DataGridViewCheckBoxColumn 实例:
dataGridViewLoadsDGs.AutoGenerateColumns = false;
...
dataGridViewLoadsDGs.Columns.Add(new DataGridViewCheckBoxColumn());
回答by Ripple
A column bound to boolean type automatically shows checkboxes. The problem is that the schema information of your DataTable is lost when it's written into the XML.
To prevent that, you can use an overload of DataSet.WriteXmlthat takes XmlWriteModeas a parameter, which allows you an option to write the schema information.
绑定到布尔类型的列会自动显示复选框。问题是你的 DataTable 的架构信息在写入 XML 时丢失了。为了防止这种情况,你可以使用的过载DataSet.WriteXml这需要XmlWriteMode作为一个参数,它允许你写的架构信息的选项。
feederProject.WriteXml("Generators.xml", XmlWriteMode.WriteSchema);

