查找的资料收藏于此。
获取总行数:dataGridView1.Rows.Count;
获取当前选中行索引:int i = this.dataGridView1.CurrentRow.Index;
获取当前选中列索引:int j = this.dataGridView1.CurrentCell.ColumnIndex;
MessageBox.Show("一共有"+ dataGridView1.SelectedRows.Count.ToString()+"被选中");
if (dataGridView1.Rows[0].Selected )
MessageBox.Show("第一行被选中了");
取消默认选中行。
DataGridView.ClearSelection()
选中某个单元格的值。
DataGridView.Rows(0).Cells(0)
http://blog.youkuaiyun.com/wuoomail/article/details/6628397
在机房收费系统中所有学生全部下机。
''' <summary>
''' 所有学生全部下机。
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub ToolStripMenuItem_AllAwayLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem_AllAwayLine.Click
Try
Dim R_intStuCardID As Integer '定义卡号的循环变量。
Dim dataSetCount As Integer ' 定义DataGridView中的总行数。
'获得DataGridView中的总行数。
dataSetCount = (dgvOnline.Rows.Count.ToString())
'如果没有正在上机的记录。
If dataSetCount = 0 Then
MsgBox("小提示:没有上机的记录!")
Exit Sub
End If
'根据DataGridView 中的行数循环来执行全部下机。
For dataSetCount = 0 To dataSetCount - 1
R_intStuCardID = dgvOnline.Rows(dataSetCount).Cells(dataSetCount).Value.ToString()
Dim bllOnlineDel As New BLL.BLL_OnlineInquire
If bllOnlineDel.BLL_ForceSelStuOnline(R_intStuCardID, UI_FormLogin.strMacIP, UI_FormLogin.txtUserName.Text) = False Then
MsgBox("所有学生下机失败!")
End If
Next
'在删除所有正在上机的学生记录后,重新把表绑定到DataGridView中。
Dim bllStuOnline As New BLL.BLL_OnlineInquire
Dim myDataSet As New DataSet
myDataSet = bllStuOnline.StuOnlineInquire()
'绑定到datagrideview控件上。
dgvOnline.DataSource = myDataSet.Tables(0)
MsgBox("所有学生下机成功!")
Catch ex As Exception
MsgBox("所有学生下机失败!")
End Try
End Sub
选中DataGridView中某一行获取该行第一列的值。
''' <summary>
''' 单击DataGridView某一行,获取该行第一列的值。
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub dgvOnline_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvOnline.MouseClick
Try
If dgvOnline.Rows.Count = 0 Then
dgvOnline.Enabled = False
Exit Sub
End If
Dim result As String = dgvOnline.Item(0, dgvOnline.CurrentCell.RowIndex).Value.ToString.Trim
intStuCardID = result
Catch ex As Exception
Throw New Exception("选中一行出错误!")
End Try