Bound datagridview with unbound comboboxcolumn - problems

Answered Bound datagridview with unbound comboboxcolumn - problems

  • 2012年7月17日 1:44
     
     

    Hello !

    i'm working on a vb.net 2010  /sql server 2008r2 / entity framework project

    i have a bound datagridview , and inside datagridview  i have a unbound combobox column that have integer as items (1,2,3...)

    when i try to select a value from unbound combobox , after leaving the cell the value is back to default , and doesn't keep my selection.

    What can i do ?

    thank you !

すべての返信

  • 2012年7月18日 4:44
    モデレータ
     
     回答済み コードあり

    Hi dcode25,

    Welcome to the MSDN forum.

    As I read your thread, it seems the combobox column of datagridview is also bound with database. I have no idea about what is your code, here is a sample made for you:

             'add value to DataTable
             DataGridView1.DataSource = WTDataTable
                Dim combo As New DataGridViewComboBoxColumn
                combo.Name = "Combo"
                combo.HeaderText = "Combobox"
                For i As Integer = 0 To 5
                    combo.Items.Add(i.ToString)
                Next
                DataGridView1.Columns.Insert(0, combo)

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • 2012年7月18日 11:14
     
     

    Thank you !

    But the combobox , isn't bound to database.

    I use exactly your code to create and fill the combo , but the problem is when i try on runtime , to select a value from this combobox , after leave the cell , the value on this combobox is back to default value ( 0). So i can't select a value from this combobox , this is my problem .

    What can i do ?

  • 2012年7月19日 2:41
    モデレータ
     
     

    Hi dcode25,

    It is strange. The code works well in my side. Here is a screen shot in my side:

    Would you like to share the code about the Combobox in your side? Or create a new application to test my code.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • 2012年7月19日 12:25
     
     

    Thank you !

    The only difference is that i have created the unbound combobox inside datagridview on design time , and not in runtime like your code.

    But is this  the cause  of my problem ?

  • 2012年7月20日 2:41
    モデレータ
     
     

    Hi dcode25,

    >>i have created the unbound combobox inside datagridview on design time

    I think that is the reason. When you add the combobox in design time, the Combobox column will be a part of datagridview. I have no idea about how you bind the data with datagridview. But it can lead to also bind with datagridview by mistake. So I will suggest you to use the code I showed for you.

    Have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • 2012年7月21日 0:35
     
     

    I have used your code , and now i can change the value of combobox , but if i try to use the value of this combobox after changing , the old value is returned . I use this instruction :

    Myvalue=DataGridView1.Rows(e.RowIndex).Cells("Combo").Value   ( or .FormattedValue) 

    i have used this instruction on
    Datagridview.CellParsing
    Datagridview.CellValidating
    DatagridView.CellValueChanged

    But is the same result , Myvalue has the old value of combobox.

    What can i do ?


    • 編集済み dcode25 2012年7月21日 0:37
    •  
  • 2012年7月23日 2:46
    モデレータ
     
      コードあり

    Hi dcode25,

    Not sure the code you have tried, but this code works well in my side:

    Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
                MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
        End Sub

    Have a try.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • 2012年7月23日 2:54
     
      コードあり

    Hi, I'm doing the exact same thing but I didn't have that problem, how u populate ur comboboxcolumn?

    this is wut i did:

    Dim mycomboboxcolumn As New DataGridViewComboBoxColumn()
    Dim array() as string = {"one", "two", "three", "four"}
    For i as Integer = 0 to 3
        mycomboboxcolumn.Items.Add(array(i))
    Next
    comboboxcolumn_NombreEmpresa.Name = "My ComboColumn"
    dgv_Dynamic.Columns.Add(comboboxcolumn)

    ofc the dgv was defined as:

    Dim WithEvents dgv_Dynamic As New DataGridView()

    hope it helps





    • 編集済み dpolancom 2012年7月23日 3:11
    •  
  • 2012年7月26日 9:22
    モデレータ
     
     

    Hi dcode25,

    We haven’t heard from you for several days. I’d like to mark my reply as answer firstly. If you have any additional questions, you also can unmark the replay and post your question here.

    Sorry for any inconvenience and have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us