Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to add a column as the second column in the data grid view in vb.net.
The data in the combobox of the grid should be in (.displaymember) and (.valuemember) pair. I tried but I do not think I am going anywhere with it. for in the tried code it adds the combobox column as the last column only.

What I have tried:

Private Sub frmInvoice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSql1 As String
        Dim combo As New DataGridViewComboBoxColumn()
        strSql1 = "Select ItemAutoID, ItemName from ItemMaster"

        Try
            conn.Open()
            With com
                .CommandType = CommandType.Text
                .CommandText = strSql1
                .Connection = conn
            End With
            adp.SelectCommand = com
            adp.Fill(dt)
            adp.Dispose()
            com.Dispose()
            conn.Close()

            With combo
                .HeaderText = "Item Name"
                .Name = "combo"
                .DisplayMember = "ItemName"
                .ValueMember = "ItemAutoID"
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        DataGridView1.ColumnCount = 5
        DataGridView1.Columns(0).Name = "Sr. No."
        DataGridView1.Columns.Add(combo)
        DataGridView1.Columns(2).Name = "Uom"
        DataGridView1.Columns(3).Name = "Qty"
        DataGridView1.Columns(4).Name = "Rate"
        DataGridView1.Columns(5).Name = "Value"


        

    End Sub
Posted
Updated 18-Sep-16 9:03am
v2

Try -
VB
DataGridView1.Columns.Insert(1, combo)

Please let me know if it doesn't help.
Thanks :)
 
Share this answer
 
Comments
Member 12741660 19-Sep-16 2:28am    
you are right, but the solution came in late I found this yesterday. This same line it works. But thanks anyways.
Suvendu Shekhar Giri 19-Sep-16 2:50am    
Glad that your problem is fixed :)
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

VB
DataGridView1.Columns.Add(combo)

What let you think that your code is adding a column in second position ?
 
Share this answer
 

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