从访问权限填充datagridview [英] Fill datagridview from access
问题描述
大家好
我正在尝试创建添加,删除,更新datagridview
我创建了一个datagridview1,并添加了2个textboxcolumns和1个comboboxcolumns
我使用此代码填充datagrid
Hi to all
I''m try to create a add,delete,update datagridview
I ave create a datagridview1 and a have add 2 textboxcolumns and 1 comboboxcolumns
I use this code to fill the datagrid
Dim Con As OleDbConnection
Dim adapter As New OleDbDataAdapter
Dim dataSet As New DataSet
Dim sqlCmd As OleDbCommand = New OleDbCommand("Select line_id, line_code, line_descr from line order by line_id")
Con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source = C:\Documents and Settings\Roidoulis\Επιφάνεια εργασίας\Roller_Database\bin\Debug\Roller_old.mdb")
Try
Con.Open()
sqlCmd.Connection = Con
adapter.SelectCommand = sqlCmd
adapter.Fill(dataSet, "lalakis")
With DataGridView1
.DataSource = dataSet
.DataMember = "lalakis"
End With
Catch 'ex As OledbException
End Try
adapter.Dispose()
Con.Close()
Con.Dispose()
我的问题之一是……
datagrid将我的3列留空,并添加3个新列,其标题为我的访问表中的名称.
问题1)如何使datagridview写在我的前三列中而不创建3个新列???????
如果我将代码更改为此
One of my problem is......
The datagrid leave my 3 columns empty and add 3 new columns with header the name from my access table.
Question 1 ) How i can make datagridview write in my first three columns and not create 3 new one????????
If i change me code to this
With DataGridView1
.AutoGenerateColumns = False
datagriv添加6空行(6是我文件中的记录)
之后,我删除自己的列,并使用它来更改标题文本
the datagriv add 6 empty rows (6 is the record in my file)
After this i delete my own columns and i use this to change the header text
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Code"
.Columns(2).HeaderText = "Descr"
问题2)请帮助我将第二列更改为combobox
谢谢和问候.
如果您发现有问题,请通知我....
Question 2 ) Please help me to change the second column to combobox
Thanks and regards.
If you see something wrong please infor me.......
推荐答案
个性我个人认为您这样做的方式是错误的.我将创建一个具有三个属性LineID,LineCode(将为Enum)和LineDescription的LineItem类.此类将具有共享的功能,该功能将返回数据库中所有当前行项目的列表.像这样的东西:
Personaly I think you are going about this the wrong way. I would create a class LineItem with three properties LineID, LineCode (which would be an Enum) and LineDescription. This Class would have a shared Function which would return a list of all current line items from the Database. Something like this:
Enum LineCode
Code1
Code2
Code3
End Enum
Class LineItem
Public Property LineID As Integer
Public Property LineCode As LineCode
Public Property LineDescription As String
Public Shared Function GetCurrentLineItems() As List(Of LineItem)
Dim lineItems As List(Of LineItem) = New List(Of LineItem)
Using con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source = D:\Documents\LineItems.mdb")
Dim cmd As New OleDb.OleDbCommand("SELECT LineID, LineCode, LineDescription FROM LineItems Order By LineID", con)
con.Open()
Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader()
While dr.Read()
Dim newLine As New LineItem()
newLine.LineID = dr.GetInt32(0)
newLine.LineCode = dr.GetInt32(1)
newLine.LineDescription = dr.GetString(2)
lineItems.Add(newLine)
End While
dr.Close()
End Using
Return lineItems
End Function
End Class
在我的Form上,我将手动设置带有三个相关列的DataGridView,并且在Form类中将拥有一个私有字段,该字段将保存一个List(Of LineItem)
.您可以在调用GetCurrentLineItems
方法的Forms Load事件中填充它.然后,您可以绑定到LineItems列表,也可以像这样手动输入每一行(我个人更喜欢)
On my Form I would manually set up the DataGridView with the three relevant columns and I would have a private field in my Form class that would hold a List(Of LineItem)
. You can populate this in the Forms Load event calling the GetCurrentLineItems
method. Then you can either bind to the list of LineItems or manually enter each row (which I personally prefer) like this
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
currentLineItems = LineItem.GetCurrentLineItems()
colCode.DataSource = [Enum].GetValues(GetType(LineCode))
colCode.DataPropertyName = "LineCode"
For Each lineItem As LineItem In currentLineItems
Dim dgRow As New DataGridViewRow()
dgRow.CreateCells(DataGridView1)
dgRow.Cells(0).Value = lineItem.LineID
dgRow.Cells(1).Value = lineItem.LineCode
dgRow.Cells(2).Value = lineItem.LineDescription
DataGridView1.Rows.Add(dgRow)
Next
End Sub
其中colCode是我的ComboBox列
希望对您有帮助
where colCode is my ComboBox column
Hope this helps
这篇关于从访问权限填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!