如果旁邊的單元格的值為“1”,我正在嘗試以編程方式將 CheckBox 添加到 DataGridVew 單元格。我正在嘗試在添加行時執行此操作
我希望有人可以在這里幫助我使用正確的代碼。我知道其中一行代碼不正確,但我已將其放入以說明我正在嘗試做什么。
提前致謝。
Private Sub Controls_DGV_RowsAdded(sender As Object, e As Windows.Forms.DataGridViewRowsAddedEventArgs) Handles Controls_DGV.RowsAdded
If Controls_DGV.Rows(e.RowIndex).Cells(2).Value = "1" Then
Controls_DGV.Rows(e.RowIndex).Cells(1).AddCheckBox ' THIS LINE IS INCORRECT
End If
End Sub
uj5u.com熱心網友回復:
這是一樣的除了檢查值,在這種情況下@miguel
uj5u.com熱心網友回復:
您想要顯示復選框的列號 1 應該已經是 DataGridViewCheckBoxColumn 型別,然后如果值不是“1”,您可以為 DataGridViewTextBoxCell 轉換單元格的型別,因此沒有復選框,您甚至可以放在那里如果你想要一些文字。因為您使用的是 3 列,所以我會嘗試這樣做。
如果您以編程方式添加列,則在您的 Form1_Load() 中應該有這樣的內容:
Dim ChkBox As New DataGridViewCheckBoxColumn
Controls_DGV.Columns.Add("TextBox1", "TextBox1")
Controls_DGV.Columns.Add(ChkBox)
Controls_DGV.Columns.Add("TextBox2", "TextBox2")
然后使用你的代碼應該是這樣的:
Private Sub Controls_DGV_RowsAdded(sender As Object, e As Windows.Forms.DataGridViewRowsAddedEventArgs) Handles Controls_DGV.RowsAdded
If Controls_DGV.Rows(e.RowIndex).Cells(2).Value <> "1" Then
' replace the checkbox cell by textbox cell
Controls_DGV.Rows(e.RowIndex).Cells(1) = New DataGridViewTextBoxCell()
Controls_DGV.Rows(e.RowIndex).Cells(1).Value = "(empty or some text)"
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361780.html
