vb.net中的Datagridview [英] Datagridview in vb.net
本文介绍了vb.net中的Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我根据需要在运行时在datagrid中生成复选框类型列。
现在我想确定如果在特定行中检查单元格,则不能检查该行中的其他单元格,即我将其他单元格作为只读。
我试过这样做但面临问题。任何人都可以帮助我吗?
请告诉我哪里出错了。
这是我的代码。
Hello,
I am generating checkbox type columns in datagrid at runtime as per my needs.
Now I want to make sure that if a cell is checked in a particular row ,then no other cells in that row could be checked i.e. I am making other cells as readonly.
I've tried to do so but facing problem problem. Can anyone help me?
Please tell me where am I going wrong.
Here is my code.
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
row_index = e.RowIndex
Dim column_count As Int16 = DataGridView1.Columns.Count
Dim column_checked As Int16 = 0
Dim i As Int16
' to get which cell is checked
For i = 1 To column_count - 1
If (DataGridView1.Rows(row_index).Cells(i).Value = True) Then
column_checked = i
Exit For
Else
column_checked = 0
End If
Next
MsgBox("Row selected= " & row_index & vbCrLf & "Column checked=" & column_checked)
'***************************************************************************
If (column_checked = 0) Then
' if no cells are checked then make all the cells in that row writable
For i = 1 To column_count - 1
DataGridView1.Rows(row_index).Cells(i).ReadOnly = False
Next
Else
' to make other cells in that row readonly
For i = 1 To column_count - 1
If i = column_checked Then
Continue For
Else
DataGridView1.Rows(row_index).Cells(i).ReadOnly = True
End If
Next
End If
End Sub
推荐答案
使用此代码发送给你的
Use This Code insted of yours
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim column As New DataGridViewCheckBoxColumn()
If True Then
column.HeaderText = "Selected"
column.Name = "Selected"
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
column.FlatStyle = FlatStyle.Standard
column.ThreeState = False
column.CellTemplate = New DataGridViewCheckBoxCell()
'column.CellTemplate.Style.BackColor = Color.Red ;
End If
DataGridView1.Columns.Insert(0, column)
Dim dt As New DataTable()
dt.Columns.Add("Name")
dt.Columns.Add("Name1")
dt.Columns.Add("Name2")
Dim dr As DataRow
For j As Integer = 0 To 9
dr = dt.NewRow()
dr("Name") = "H" & j
dr("Name1") = "H" & j
dr("Name2") = "H" & j
dt.Rows.Add(dr)
Next
DataGridView1.DataSource = dt
End Sub
Dim row_index As Integer
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
row_index = e.RowIndex
Dim column_count As Int16 = DataGridView1.Columns.Count
Dim column_checked As Int16 = 0
Dim i As Int16
' to get which cell is checked
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value IsNot Nothing And row.Cells(0).Value = True Then
column_checked = 1
End If
Next
MsgBox("Row selected= " & row_index & vbCrLf & "Column checked=" & column_checked)
'***************************************************************************
If (column_checked = 0) Then
' if no cells are checked then make all the cells in that row writable
For i = 1 To column_count - 1
DataGridView1.Rows(row_index).Cells(i).ReadOnly = False
Next
Else
' to make other cells in that row readonly
For i = 1 To column_count - 1
'If i = column_checked Then
' Continue For
'Else
DataGridView1.Rows(row_index).Cells(i).ReadOnly = True
'End If
Next
End If
End Sub
Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
If DataGridView1.IsCurrentCellDirty Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
End Clas
这篇关于vb.net中的Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文