Hallo Christina,
schau Dir mal das nachfolgende Beispiel an.
Den Code einfach in ein leeres Formmodul (Form1.vb)
kopieren und dann starten.
' /// Code in Formmodul (Form1.vb)
Public Class Form1
Private mDT As DataTable
Private mDV As DataView
Private WithEvents DGV As DataGridView
Private mFlag As Boolean
Private Sub Form1_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
DGV = New DataGridView
With DGV
.Dock = DockStyle.Fill
.DefaultCellStyle.Font = _
New Font("Arial", 12)
.ColumnHeadersDefaultCellStyle.Font = _
New Font("Arial", 8, FontStyle.Bold)
End With
Me.Controls.Add(DGV)
CreateData()
DGV.DataSource = mDV
Dim DGVCBcol As New DataGridViewCheckBoxColumn(False)
DGV.Columns.Add(DGVCBcol)
DGV.Columns(3).HeaderText = "Erfassung"
DGV.AutoResizeColumns()
For i As Integer = 0 To 11
DGV.CurrentCell = DGV.Rows(i).Cells(0)
Next
End Sub
Private Sub CreateData()
Dim i As Integer
Dim DR As DataRow
mDT = New DataTable
With mDT
.Columns.Add("ID", GetType(Integer))
.Columns(0).AllowDBNull = False
.Columns.Add("SText", GetType(String))
.Columns.Add("LText", GetType(String))
For i = 1 To 12
DR = .NewRow
DR.Item(0) = i
DR.Item(1) = MonthName(i, True)
DR.Item(2) = MonthName(i, False)
.Rows.Add(DR)
Next
.AcceptChanges()
End With
mDV = New DataView(mDT)
End Sub
Private Sub DGV_CellValidating _
(ByVal sender As Object, _
ByVal e As DataGridViewCellValidatingEventArgs _
) Handles DGV.CellValidating
If Not mFlag Then
If e.ColumnIndex = 2 Then
SetCheckBox(e.RowIndex)
End If
End If
End Sub
Private Sub DGV_RowEnter _
(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs _
) Handles DGV.RowEnter
If Not mFlag Then
SetCheckBox(e.RowIndex)
End If
End Sub
Private Sub SetCheckBox(ByVal RowIndex As Integer)
If DGV.Rows.Count > 0 Then
If DGV.Columns.Count > 3 Then
Dim Ccrit As DataGridViewTextBoxCell = _
DirectCast(DGV.Rows(RowIndex).Cells(2), _
DataGridViewTextBoxCell)
Dim Ccbox As DataGridViewCheckBoxCell = _
DirectCast(DGV.Rows(RowIndex).Cells(3), _
DataGridViewCheckBoxCell)
Dim DRV As DataRowView = mDV.Item(RowIndex)
Dim CritValue As String
If Ccrit.EditedFormattedValue.ToString.Length _
0 Then
CritValue = Ccrit.EditedFormattedValue.ToString
mFlag = True
If CritValue.EndsWith _
("r", StringComparison.OrdinalIgnoreCase) Then
DGV.Rows(RowIndex).ReadOnly = True
Ccbox.ThreeState = True
Ccbox.Value = CheckState.Indeterminate
Else
DGV.Rows(RowIndex).ReadOnly = False
Ccbox.ThreeState = False
Ccbox.Value = CheckState.Checked
End If
mFlag = False
End If
End If
End If
End Sub
End Class
' \\\ E N T E _________
Zeilen in denen der Text in Spalte LText mit "r" oder "R"
endet werden gesperrt und die Checkbox bekommt den
Wert CheckState.Indeterminate.
Ich verstehe zwar nicht so ganz, was Du damit bezweckst,
vermute aber mal, dass es das ist, wonach Du suchst.
Weitere Beispiele mit dem DataGridView findest Du
unter
www.gssg.de -> Visual Basic -> VB.net
Gruß aus St.Georgen
Peter Götz
www.gssg.de (mit VB-Tipps u. Beispielprogrammen)