2017-04-21 73 views
0


你好那里
任何人都可以帮助我解决这个C#问题。我正在把这件事撕掉。我会提供尽可能多的信息。我无法使DataGridViewCheckBoxColumn事件正常工作。尽管我点击了复选框,它总是会得到一个FALSE值。其他datagridviews(文本和组合框)列工作正常。这是我在做什么......

好的,所以我在运行时动态地在我的构造函数中创建datagridviews(DGVs),它基于标签控件中有多少标签页,这些标签页由任意几星期指定日期范围内即每个标签页面(即每个星期标签页)C#编程创建datagridview - DataGridViewCheckBoxColumn cellValueChanged检查状态总是返回FALSE

for (int i = 0; i < wcNumWeeks; i++) 
{ 
    foreach (DataRow dr in wbDatesDT.Rows) 
    { 
     if (Convert.ToInt16(dr["tabNo"].ToString()) == i + 1) 
     { 
     wcDate = Convert.ToDateTime(dr["wcDate"].ToString()); 
     break; 
     } 
    } 
    weeksTabControl.TabPages.Add(wcDate.ToShortDateString()); 
    weeksTabControl.TabPages[i].AutoScroll = true; 
    weeksTabControl.TabPages[i].Width = 1500; 
    weeksTabControl.TabPages[i].Height = 700; 
    weeksTabControl.TabPages[i].Controls.Add(new DataGridView() 
    { 
     Name = "dataGridView" + (i + 1).ToString(), 
     Dock = DockStyle.Fill, 
     Width = 1450, 
     Height = 650, 
     Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right), 
     ScrollBars = System.Windows.Forms.ScrollBars.Both, 
     AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells 
    }); 
} 

再在构造,每个datagridview的创建,我创建事件,如下一个DGV:

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); 

     } 
     } 
    } 
} 

012是个 事件本身如下:

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; 
    } 

} 


谁能告诉我在哪里,我错了,并指出我在正确的方向。

提前感谢

阿布斯

+1

我无法找到任何使它不适用于我的东西。您的方法'wbDataGridView_CurrentCellDirtyStateChanged'和'wbDataGridView_CellValueChanged'为我工作时,我插入一个简单的DataGridView只有一列作为复选框列。我在其他发布的代码中看不到任何其他内容,看起来会以任何方式影响它。 –

回答

0

OK ....我想我已经整理了。

在我的CellMouseUp()事件中,我没有为LEFT按钮提供服务。

因此,现在添加,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; 
      } 
     } 

感谢您的意见。非常感激。

工作一个很好的联合国!