[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vb.net 2008 add combobox into datagribview

Posted on 2009-04-07
22
Medium Priority
?
774 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 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