在控件中使用到判断单元格是否越界。但没有用到。因为使用了另外一种初始化网格。有两种初始化网格的方法:1.根据行列数和行标题,生成一个空白网格。有标题,里面数据全是空白字符" "。2.根据表DataTable,生成相应的网格。
下面的这一段代码应该用到方法1中。
#Region "验证单元格是否存在的函数." Private Function IsValidRowAndColumn(ByVal Row As Integer,ByVal Column As Integer) As Boolean If Column < 0 OrElse Row < 0 Then MessageBox.Show(Me,"网格行列数不可以为负","网格错误") Return False ElseIf Row > Me.Lv.Items.Count Then MessageBox.Show(Me,"行数越界:" & Row.ToString,"网格错误") Return False ElseIf Column > Me.Lv.Columns.Count Then MessageBox.Show(Me,"列数越界:" & Column.ToString(),"网格错误") Return False Else Return True End If End Function Private Function IsValidRow(ByVal Row As Integer) As Boolean If Row < 0 Then MessageBox.Show(Me,"网格行数不可以为负","网格错误") Return False Else Return True End If End Function Private Function IsValidColumn(ByVal Column As Integer) As Boolean If Column < 0 Then MessageBox.Show(Me,"网格列数不可以为负","网格错误") Return False Else Return True End If End Function #End Region #Region "获取或设置单元格的值" Public Sub SetCellValue(ByVal Row As Integer,ByVal Column As Integer,ByVal ItemValue As String) If IsValidRowAndColumn(Row,Column) Then If Column = 0 Then Me.Lv.Items(Row).Text = ItemValue Else Me.Lv.Items(Row).SubItems(Column).Text = ItemValue End If End If End Sub Public Function GetCellValue(ByVal Row As Integer,ByVal Column As Integer) As String If IsValidRowAndColumn(Row,Column) Then If Column = 0 Then Return Me.Lv.Items(Row).Text Else Return Me.Lv.Items(Row).SubItems(Column).Text End If Else Return Nothing End If End Function #End Region
自己写的初始化网格窗体:
Public Delegate Sub AfterRowSelectEventHandler(ByVal sender As Object,ByVal e As SelectEventArgs) Public Class FrmPopLv Public Event AfterRowSelectEvent As AfterRowSelectEventHandler Private mCols,mRows As Integer Private DataTB As DataTable Private _ColumnsNameArray As String() Private _SelectedRow As DataRow Private _SelectedIndex As Integer = -1 Property Table() As DataTable Get Return DataTB End Get Set(ByVal value As DataTable) DataTB = value InitializeGridProperties(DataTB) End Set End Property Public Property ColumnsNameArray() As String() Get If _ColumnsNameArray Is Nothing Then Return New String() {} Else Return _ColumnsNameArray End If End Get Set(ByVal value As String()) _ColumnsNameArray = value End Set End Property Public Sub New() InitializeComponent() End Sub Public Sub New(ByVal tb As DataTable) InitializeComponent() InitializeGridProperties(tb) Me.Table = tb End Sub Public Sub New(ByVal tb As DataTable,ByVal columnsNameArray As String()) InitializeComponent() If columnsNameArray IsNot Nothing Then InitializeGridProperties(tb,columnsNameArray) Else InitializeGridProperties(tb) End If Me.Table = tb End Sub Private Sub InitializeGridProperties(ByVal tb As DataTable) If tb.Columns.Count > 0 Then Dim ColumnsName(tb.Columns.Count - 1) As String For i As Integer = 0 To tb.Columns.Count - 1 ColumnsName(i) = tb.Columns(i).Caption Next InitializeGridProperties(tb,ColumnsName) End If End Sub Private Sub InitializeGridProperties(ByVal tb As DataTable,ByVal columnsNameArray As String()) Me.Lv.Items.Clear() Me.Lv.Columns.Clear() Dim i As Integer Dim w As Integer = 50 mCols = tb.Columns.Count mRows = tb.Rows.Count If columnsNameArray.Length <> mCols Then MsgBox("自定义列字段与表的列数不对称。") Exit Sub End If '添加标题 For i = 0 To mCols - 1 Me.Lv.Columns.Add(columnsNameArray(i)) Next '添加数据 For i = 0 To mRows - 1 Dim item As New ListViewItem(tb.Rows(i)(0).ToString) For j As Integer = 1 To mCols - 1 item.SubItems.Add(tb.Rows(i)(j).ToString) Next Me.Lv.Items.Add(item) Next '改标题宽度 For i = 0 To mCols - 1 '这一句不可以放到前面。不然第一列列宽变大。 Me.Lv.Columns(i).AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize) Next End Sub Private Sub FrmPopLv_Deactivate(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Deactivate Me.Close() End Sub Private Sub FrmPopLv_KeyDown(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyData = Keys.Escape Then Me.Close() End If End Sub Private Sub FrmPopLv_Leave(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Leave Me.Close() End Sub Private Sub Lv_DoubleClick(ByVal sender As Object,ByVal e As System.EventArgs) Handles Lv.DoubleClick If Me.Lv.SelectedItems.Count > 0 Then _SelectedIndex = Me.Lv.SelectedIndices(0) _SelectedRow = Me.Table.Rows(_SelectedIndex) Dim se As New SelectEventArgs(_SelectedIndex,_SelectedRow) RaiseEvent AfterRowSelectEvent(Me,se) Me.Close() End If End Sub Private Sub Lv_KeyDown(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles Lv.KeyDown If e.KeyData = Keys.Enter Then If Me.Lv.SelectedItems.Count > 0 Then _SelectedIndex = Me.Lv.SelectedIndices(0) _SelectedRow = Me.Table.Rows(_SelectedIndex) Dim se As New SelectEventArgs(_SelectedIndex,_SelectedRow) RaiseEvent AfterRowSelectEvent(Me,se) Me.Close() End If End If End Sub Public ReadOnly Property SelectedRow() As DataRow Get Return _SelectedRow End Get End Property Public ReadOnly Property SelectedIndex() As Integer Get Return _SelectedIndex End Get End Property End Class
最后界面: