DataGridView复选框未显示 [英] DataGridView checkbox not showing
问题描述
VS2005 C#
我有一个带复选框列的绑定DataGridView.我想跟踪第二个隐藏列(一个int)中的值,因此如果第二个列具有值,则将复选框设置为true.
我已经在下面尝试了两个"for循环",但是该复选框不会显示为选中状态.
当我逐步使用调试器时,复选框的值将设置为true("Y"),但UI不会更新.进行.Refresh()无济于事.如果我第二次重新加载数据,则该复选框将正确显示.
谢谢,
悬崖
下面的一些代码:
VS2005 C#
I have a bound DataGridView with a checkbox column. I want to track the value in a 2nd hidden column (an int), so I set the checkbox to be true if the 2nd column has a value.
I''ve tried both ''for loops'' below, but the checkbox won''t display checked.
When I step through using the debugger, the checkbox value gets set to true ("Y"), but the UI won''t update. Doing a .Refresh() doesn''t help. If I do a 2nd reload of the data, the checkbox displays correctly.
Thanks,
Cliff
Some code below:
//Setup Code
const string TRUE_VALUE = "Y", FALSE_VALUE = "N";
grcCpaRCustomerCheckBox.TrueValue = TRUE_VALUE;
grcCpaRCustomerCheckBox.FalseValue = FALSE_VALUE;
//Loading data code
foreach (DataGridViewRow row in grvCpaResponsibility.Rows)
{
if (row.Cells[grcCustLocationId.Index].Value != null)
row.Cells[grcCpaRCustomerCheckBox.Index].Value = TRUE_VALUE;
else
row.Cells[grcCpaRCustomerCheckBox.Index].Value = FALSE_VALUE;
}
for (int i = 0; i < objCpa.CpaResponsibleParties.Count; i++)
{
if (objCpa.CpaResponsibleParties[i].CustLocationId.HasValue)
grvCpaResponsibility[grcCpaRCustomerCheckBox.Index, i].Value = TRUE_VALUE;
else
grvCpaResponsibility[grcCpaRCustomerCheckBox.Index, i].Value = FALSE_VALUE;
}
推荐答案
而不是使用常量字符串
Instead of using the constant string
const string TRUE_VALUE = "Y", FALSE_VALUE = "N";
尝试分配布尔值true和false.应该可以.
try to assign the boolean value true and false. It should work.
我已经修改了您的解决方案...
看看这个
I have modified your solution...
Check out this
//Setup Code
const string TRUE_VALUE = "Y", FALSE_VALUE = "N";
grcCpaRCustomerCheckBox.TrueValue = TRUE_VALUE;
grcCpaRCustomerCheckBox.FalseValue = FALSE_VALUE;
//Loading data code
foreach (DataGridViewRow row in grvCpaResponsibility.Rows)
{
if (row.Cells[grcCustLocationId.Index].Value != null)
Convert.ToBoolean(row.Cells[grcCpaRCustomerCheckBox.Index].Value) = true;
else
Convert.ToBoolean(row.Cells[grcCpaRCustomerCheckBox.Index].Value) = false;
}
for (int i = 0; i < objCpa.CpaResponsibleParties.Count; i++)
{
if (objCpa.CpaResponsibleParties[i].CustLocationId.HasValue)
Convert.ToBoolean(grvCpaResponsibility[grcCpaRCustomerCheckBox.Index, i].Value) = true;
else
Convert.ToBoolean(grvCpaResponsibility[grcCpaRCustomerCheckBox.Index, i].Value) = false;
}
以上操作无效.但是我确实找到了解决方案.离开并沉睡"一个问题会有所帮助.将TrueValue设置为Int可以保持我一直想要的int值.保存到&从数据库读取.
The above didn''t work. But I did find a solution. Stepping away and ''sleeping'' on a problem can help. Setting the TrueValue to Int holds the int Value which is what I wanted anyway. Saves to & reads from the DB.
//Setup Code
grcCpaRCustomerCheckBox.DataPropertyName = "CustLocationId";
//grcCustLocationId.DataPropertyName = "CustLocationId"; //end up not using this column in the grid
//grcCpaRCustomerCheckBox.TrueValue = int;//Had to do this in the IDE
grcCpaRCustomerCheckBox.FalseValue = null;
private void grvCpaResponsibility_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == grcCpaRCustomerCheckBox.Index)
{
if (grvCpaResponsibility[grcCpaRCustomerCheckBox.Index, e.RowIndex].Value == null)
grvCpaResponsibility[grcCpaRCustomerCheckBox.Index, e.RowIndex].Value = this.intCustLocationId;
else
grvCpaResponsibility[grcCpaRCustomerCheckBox.Index, e.RowIndex].Value = null;
}
}
这篇关于DataGridView复选框未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!