如何从Vb .Net中的Datagridview中删除选定的行 [英] How Do I Delete A Selected Row From The Datagridview In Vb .Net
本文介绍了如何从Vb .Net中的Datagridview中删除选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai们,我创建了一个表单来插入,更新,删除。
我正在使用datagridview控件查看数据库更新。
这里我真正需要的是什么是,
如果我从datagridview点击一行并点击删除它应该删除。
i需要代码这个
帮帮我,
关注Sissy Ram
解决方案
私人 Sub Button1_Click( ByVal sender As 系统。对象, ByVal e As System.EventArgs)句柄 Button1.Click
如果 我 .DataGridView1.SelectedRows.Count> 0 然后
GetValueByQuery( delete * from table1 where id =& Me .DataGridView1.SelectedRows( 0 )。单元格( 0 )。值)
结束 如果
结束 Sub
实际上,getValuebyQuery是我自己的函数,我将它放在这里,以便您可以使用它。在此功能中,您可以根据需要使用select / insert / update / delete查询..
< pre lang = vb> 公共 oCon 作为 新 System.Data。 OleDb.OleDbConnection( provider = microsoft.jet.OLEDB.4.0; data source =& Application .StartupPath& \ Data\mydata.mdb)
函数 GetValueByQuery( ByVal 查询作为 < span class =code-keyword> String ) As String
< span class =code-keyword> Dim temp As String
< span class =code-keyword> Dim ocom As 新 OleDbCommand
Dim oRead As OleDbDataReader
如果 oCon.State = ConnectionState.Closed 然后
oCon.Open()
结束 如果
ocom.Connection = oCon
ocom.CommandText = Query
oRead = ocom.ExecuteReader
如果 oRead.HasRows = True 那么
oRead.Read()
如果 IsDBNull(oRead( 0 ))= 真 然后
temp =& quot; 0& quot;
其他
temp = oRead( 0 )
结束 如果
oRead.Close()
Else
temp =& quot; 0& quot;
oRead.Close()
结束 如果
返回 temp
结束 功能 < / pre >
Hai guys, I created a form to insert, update, delete.
And i am using datagridview control to view the database updation.
Here what i actually need is,
If i click a row from datagridview and click delete it should delete.
i need code for this
Help me,
Regards Sissy Ram
解决方案
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Me.DataGridView1.SelectedRows.Count > 0 Then GetValueByQuery("delete * from table1 where id=" & Me.DataGridView1.SelectedRows(0).Cells(0).Value) End If End Sub
Ohh actually getValuebyQuery is my own function I am enclosing here with so that you can use it. In this function you can use select/insert/update/delete query as you required..
<pre lang="vb"> Public oCon As New System.Data.OleDb.OleDbConnection("provider=microsoft.jet.OLEDB.4.0;data source=" & Application.StartupPath & "\Data\mydata.mdb") Function GetValueByQuery(ByVal Query As String) As String Dim temp As String Dim ocom As New OleDbCommand Dim oRead As OleDbDataReader If oCon.State = ConnectionState.Closed Then oCon.Open() End If ocom.Connection = oCon ocom.CommandText = Query oRead = ocom.ExecuteReader If oRead.HasRows = True Then oRead.Read() If IsDBNull(oRead(0)) = True Then temp = "0" Else temp = oRead(0) End If oRead.Close() Else temp = "0" oRead.Close() End If Return temp End Function</pre>
这篇关于如何从Vb .Net中的Datagridview中删除选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文