Solved

vb.net 2008 add combobox into datagribview

Posted on 2009-04-07
22
746 Views
Last Modified: 2013-11-26
Guys,

how to add combobox into a datagridview. and how to populate this combobox.
0
Comment
Question by:aarontham
  • 11
  • 10
22 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24094308
can do by :
<asp:GridView ID="GVTest" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlTest">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
0
 

Author Comment

by:aarontham
ID: 24094344
pls provide vb.net 2008 code
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24094508
You can do something like this:


        ' Create a new combobox column

        Dim dgvColumn As New DataGridViewComboBoxColumn

        ' Define the datasource (dt = datatable with data)

        dgvColumn.DataSource = dt.DefaultView

        dgvColumn.DisplayMember = "field to show"

        dgvColumn.ValueMember = "id"
 

        ' Adds the new column to the datagridview

        Me.DataGridView1.Columns.Add(dgvColumn)

Open in new window

0
 

Author Comment

by:aarontham
ID: 24103820
Hi sree_ven,

i need code in vb.net 2008
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24104766
Have you looked to my code ?
0
 

Author Comment

by:aarontham
ID: 24104860
Hi jpaulino,

i have datagridview like below.  

company    UserName    Level
ABC            Paul              Admin
FGK            Alex              Operator
Ford           Sun               Manager

on the level column i only have 3 choice. i need a combobox on the level cell so that i can select either Admin,Operator or Manager.
I use the same save code you provide me.

pls advice
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24104895
I don't understand if you already have that information on the datagridview or if you want to add it. If you don't have you can use the code bellow and if you already have it you can hide that column and add this one.
        Dim dgvColumn As New DataGridViewComboBoxColumn

        dgvColumn.Name = "Level"

        dgvColumn.Items.AddRange(New Object() {"Admin", "Operator", "Manager"})

        Me.DataGridView1.Columns.Add(dgvColumn)

Open in new window

0
 

Author Comment

by:aarontham
ID: 24104962
Hi jpaulino:

I already have that information in the Datagridview. now i wish to change let say Alex from Operator
 to Manager. how to add a combobox on that column yet after value change i can save back to database use the db.update
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24105021
You can hide the existing column from the datagridview and add the new one (with the combobox). Then when you change the combobox you update the hidden column value in that row.
0
 

Author Comment

by:aarontham
ID: 24105360
Hi jpaulino,

after i hide the column.
i try your below code.
 Dim dgvColumn As New DataGridViewComboBoxColumn
        dgvColumn.Name = "Level"
        dgvColumn.Items.AddRange(New Object() {"Admin", "Operator", "Manager"})
        Me.DataGridView1.Columns.Add(dgvColumn)
 
i got below error.
object reference not set to an instance of an object
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24105392
How have you hide it ? If it's the third column you have to do this way:

Me.DataGridView1.Columns(2).Visible = False
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:aarontham
ID: 24105737
Hi jpaulino,

i got it work.  i also need to display the level result from db to the combobox.pls provide code
pls also provide code for combobox update the hidden column value .
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24106520
Here you have an example
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23805212.html
After the selection you just need to fill the correct  cell
0
 

Author Comment

by:aarontham
ID: 24113465
Hi jpaulino,

i got it work. but on the data loading part. it that possible to load those data into combobox to display user current level.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24114087
After the load, before the user see it, you can loop on all rows and set the correct value in the combo.
To select a value in the combobox you just need to do me.datagridview(column,row).value = "The current user form the hidden column/cell"
0
 

Author Comment

by:aarontham
ID: 24117461
hi jpaulino,
i tryed it got error. but after the form finish load. i use button to fire it work.
the loading done in form load event. any idea what event after the form load event
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24127393
You have to show how you get the error ... show some code!
0
 

Author Comment

by:aarontham
ID: 24128197
Hi jpaulino,

i attach is the code .

below is the error msg.



---------------------------
DataGridView Default Error Dialog
---------------------------
The following exception occurred in the DataGridView:



System.Exception: Admin is not a valid value for Int32. ---> System.FormatException: Input string was not in a correct format.

   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)

   at System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo)

   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)

   --- End of inner exception stack trace ---

   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)

   at System.ComponentModel.TypeConverter.ConvertFrom(Object value)

   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)



To replace this default dialog please handle the DataError event.
---------------------------
OK  
---------------------------

 Me.Cursor = Cursors.AppStarting

        Dim cnString3 As String = My.Settings.DataCon

        Dim strQuery3 As String = "SELECT * From t_user"

        adapter = New MySqlDataAdapter(strQuery3, cnString3)

        myDataSet = New DataSet

        'Try

        adapter.Fill(myDataSet)
 

        'FILL DATAGRIBVIEW FROM DATABASE

        Me.DataGridView1.DataSource = myDataSet.Tables(0).DefaultView

        'HIDE COLUMN

        ' Me.DataGridView1.Columns("Level").Visible = False

        Me.DataGridView1.Columns(4).Visible = False
 
 

        Dim dgvCombo As New DataGridViewComboBoxColumn

        With dgvCombo

            .HeaderText = "Level"

            .Width = 80

            .Items.AddRange(New Object() {"Admin", "Operator", "Manager", ""})

            ' .Name = "strTest"

        End With

        DataGridView1.Columns.Insert(4, dgvCombo)
 
 

        For i = 0 To DataGridView1.RowCount - 1

            'MsgBox(DataGridView1.Item(5, i).Value)

            DataGridView1(0, i).Value = DataGridView1(5, i).Value

        Next

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24130063
In the column 0 you have the user level and you're trying to pass the new combocolumn to it ?
Check if you have a numeric value for the level and you're trying to use a string (in this case "Admin")
0
 

Author Comment

by:aarontham
ID: 24134931
Hi jpaulino:

in column 0 is User ID which is interger. and the user level in column 4
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24135909
In what line you having the error ?
And what this cycle is doing ?
        For i = 0 To DataGridView1.RowCount - 1
            'MsgBox(DataGridView1.Item(5, i).Value)
            DataGridView1(0, i).Value = DataGridView1(5, i).Value
        Next
0
 

Author Comment

by:aarontham
ID: 24154280
Hi jpaulino,

the cycle is you teach me to add those User level from Datagridview to the Datagribview combobox.

i remove this cycle it work. but will have 1 column with user lever and one column with combobox.

i prefer to hide the user level column and then copy the user level to then combobox.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now