【发布时间】:2020-07-17 09:42:15
【问题描述】:
我有一个 DataGridView (DGV),我从 SQLite DB 手动添加项目
我的选择模式设置是 CellSelect 和 FullRowSelect 可能已经尝试过其他的
错误和问题因用户单击 DGV 的位置而异
现在的代码只有在选择标题 PID 下的空列时才会发生错误
And when the Column Header PID is selected See Posted Image
我已将错误作为 cmets 包含在代码中
我想解决的问题之一是禁用所有列上的所有排序
添加到 DGV 的数据是一个主键,它是一个整数 PID
还有一些文本是 NVARCHAR(2048) 这个文本来自 RichTextBox
要解决的两个最大问题是防止错误,是的,这些是问题
Private Sub PopulateDGV()
For Each header As DataGridViewHeaderCell In dgvOne.Rows
'header.SortMode = DataGridViewColumnHeaderCell.NotSortable
Next
'The code Above NOT Working
'ERRORS
'System.InvalidOperationException 'Column's SortMode cannot be set to Automatic
'While the DataGridView control's SelectionMode is set to ColumnHeaderSelect.'
'Your app has entered a break state, but there is no code to show because all
'threads were executing external code (typically system Or framework code).
'dgvOne = DataGridViewColumnSortMode.NotSortable
Dim str2 As String
Dim s1 As Integer
Dim dbName As String = "Word.db"
Dim conn As New SQLiteConnection("Data Source =" & dbName & ";Version=3;")
Dim valuesList As ArrayList = New ArrayList()
'Read from the database
Dim cmd As SQLiteCommand = New SQLiteCommand("Select * FROM ParentTable", conn)
conn.Open()
Dim rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
'Set Design of the DataGridView
dgvOne.DefaultCellStyle.Font = New Font("Tahoma", 10)
dgvOne.ColumnCount = 2
dgvOne.Columns(0).Width = 60
dgvOne.Columns(1).Width = 420
'Set Col Header Size Mode = Enabled
'Set Col Header Default Cell Styles DO in Properties
dgvOne.ColumnHeadersHeight = 34
'DGV Header Names
dgvOne.Columns(0).Name = "PID"
dgvOne.Columns(1).Name = "Entry Data"
'Read from DB Table add to DGV row
While rdr.Read()
valuesList.Add(rdr(1)).ToString()
lbOne.Items.Add(rdr(1)).ToString()
s1 = rdr(0).ToString
str2 = rdr(1)
dgvOne.Rows.Add(s1, str2)
End While
'Add Blank rows to DGV
For iA = 1 To 4
dgvOne.Rows.Add(" ")
Next
rdr.Close()
conn.Close()
End Sub
Private Sub dgvTwo_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvOne.CellMouseClick
If (e.RowIndex = -1) Then
MessageBox.Show("No Clicking Here")
Return
End If
If dgvOne.CurrentCell.Value Is Nothing Then
tbMessage.Text = "NO Data Here"
Return
ElseIf e.RowIndex >= 0 Then
tbMessage.Text = e.RowIndex
Dim str2 As String
Dim s2 As String
Dim row As DataGridViewRow = dgvOne.Rows(e.RowIndex)
s2 = row.Cells(0).Value.ToString
str2 = row.Cells(1).Value.ToString
strB = str2
rtbEnter.Text = strB
'ERRORS
'System.NullReferenceException
'HResult = 0x80004003
'Message = Object reference Not Set To an instance Of an Object.
'Source = TestSQL
'StackTrace:
'at TestSQL.frmThree.dgvTwo_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e) in C:\Users\Dwight\source\repos\TestSQL\TestSQL\frmThree.vb:line 117
End If
End Sub
【问题讨论】:
-
首先,我建议您了解列和单元格之间的区别,然后编辑您的问题以引用正确的问题。您正在使用“列”清除应该使用“单元格”的地方,这会混淆整个事情。
-
“要解决的两个最大问题”。 SO 问题需要解决一个特定问题,因此根据定义,您的问题是不可接受的。
标签: vb.net datagridview