2012-11-21 42 views
3

enter image description here添加單選按鈕到Windows窗體DataGrid控件

我有我的DataGrid Windows窗體上,我在運行時添加一行到數據網格。我有一個DataGrid的列,我需要在運行時添加不同的UI控件(列的每個單元格將包含不同的UI控件,如下拉列表,複選框,超鏈接,單選按鈕)。我可以添加除單選按鈕控件之外的其他控件,如何將單選按鈕添加到DataGrid列? 我已經使用this它不適合我,因爲它需要整列作爲單選按鈕列。

+0

什麼使用它的目的?使用Radiobutton時,只有一些選項可供選擇。如果你只希望每行使用它,你應該使用複選框 – MVCKarl

+0

如果只有一個選項可供選擇,我會建議使用複選框。 – Rauld

+0

我已經通過添加圖片更新了問題,這就是我在網格中需要的。 – nirav

回答

0

在這裏,如何使複選框的列作爲RadioButtons工作。從你的圖片你需要一個所有的列作爲RadioBUttons - >這是更復雜,並取決於你的綁定數據datagridview。我的工作多與VB.NET所以這裏可能有些語法錯誤...

在你的datagridview 創建DataGridViewCheckBoxColumn柱DataGridView中的三個事件我複選框列的工作作爲一個單選按鈕

private Boolean bRbtnCurrentValue 
private Int32 iColumnRadioBtn = 4 
private Datagridview dgv //Only for this exapmle 

//In Event dgv_CellBeginEdit 
{ 
    //Here we stored current value to variable 
    if (this.dgv.CurrentCell.ColumnIndex = this.iColumnRadioBtn) 
     this.bRbtnCurrentValue = this.dgv 
} 

//In Event dgv_CellEndEdit 
{ 
    //Here we update if value changed 
    Boolean bNewValue = this.dgv.CurrentCell.Value 
    if (this.dgv.CurrentCell.ColumnIndex = this.iColumnRadioBtn) 
    { 
     if(bNewValue=False) 
      this.dgv.CurrentCell.Value=this.bRbtnCurrentValue 
     else 
      //Here jo actions when value changed(database update etc.) 
    } 
} 

//Event dgv_ CurrentCellDirtyStateChanged 
{ 
    if(this.dgv.CurrentCell.ColumnIndex = this.iColumnRadioBtn) andalso (dgv.IsCurrentCellDirty = True) 
    { 
     foreach(DataGridViewRow dgvr In dgv.Rows) 
     { 
      If (dgvr.Index = dgv.CurrentRow.Index) 
       If (dgv.CurrentCell.Value = True) 
        dgv.CancelEdit() //True to False cannot be changed 
      else 
       If (dgvr.Cells(dgv.CurrentCell.ColumnIndex).Value=True) 
        dgvr.Cells(dgv.CurrentCell.ColumnIndex).Value = False 
     } 
    } 
} 
+0

正如你告訴我應該ceate DataGridViewCheckBoxColumn,但在我的應用程序列中還可以包含cotrols像組合,文本框和超鏈接。所以我不能添加DataGridViewCheckBoxColumn,我想添加不同的控制列的每個單元格。 – nirav

相關問題