Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewComboBoxColumn doesn't sort and looses values when DataGridViewTextBoxColumns get sorted
 

DataGridViewComboBoxColumn doesn't sort and looses values when DataGridViewTextBoxColumns get sorted

Example 1:

Public
Class Form1 Dim mdtWeight As DataTable Dim mdtCases As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load mdtZwaarte = New DataTable("WeightTable") mdtZwaarte.Columns.Add("ID", GetType(Integer)) mdtZwaarte.Columns.Add("WEIGHT", GetType(String)) mdtZwaarte.Rows.Add(1, "Lite") mdtZwaarte.Rows.Add(2, "Middle") mdtZwaarte.Rows.Add(3, "Heavy") mdtZaken = New DataTable("CasesTable") mdtZaken.Columns.Add("NR", GetType(Integer))

mdtZaken.Rows.Add(38) mdtZaken.Rows.Add(49) mdtZaken.Rows.Add(43) mdtZaken.Rows.Add(69) Dim txtColumn As DataGridViewTextBoxColumn Dim cbColumn As DataGridViewComboBoxColumn DataGridView1.AutoGenerateColumns = False txtColumn = New DataGridViewTextBoxColumn txtColumn.HeaderText = "Document number" txtColumn.Name = "DocumentNumber" txtColumn.ReadOnly = True cbColumn = New DataGridViewComboBoxColumn cbColumn.HeaderText = "Case Weight" cbColumn.Name = "CaseWeight" cbColumn.SortMode = DataGridViewColumnSortMode.Automatic 'Add these Columns to DataGridView Column Collection DataGridView1.Columns.AddRange(New DataGridViewColumn() {txtColumn, cbColumn}) DataGridView1.Columns("CaseWeight").DataPropertyName = "ID" DataGridView1.Columns("DocumentNumber").DataPropertyName = "NR" cbColumn.ValueMember = "ID" cbColumn.DisplayMember = "WEIGHT"

cbColumn.DataSource = mdtWeight DataGridView1.DataSource = mdtCases End Sub End Class
Example 2:
Public
Class Form1 Dim dsData As DataSet Dim dtSuppliers As DataTable Dim dtOrders As DataTable Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim OrderNo As DataGridViewTextBoxColumn Dim SupplierName As DataGridViewComboBoxColumn InitTables() DataGridView1.AutoGenerateColumns = False

'Column 1: OrderNo OrderNo = New DataGridViewTextBoxColumn OrderNo.HeaderText = "Order No" OrderNo.Name = "OrderNo" OrderNo.ReadOnly = True 'Column 2: SupplierName SupplierName = New DataGridViewComboBoxColumn SupplierName.HeaderText = "Supplier Name" SupplierName.Name = "SupplierName" SupplierName.SortMode = DataGridViewColumnSortMode.Automatic 'Add these Columns to DataGridView Column Collection DataGridView1.Columns.AddRange(New DataGridViewColumn() {OrderNo, SupplierName})
DataGridView1.Columns("SupplierName").DataPropertyName = "SupplierID" DataGridView1.Columns("OrderNo").DataPropertyName = "OrderNo" SupplierName.ValueMember = "SupplierID" SupplierName.DisplayMember = "SupplierName" DataGridView1.DataSource = dsData.Tables("Orders") 'dtOrders SupplierName.DataSource = dsData.Tables("Suppliers") 'dtSuppliers End Sub Private Sub InitTables() dtSuppliers = New DataTable("Suppliers") dtOrders = New DataTable("Orders") 'DataTable Suppliers: SupplierID | SupplierName dtSuppliers.Columns.Add("SupplierID", GetType(Integer)) dtSuppliers.Columns.Add("SupplierName", GetType(String)) 'DataTable Suppliers: OrderNo | SupplierID 'Use this SupplierID to get SupplierName from Suppliers Table 'to Show in DataGridView dtOrders.Columns.Add("OrderNo", GetType(Integer)) dtOrders.Columns.Add("SupplierID", GetType(Integer)) 'Add 5 Rows to Both Tables Dim row As DataRow For i As Integer = 1 To 5 row = dtSuppliers.NewRow row("SupplierID") = i row("SupplierName") = "Supplier " & i dtSuppliers.Rows.Add(row) row = dtOrders.NewRow row("OrderNo") = i row("SupplierID") = i dtOrders.Rows.Add(row) Next dsData = New DataSet dsData.Merge(dtSuppliers) dsData.Merge(dtOrders) End Sub
End
Class

