2012-07-13 105 views
3

如何強制在Datagridview的列中僅檢查單個複選框?Datagridview只強制在列中選中一個複選框

+1

爲了與整個計算機軟件行業保持一致,請使用單選按鈕而不是複選框。列表複選框意味着您可以選擇多個項目,而單選按鈕意味着您只能爲該組選擇一個項目。可以解決您的問題,但我強烈建議您反對這個方向。如果您選擇複選框而不是單選按鈕,因爲您不允許添加值,請添加單選按鈕「無」或「清除」按鈕以選擇無選項。 – 2012-07-13 08:09:46

+0

@SteveB我知道我遲到這個評論,但我沒有看到Winforms Datagridview中的單選按鈕? – 2013-02-12 13:04:41

回答

3

您必須訂閱網格的CellValueChanged事件,並根據當前單元格的檢查狀態,循環DataGridView並將true/false設置爲其他單元格的值。

void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
{ 
     if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell) 
     { 
      if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value)) 
      { 
        // Maybe have a method which does the 
        //loop and set value except for the current cell 
      } 
     } 
} 
+0

一個客戶端解決方案(jQuery的可以幫助很多)是更加可取的,這將避免幾十回發 – 2012-07-13 08:19:45

+0

@SteveB檢查標記其** winform ** – V4Vendetta 2012-07-13 08:29:20

+1

@ V4Vendetta只是一個問題,爲什麼CellValueChanged而不是CellContentClick?據我所知,CellValueChanged事件是當任何單元格的內容改變時拋出,但CellContentClick事件拋出時,單元格中的某些內容已被點擊(IE:在這種情況下的複選框),所以不會使用CellContentClick事件比較好? – l46kok 2012-07-13 08:36:43

0

可以使用CellEndEdit事件DGV,因爲當細胞被修改後,會出現。請閱讀下面的代碼片段中的評論,以使用下面的代碼:

private void dgrvUserProfileView_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    int CheckedCount = 0; 

    //Make sure you have set True Value/ false Value property of check box column to 1/0 or true/false resp. 
    //Lets say your column 5th(namely Department) is a checked box column 
    if (dgrvUserProfileView.Columns[e.ColumnIndex].Name == "Department") 
    { 
     for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++) 
     { 
      if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true) 
      { 
       CheckedCount = CheckedCount + 1; 
      } 
     } 

     if (CheckedCount == 1) 
     { 
      for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++) 
      { 
       if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true) 
       { 
        dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = true; 
       } 
      } 
     } 
     else 
     { 
      for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++) 
      { 
       dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = false; 
      } 
     } 
    } 
} 

希望這有助於!

+0

或從這個答案採取參考http://stackoverflow.com/questions/10438613/datagridview-how-can-i-make-a-checkbox-act-as-a-radio-button – akhil 2012-07-13 08:12:13

0
private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     int currentcolumnclicked = e.ColumnIndex; 
     for (int i = 0; i <= dgvlist.Columns.Count - 1; i++) 
     { 
      if (dgvlist.Columns[i] is DataGridViewCheckBoxColumn) 
      { 
       if (Convert.ToString(dgvlist.CurrentRow.Cells[i].EditedFormattedValue) == "True" && i !=currentcolumnclicked) 
       { 
        dgvlist.CurrentRow.Cells[i].Value = false; 
       } 
      } 
     } 
    } 
2
private void dataGridViewProduit_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
    { 
     if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell) 
     { 
      if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value)) 
      { 
       foreach (DataGridViewRow row in (sender as DataGridView).Rows) 
       { 
        if (row.Index != (sender as DataGridView).CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true) 
        { 
         row.Cells[e.ColumnIndex].Value = false; 
        } 
       } 
      } 
     } 
    } 

    private void dataGridViewClient_CurrentCellDirtyStateChanged(object sender, EventArgs e) 
    { 
     if (this.dataGridViewClient.IsCurrentCellDirty) 
     { 
      dataGridViewClient.CommitEdit(DataGridViewDataErrorContexts.Commit); 
     } 
    } 
-2
private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if (e.ColumnIndex == 1) 
      { 
       try 
       { 
        string val = dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); 
        if (val == "False") 
         val = "True"; 
        else if (val == "True") 
         val = "False"; 
        dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = val; 

       for (int i = 0; i < dataGridView3.Rows.Count; i++) 
       { 
        string active = ""; 
        if (i != e.RowIndex) 
        { 
         if (val == "False") 
         { 
          dataGridView3.Rows[i].Cells[1].Value = "True"; 
          active = "Y"; 
         } 
         else if (val == "True") 
         { 
          dataGridView3.Rows[i].Cells[1].Value = "False"; 
          active = "N"; 
         } 
        } 
        else 
        { 
         if (val == "False") 
          active = "N"; 
         else 
          active = "Y"; 
        } 




       } 


      } 
      catch (Exception ex) 
      { } 
     } 
    } 
0
private void dgvCaixa_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell) 
      { 
       foreach (DataGridViewRow row in dgvCaixa.Rows) 
       { 
        if (row.Index != dgvCaixa.CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true) 
        { 
         row.Cells[e.ColumnIndex].Value = false; 
        } 
       } 
      } 
     } 
+0

你能解釋這是如何解決問題。 – 2014-02-15 12:33:43

4
private void grdRegClass_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (grdRegClass.Columns.IndexOf(grdRegClass.Columns["Status"]) == e.ColumnIndex) 
     { 
      int currentcolumnclicked = e.ColumnIndex; 
      int currentrowclicked = e.RowIndex; 
      foreach (DataGridViewRow dr in grdRegClass.Rows) 
      { 
       dr.Cells[currentcolumnclicked].Value = false; 
      } 
      grdRegClass.CurrentRow.Cells[currentrowclicked].Value = true; 
     } 
    } 
0

在vb.net:

Private Sub DataGridViewJobsList_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewJobsList.CellValueChanged 
    If TypeOf TryCast(sender, DataGridView).CurrentCell Is DataGridViewCheckBoxCell Then 
     Dim cell1 As DataGridViewCell 
     cell1 = TryCast(sender, DataGridView).CurrentCell 
     If cell1.Value = True Then 
      For Each row As DataGridViewRow In TryCast(sender, DataGridView).Rows 
       If row.Index <> cell1.RowIndex AndAlso row.Cells(e.ColumnIndex).Value = "1" Then 
        row.Cells(e.ColumnIndex).Value = False 
       End If 
      Next 
     End If 
    End If 
End Sub 

Private Sub DataGridViewJobsList_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridViewJobsList.CurrentCellDirtyStateChanged 
    If Me.DataGridViewJobsList.IsCurrentCellDirty Then 
     DataGridViewJobsList.CommitEdit(DataGridViewDataErrorContexts.Commit) 
    End If 
End Sub 
0

您可以將VirtualMode設置設置爲TRUE DGV只允許一個複選框。並且你可以設置VirtualMode設置爲DatagridView檢查多個。

我認爲這是解決方案最簡單的方法。