DataGirdView学习笔记
DataGridView资料在网上也没有找到太详细,通过几天的学习,个人感觉DataGridView在处理数据库上很强大也很简单。在这里整理下学到的一些知识,相对于DataGrieView来说只是很小的一部分功能,以后会持续更新。
一、DataGridView属性
1、AllowUserToAddRows
指示是否向用户显示用于添加行的选项。ture:会显示一个空行。false:不显示空行
区别:将数据库内容填充到DataGridView后,在获取最后一行数时,如果是ture则需要-2,false需要-1。
2、ReadOnly
指示用户是否可以编辑DataGridView控件的单元格。ture:可编辑。false:不可编辑
3、RowHeadersVisib
指示是否显示包含行标题的列。ture:显示。false:不显示
4、SelectionMode
指示如何选择DataGridView的单元格
FullRows:选中全行。
CellSelect:选中一个单元格
二、DataGridView操作
1、读取数据库,这里以access数据库为例,MSSQL数据库以后再更新
Public Class Form1
Public objConn As New OleDbConnection("Provider = Microsoft.jet.OLEDB.4.0;Data Source=d:\movie.mdb")
Public ds As DataSet = New DataSet()
Public objAdapter As New OleDbDataAdapter("select id,m_name,m_type3,m_hits,m_hits_time,m_date,moviestate from qingtiandy_movie where id < 100", objConn)
Public objAdapter2 As New OleDbDataAdapter("select m_name from qingtiandy_movie ", objConn)
Public objCmdBld As New OleDbCommandBuilder(objAdapter) '执行命令
Public LeafData As New Data.DataTable()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
objConn.Open()
DataGridView1.AutoGenerateColumns = False
objAdapter.Fill(ds, "qingtiandy_movie") '填充数据到dataset
DataGridView1.DataSource = ds.Tables("qingtiandy_movie")
'绑定自定义列
DataGridView1.Columns("id").DataPropertyName = ds.Tables(0).Columns(0).ToString
DataGridView1.Columns("m_name").DataPropertyName = ds.Tables(0).Columns(1).ToString
DataGridView1.Columns("m_type3").DataPropertyName = ds.Tables(0).Columns(2).ToString
DataGridView1.Columns("m_hits").DataPropertyName = ds.Tables(0).Columns(3).ToString
DataGridView1.Columns("m_hits_time").DataPropertyName = ds.Tables(0).Columns(4).ToString
DataGridView1.Columns("m_date").DataPropertyName = ds.Tables(0).Columns(5).ToString
DataGridView1.Columns("moviestate").DataPropertyName = ds.Tables(0).Columns(6).ToString
objConn.Close()
End Sub
End Class
2、删除数据
在DataGridView中数据时可以使用键盘delete键删除,也可以使用右键菜单。
1)、使用delete键删除
delete键删除有两个动作,a、UserDeletingRow b、UserdeletedRow
a、UserDeletingRow 行为发生在删除时
Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
'使用delete键执行删除操作,可以删除单行或多行
Dim index As Integer = Me.DataGridView1.CurrentRow.Index '获取光标所在行
'获取当前行内容
MsgBox(DataGridView1.Rows(index).Cells("id").Value.ToString())
MsgBox(DataGridView1.Rows(index).Cells("m_name").Value.ToString())
Dim m_name As String = DataGridView1.Rows(index).Cells("m_name").Value.ToString()
MsgBox("m_name = " & m_name)
ds.Tables(0).Rows(index).Delete() '在dataset中删除
objAdapter.Update(ds, "qingtiandy_movie") '将dataset数据更新到数据库
End Sub
b、UserdeletedRow 行为发生在删除后
Private Sub DataGridView1_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserDeletedRow
MsgBox("删除成功")
End Sub
2)、鼠标右键删除数据
右键弹出菜单
Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
'鼠标右键选中行并弹出菜单
If (e.Button = MouseButtons.Right) Then
If (e.RowIndex >= 0) Then
'若行已经是选中状态就不再进行设置
If (DataGridView1.Rows(e.RowIndex).Selected = False) Then
DataGridView1.ClearSelection()
DataGridView1.Rows(e.RowIndex).Selected = True
End If
'只选中一行时设置活动单元格
If (DataGridView1.SelectedRows.Count = 1) Then
DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
End If
'弹出操作菜单
ContextMenuStrip1.Show(MousePosition.X, MousePosition.Y)
End If
End If
End Sub
Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
'右键菜单删除光标所在行,一次只能删除一行
Dim index As Integer = Me.DataGridView1.CurrentRow.Index
MsgBox(DataGridView1.Rows(index).Cells("id").Value.ToString()) '获取当前行内容
ds.Tables(0).Rows(index).Delete() '在dataset中删除
objAdapter.Update(ds, "qingtiandy_movie")
End Sub
3、添加新数据
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'添加数据
objAdapter.Fill(ds, "qingtiandy_movie") '填充数据到dataset
DataGridView1.DataSource = ds.Tables("qingtiandy_movie")
DataGridView1.Columns("id").DataPropertyName = ds.Tables("qingtiandy_movie").Columns(0).ToString
DataGridView1.Columns("m_name").DataPropertyName = ds.Tables("qingtiandy_movie").Columns(1).ToString
DataGridView1.Columns("m_type3").DataPropertyName = ds.Tables("qingtiandy_movie").Columns(2).ToString
DataGridView1.Columns("m_hits").DataPropertyName = ds.Tables("qingtiandy_movie").Columns(3).ToString
DataGridView1.Columns("m_hits_time").DataPropertyName = ds.Tables("qingtiandy_movie").Columns(4).ToString
DataGridView1.Columns("m_date").DataPropertyName = ds.Tables("qingtiandy_movie").Columns(5).ToString
DataGridView1.Columns("moviestate").DataPropertyName = ds.Tables("qingtiandy_movie").Columns(6).ToString
ds.Tables("qingtiandy_movie").Rows.Clear() '清空数据
Me.DataGridView1.DataSource = ds.Tables("qingtiandy_movie") '重新设置源保证datatables结构不变
ds.Tables("qingtiandy_movie").Rows().Add(Array_Name) '添加数据到datatables
objAdapter.Update(ds, "qingtiandy_movie") '更新到数据库
End Sub