将数据网格中的数据显示为表单 [英] displaying data from a datagrid into a form
问题描述
我正在使用VB.Net
我有一个包含一些文本框的表单,用于显示来自数据库的数据(使用MS-ACCESS创建).该表之一被命名为pmast.表单上有一些按钮,单击这些按钮可将记录上移,下移,顶部和顶部.底部.它还具有一个按钮,单击该按钮可打开一个数据网格并显示记录.我已经编写了一个代码来从网格中选择特定记录,并将内容显示到表单中.
现在,当我通过单击从网格中选择特定记录时,该记录正在更改其顺序,因此我无法移至上一个/下一个记录. Pl.建议适当的补救措施.
代码如下:
*****用于打开桌子
I am using VB.Net
I have a form containing some text boxes for displaying data from a database (created using MS-ACCESS). One of the table is named as pmast. The form has some buttons which when clicked moves the record up, down, top & bottom. Also it has a button which when clicked opens a datagrid and displays the records. I have written a code to select a particular record from the grid and displays the contents into the form.
Now when I am selecting a particular record from the grid by clicking, the record is changing its sequence and I am not able to move to previous/next record. Pl. suggest a suitable remedy.
The code is as follows :
***** FOR OPENING THE TABLE
Private Sub pmastfrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.MdiParent = mainform
ds = "Provider=Microsoft.Jet.OLEDB.4.0;"
ds = ds & "Data Source=" & Application.StartupPath & "\TCPL.mdb;"
ds = ds & "Mode=ReadWrite|Share Deny None;"
ds = ds & "Persist Security Info=False;"
Dim strcon As New OleDb.OleDbConnection(ds)
bindda = New OleDb.OleDbDataAdapter("select * from pmast order by rcpno", strcon)
If Not oleds.Tables("pmast") Is Nothing Then
oleds.Tables("pmast").Clear()
End If
bindda.Fill(oleds, "pmast")
bindmg = Me.BindingContext(Me.oleds.Tables("pmast"))
If Me.labrefid.DataBindings.Count > 0 Then
Me.labrefid.DataBindings.Remove(labrefid.DataBindings("text"))
Me.labrefid.DataBindings.Add("text", oleds.Tables("pmast"), "rcpno")
Else
Me.labrefid.DataBindings.Add("text", oleds.Tables("pmast"), "rcpno")
End If
If Me.labrefdt.DataBindings.Count > 0 Then
Me.labrefdt.DataBindings.Remove(labrefdt.DataBindings("text"))
Me.labrefdt.DataBindings.Add("text", oleds.Tables("pmast"), "rcpdt", True)
Else
Me.labrefdt.DataBindings.Add("text", oleds.Tables("pmast"), "rcpdt", True)
End If
If Me.patname.DataBindings.Count > 0 Then
Me.patname.DataBindings.Remove(patname.DataBindings("text"))
Me.patname.DataBindings.Add("text", oleds.Tables("pmast"), "pname")
Else
Me.patname.DataBindings.Add("text", oleds.Tables("pmast"), "pname")
End If
If Me.patadd.DataBindings.Count > 0 Then
Me.patadd.DataBindings.Remove(patadd.DataBindings("text"))
Me.patadd.DataBindings.Add("text", oleds.Tables("pmast"), "address")
Else
Me.patadd.DataBindings.Add("text", oleds.Tables("pmast"), "address")
End If
If Me.patcontact.DataBindings.Count > 0 Then
Me.patcontact.DataBindings.Remove(patcontact.DataBindings("text"))
Me.patcontact.DataBindings.Add("text", oleds.Tables("pmast"), "contact")
Else
Me.patcontact.DataBindings.Add("text", oleds.Tables("pmast"), "contact")
End If
If Me.patsex.DataBindings.Count > 0 Then
Me.patsex.DataBindings.Remove(patsex.DataBindings("text"))
Me.patsex.DataBindings.Add("text", oleds.Tables("pmast"), "category")
Else
Me.patsex.DataBindings.Add("text", oleds.Tables("pmast"), "category")
End If
If Me.patreference.DataBindings.Count > 0 Then
Me.patreference.DataBindings.Remove(patreference.DataBindings("text"))
Me.patreference.DataBindings.Add("text", oleds.Tables("pmast"), "reference")
Else
Me.patreference.DataBindings.Add("text", oleds.Tables("pmast"), "reference")
End If
bindmg.Position = 0
GroupBox1.Enabled = True
labrefid.Enabled = False
labrefdt.Enabled = False
GroupBox2.Enabled = True
patname.Enabled = False
patadd.Enabled = False
patcontact.Enabled = False
patsex.Enabled = False
patreference.Enabled = False
patage.Enabled = False
End Sub
*****用于将记录移至上一个/下一个
***** FOR MOVING THE RECORD TO PREVIOUS / NEXT
Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
If bindmg.Position = 0 Then
MsgBox("You Are Currently On The First Record", MsgBoxStyle.Information)
Else
bindmg.Position = bindmg.Position - 1
End If
End Sub
Private Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
If bindmg.Position = bindmg.Count - 1 Then
MsgBox("You Are Currently On The Last Record", MsgBoxStyle.Information)
Else
bindmg.Position += 1
End If
End Sub
*****用于打开网格
***** FOR OPENING THE GRID
Private Sub pmastview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PMASTTableAdapter.Fill(Me.TCPLDataSet.PMAST)
Me.MdiParent = mainform
End Sub
*****用于从GRID中选择特定的记录
***** FOR SELECTING A PARTICULAR RECORD FROM GRID
Private Sub PMASTDATA_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PMASTDATA.CellDoubleClick
pmastfrm.labrefid.Text = PMASTDATA.CurrentRow.Cells(0).Value
pmastfrm.labrefdt.Text = PMASTDATA.CurrentRow.Cells(1).Value
pmastfrm.patname.Text = PMASTDATA.CurrentRow.Cells(2).Value
pmastfrm.patadd.Text = PMASTDATA.CurrentRow.Cells(3).Value
pmastfrm.patcontact.Text = PMASTDATA.CurrentRow.Cells(4).Value.ToString
pmastfrm.patage.Text = PMASTDATA.CurrentRow.Cells(5).Value.ToString
pmastfrm.patsex.Text = PMASTDATA.CurrentRow.Cells(6).Value.ToString
pmastfrm.patreference.Text = PMASTDATA.CurrentRow.Cells(7).Value.ToString
pmastfrm.Show()
pmastfrm.viewbtn.Enabled = True
Me.Close()
Me.Hide()
End Sub
现在执行代码时,我无法在记录之间导航. Pl.尝试解决此问题....
Now on executing the code, I am not able to navigate between the records. Pl. try to solve this problem....
推荐答案
如果可以发布代码,那就更好了.尽管可以使用rowdatabound()和rowitemcommand() gridview通过其id捕获数据的方法.
然后,您可以对其进行编辑,将其删除为所需的任何内容,也不会丢失数据的顺序..
希望对您有帮助.
尝试在msdn上搜索gridview的rowdatabound和rowitemcommand方法.
it would be better if you can post your code..though you can use the rowdatabound() and rowitemcommand() method of gridview to catch the data by its id.
you can then edit it, delete it anything you want and you won''t lose sequence of the data also..
hope this ll help you.
try searching on msdn for rowdatabound and rowitemcommand method of gridview.
这篇关于将数据网格中的数据显示为表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!