Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 778
  • Last Modified:

vb.net 2008 add combobox into datagribview

Guys,

how to add combobox into a datagridview. and how to populate this combobox.
0
aarontham
Asked:
aarontham
  • 11
  • 10
1 Solution
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
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
 
aaronthamAuthor Commented:
pls provide vb.net 2008 code
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
aaronthamAuthor Commented:
Hi sree_ven,

i need code in vb.net 2008
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Have you looked to my code ?
0
 
aaronthamAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
aaronthamAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
aaronthamAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
How have you hide it ? If it's the third column you have to do this way:

Me.DataGridView1.Columns(2).Visible = False
0
 
aaronthamAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
aaronthamAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
aaronthamAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
You have to show how you get the error ... show some code!
0
 
aaronthamAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
aaronthamAuthor Commented:
Hi jpaulino:

in column 0 is User ID which is interger. and the user level in column 4
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
 
aaronthamAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now