Hello Guys,

First code example is coded by myself anddoesn't work properly.
There are 2 odd things:
1. The DataGridViewComboBoxdoesn't sort
2. When there are values selected in the DataGridViewComboBoxColumn and DataGridViewTextBoxColumn is sorted, the DataGridViewComboBoxColumn looses all the values and doesn't sort along.

While the second code example works just fine. (Example from the Internet)

When I look at the code I conclude that I practically do the same thing.

I haven't been able to find the difference, that's why I'm asking you guys for help: what is wrong with the first example?

Thanks in advance!

Kind Regards,
Orhan

  • Edited byorhantje Tuesday, August 18, 2009 11:56 AMreference correction
  •  
orhantje  Tuesday, August 18, 2009 11:51 AM
The datasets you are binding to are never populated in the first example. Is this a cut/paste error?

(you declare and bind mdtWeight and mdtCases but populate mdtZwaare and mdtZaken)
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, August 18, 2009 3:05 PM
Hi DeborahK,

Indeed, it is a cut paste error.

It should look like this:

Public Class Form1 

    Dim mdtWeight As DataTable
    Dim mdtCases As DataTable 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        mdtWeight = New DataTable("WeightTable")
        mdtWeight.Columns.Add("ID", GetType(Integer))
        mdtWeight.Columns.Add("WEIGHT", GetType(String))

        mdtWeight.Rows.Add(1, "Lite")
        mdtWeight.Rows.Add(2, "Middle")
        mdtWeight.Rows.Add(3, "Heavy")

        mdtCases = New DataTable("CasesTable")
        mdtCases.Columns.Add("NR", GetType(Integer))
        mdtCases.Rows.Add(38)
        mdtCases.Rows.Add(49)
        mdtCases.Rows.Add(43)
        mdtCases.Rows.Add(69)

        Dim txtColumn As DataGridViewTextBoxColumn
        Dim cbColumn As DataGridViewComboBoxColumn

        DataGridView1.AutoGenerateColumns = False

        txtColumn = New DataGridViewTextBoxColumn
        txtColumn.HeaderText = "Document number"
        txtColumn.Name = "DocumentNumber"
        txtColumn.ReadOnly = True

        cbColumn = New DataGridViewComboBoxColumn
        cbColumn.HeaderText = "Case Weight"
        cbColumn.Name = "CaseWeight"
        cbColumn.SortMode = DataGridViewColumnSortMode.Automatic

        'Add these Columns to DataGridView Column Collection
        DataGridView1.Columns.AddRange(New DataGridViewColumn() {txtColumn, cbColumn})

        DataGridView1.Columns("CaseWeight").DataPropertyName = "ID"
        DataGridView1.Columns("DocumentNumber").DataPropertyName = "NR"

        cbColumn.ValueMember = "ID"
        cbColumn.DisplayMember = "WEIGHT"
        cbColumn.DataSource = mdtWeight
        DataGridView1.DataSource = mdtCases

    End Sub

End Class


I struggled with this for a few days, but finally I have found the problem.

In the second example the DataTable that is bind to the DataGridView has also acolumn for SupplierID.

So, I should have acolumn in the mdtCases DataTable which 'references' the mdtWeight. That way the DataGridView will sort well when the headers are clicked and the selected values of a DataGridViewComboBoxColumn will not get lost after a sort.

Thanks for the help DeborahK!

Kind Regards,
Orhan
orhantje  Wednesday, August 19, 2009 6:16 AM

You can use google to search for other answers

Custom Search

More Threads

• Trouble with filtering parent table with DataGridView and typed Dataset
• DataGridView, BindingList<T>, EndEdit() problem
• Force another key in datagrid?
• Expand the width of the DataGridView?
• Displaying a memo field in a Datagridview
• How Can I Make a DataGridView Row/Cell Selected?
• function evaluation disabled because a previous function timed out. You must continue execution to reenable function evaluation
• Copy-Paste images in DataGridWiew
• DataSet and memory
• How to identify the acutal row ?