C# 将DataGridView复选框列用于字符串数据列
我在数据库中有这个表:C# 将DataGridView复选框列用于字符串数据列,c#,winforms,checkbox,datagridview,datagridviewcheckboxcell,C#,Winforms,Checkbox,Datagridview,Datagridviewcheckboxcell,我在数据库中有这个表: applicant | module | date | approvation | xxxx xxxx xxxx xxxxxxx yyyy yyyy yyyy yyyyyyy tttt tttt tttt ttttttt 我有这张桌子。查询后我分配 DataTable到我的DataGridView.DataSource: Q
applicant | module | date | approvation |
xxxx xxxx xxxx xxxxxxx
yyyy yyyy yyyy yyyyyyy
tttt tttt tttt ttttttt
我有这张桌子。查询后我分配
DataTable
到我的DataGridView.DataSource
:
QueryAssist qa = new QueryAssist();
DataTable dt = new DataTable();
dt = qa.runQuery('myquery');
dgvApprovazione.DataSource = dt;
dgvApprovazione.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
// modify, transform 2nd column in cellLink
foreach (DataGridViewRow row in dgvApprovazione.Rows)
{
row.Cells[1] = new DataGridViewLinkCell();
}
现在我想转换列approvation
,它是字符串approved
或notapproved
,并显示一个复选框
如果此单元格的值为已批准
复选框已选中且未修改(仅读取)
类似的东西:
foreach (DataGridViewRow row in dgvApprovazione.Rows)
{
if (row.Cells[3].Value.ToString().Equals("APPROVED"))
{
row.Cells[3] = new DataGridViewCheckBoxCell();
}
}
我有一个问题要实施。。。帮帮我
有可能吗?怎么做
重述:
我想在复选框(选中或未选中)中更改值包含为文本/字符串(已批准或未批准)的列
对不起,英语不好
好的替代方法?要在数据库中存储yes/no、on/off或true/false值,最好在sql server中使用bit
数据类型,但使用,您还可以通过设置和属性来显示和编辑字符串列
在下面的示例中,我假设数据库中有一个可空列,需要将值存储为APPROVED
或NOTAPPROVED
为nvarchar(50)
,并且需要使用DataGridViewCheckBoxColumn
编辑这些值
为此,应使用设计器或代码以这种方式添加列:
var column1 = new DataGridViewCheckBoxColumn();
column1.Name = "column1"; //Name of column
column1.HeaderText= "Is Approved"; //Title of column
column1.DataPropertyName = "approvation"; //Name of field in database
column1.TrueValue = "APPROVED"; //True value
column1.FalseValue = "NOTAPPROVED"; //False Value
this.dataGridView1.Columns.Add(column1);
通过这种方式,您可以将已批准或未批准显示为复选框,还可以使用复选框编辑这些值
如果您不需要编辑它们,只想显示它们,可以将列的只读属性设置为true。在数据库表中将批准字段的类型更改为布尔值即可。开始时,我的想法是滑动(foreach)整个列的“已批准”或“未批准”(字符串)。。。然后创建一个新列(复选框列)添加到datagridview,如果值为“已批准”。。。选中复选框,否则不选中复选框。现在才显示datagridview。我已经解决了在db中声明是/否字段的问题:)。。但u的解决方案是好的。最好像回答中所说的那样使用位域。但对于用户需要使用复选框编辑(显示)字符串列的情况,该解决方案可能很有用:)