大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你是想获取总行数?还是选中行和列的索引?
我们提供的服务有:成都网站设计、网站建设、微信公众号开发、网站优化、网站认证、昂仁ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的昂仁网站制作公司
获取总行数:dataGridView1.Rows.Count;
获取当前选中行索引:int i = this.dataGridView1.CurrentRow.Index;
获取当前选中列索引:int j = this.dataGridView1.CurrentCell.ColumnIndex;
假如一个表有好多个taget:
我们要拿Southern CN和KDC之间的Target的行数
思路:我们先拿Southern CN的行数,然后拿KDC的行数
dt.Rows.
IndexOf(dt.Rows.Cast(Of System.Data.DataRow).
Where(Function(r) r(3).ToString.StartsWith("Target") And dt.Rows.IndexOf(r) iSCN And dt.Rows.IndexOf(r) iKDCRow).First()) + 1
在窗体上加上三个标签控件:Label1、Label2、Label3
Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
Dim counter As Integer
Dim SelectedCellTotal As Integer = 0
Dim SelectedCellCount As Integer = 0
For counter = 0 To (DataGridView1.SelectedCells.Count - 1)
If DataGridView1.SelectedCells(counter).FormattedValueType Is _
Type.GetType("System.String") Then
Dim value As String = Nothing
If (DataGridView1.IsCurrentCellDirty = True) Then
value = DataGridView1.SelectedCells(counter).EditedFormattedValue.ToString()
Else
value = DataGridView1.SelectedCells(counter).FormattedValue.ToString()
End If
If value IsNot Nothing Then
If Not value.Length = 0 Then
SelectedCellTotal = SelectedCellTotal + Integer.Parse(value)
SelectedCellCount = SelectedCellCount + 1
End If
End If
End If
Next
Label1.Text = "选中的单元格个数为: " SelectedCellCount.ToString()
Label2.Text = "单元格里数据之和为: " SelectedCellTotal.ToString()
Label3.Text = "选中的单元格行数为:" DataGridView1.SelectedRows.Count.ToString()
End Sub
仿造我的例子,你自己做做看1)设计一个类似的界面(我只有两个字段) 2)单击GridView的右上角小箭头,去掉逗允许编辑地(黑色框部分): 3)然后把GridView的属性做如下改动: 4)在Form1中增加绑定数据的代码(我是模拟的)Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '动态增加一个表格,绑定到GridView上
Dim dt As New DataTable
dt.Columns.Add("Id", GetType(Integer))
dt.Columns.Add("name", GetType(String))
dt.Columns(0).AutoIncrement = True
dt.Columns(0).AutoIncrementSeed = 1
dt.Columns(0).AutoIncrementStep = 1 '模拟数据库数据
Dim row As DataRow For i As Integer = 1 To 10
row = dt.NewRow
row("name") = "name" i
dt.Rows.Add(row)
Next
dt.AcceptChanges() DataGridView1.DataSource = dt End Sub 5)然后使用SelectionChanged事件这样编码:Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
If (DataGridView1.SelectedRows IsNot Nothing AndAlso DataGridView1.SelectedRows.Count 0) Then
'只选择单行,因此取第一行
txtId.Text = DataGridView1.SelectedRows(0).Cells(0).Value
txtName.Text = DataGridView1.SelectedRows(0).Cells(1).Value
End If
End Sub