C#以编程方式创建的datagridview-DataGridViewCheckBoxColumn cellValueChanged检查状态始终返回FALSE
在C#中,我无法使DataGridViewCheckBoxColumn事件工作。它总是得到一个错误的值,即使我点击了复选框。其他DataGridView(文本和组合框)列工作正常。这就是我正在做的 好的,因此我在运行时在构造函数中根据选项卡控件中的选项卡页数量动态创建DataGridView(DGV),这是由任何给定日期范围内的周数决定的,即每个选项卡页一个DGV(其中,您可以为每个周设置选项卡页) 活动本身如下:C#以编程方式创建的datagridview-DataGridViewCheckBoxColumn cellValueChanged检查状态始终返回FALSE,c#,winforms,datagridview,C#,Winforms,Datagridview,在C#中,我无法使DataGridViewCheckBoxColumn事件工作。它总是得到一个错误的值,即使我点击了复选框。其他DataGridView(文本和组合框)列工作正常。这就是我正在做的 好的,因此我在运行时在构造函数中根据选项卡控件中的选项卡页数量动态创建DataGridView(DGV),这是由任何给定日期范围内的周数决定的,即每个选项卡页一个DGV(其中,您可以为每个周设置选项卡页) 活动本身如下: foreach (Control thisControl in weeksTab
foreach (Control thisControl in weeksTabControl.Controls)
{
if (thisControl.GetType() == typeof(TabPage))
{
foreach (Control dgv in thisControl.Controls)
{
if (dgv.GetType() == typeof(DataGridView))
{
BuildWhiteboardDGV((DataGridView)dgv);
PopulateWhiteboardDGV((DataGridView)dgv);
wbDataGridView = (DataGridView)dgv;
wbDataGridView.CellMouseUp += new DataGridViewCellMouseEventHandler(wbDataGridView_CellMouseUp);
wbDataGridView.CellEndEdit += new DataGridViewCellEventHandler(wbDataGridView_CellEndEdit);
wbDataGridView.CurrentCellDirtyStateChanged += new EventHandler(wbDataGridView_CurrentCellDirtyStateChanged);
wbDataGridView.CellValueChanged += new DataGridViewCellEventHandler(wbDataGridView_CellValueChanged);
}
}
}
}
void wbDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (wbDataGridView.IsCurrentCellDirty)
{
wbDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
void wbDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex >= 13 && e.ColumnIndex <= 15)
{
System.Drawing.Point cur = new System.Drawing.Point(e.ColumnIndex, e.RowIndex);
DataGridViewCheckBoxCell curCell = (DataGridViewCheckBoxCell)wbDataGridView[cur.X, cur.Y];
if (curCell.Value != null && (bool)(curCell.Value) == true)
{
MessageBox.Show("TRUE");
}
else if (curCell.Value != null && (bool)(curCell.Value) == false)
{
MessageBox.Show("FALSE");
}
else
{
MessageBox.Show("NULL");
}
}
return;
}
catch (Exception ex )
{
MessageBox.Show("wbDataGridView_CellValueChanged() ERROR - " + ex.Message + " --> " + ex.InnerException.ToString());
return;
}
}
void wbDataGridView\u CurrentCellDirtyStateChanged(对象发送方,事件参数e)
{
if(wbDataGridView.IsCurrentCellDirty)
{
wbDataGridView.CommittedIt(DataGridViewDataErrorContexts.Commit);
}
}
void wbDataGridView\u CellValueChanged(对象发送方,DataGridViewCellEventArgs e)
{
尝试
{
如果(e.ColumnIndex>=13&&e.ColumnIndex好的话……我想我已经把它分类了
在我的CellMouseUp()活动中,我没有按左边的按钮
因此,通过现在添加它,CellValueChanged()事件可以正确工作并捕获正确的DataGridViewCheckCellColumn检查状态:选中时为TRUE,未选中时为FALSE
public void wbDataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left && e.RowIndex != -1) // added this and it now works
{
this.rowIndex = e.RowIndex;
this.colIndex = e.ColumnIndex;
this.wbDataGridView = (DataGridView)sender;
return;
}
if (e.Button == System.Windows.Forms.MouseButtons.Right && e.RowIndex != -1)
{
this.rowIndex = e.RowIndex;
this.colIndex = e.ColumnIndex;
this.wbDataGridView = (DataGridView)sender;
return;
}
}
谢谢你的评论,非常感谢
Jobs是一个很好的联合国!!我找不到任何让它对我不起作用的方法。当我将它们插入一个简单的DataGridView,其中只有一列作为复选框列时,你的方法wbDataGridView\u CurrentCellDirtyStateChanged
和wbDataGridView\u CellValueChanged
对我起作用。我在其余的部分没有看到任何其他内容您发布的代码中的一部分看起来会以任何方式影响它。
public void wbDataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left && e.RowIndex != -1) // added this and it now works
{
this.rowIndex = e.RowIndex;
this.colIndex = e.ColumnIndex;
this.wbDataGridView = (DataGridView)sender;
return;
}
if (e.Button == System.Windows.Forms.MouseButtons.Right && e.RowIndex != -1)
{
this.rowIndex = e.RowIndex;
this.colIndex = e.ColumnIndex;
this.wbDataGridView = (DataGridView)sender;
return;
}
}