2017-04-21 70 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; 
      } 
     } 

感謝您的意見。非常感激。

工作一個很好的聯合國!