Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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
VB
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()


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
VB
With DataGridView1
    .AutoGenerateColumns = False

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
VB
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Code"
.Columns(2).HeaderText = "Descr"


Question 2 ) Please help me to change the second column to combobox

Thanks and regards.

If you see something wrong please infor me.......
Posted
Updated 18-Apr-11 21:50pm
v3

1 solution

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:

VB
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


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

VB
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


where colCode is my ComboBox column

Hope this helps
 
Share this answer
 
Comments
Vagelisr 19-Apr-11 5:04am    
My frend thanks a lot for all of this code but i am too rookie in datagrid and to be absolutely ownet i dont undestand many thinks.
I was wondering if you can create an example as simple as possible
I knew i ask a lot of things but an example will help me a lot.

Thanks again for your post and have a nice day.......
Wayne Gaylard 19-Apr-11 5:09am    
I think the example I posted is pretty self explanatory. I would read up more on DataGridView and learn the basics first if I were you.
Vagelisr 19-Apr-11 6:11am    
You are absolutely wright my friend but wright now i dont have time for "reading"
I knew i have take the wrong path but when i found some time i will do this.

Thank you very much for all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900