Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB

I am having two Forms with one Datagridview in each.

Form1
Datagridview1 including columns:
Phase including cells: a,b,c
Order including cells: 3,1,2

Form2
Datagridview2 including columns:
Phase including cells: a,b,c which are picked from Datagridview1

The problem is how to sort Datagridview2 so that Phase order in it will be picked from Datagridview1? So what I would like to see in Datagrid2 is:

Phase rows in order: b,c,a

What I have tried:

Here's my code to give more deeply overview of the problem.
*) The problem is here:
CYCLE_TIMESDataGridView.Columns("Process Phase") is picked as Combobox cell value and is like "Assembly phase" but ProcessPhasesNokiaBindingSource.Sort("Basic Process") is number like 1,2...defining the Phase orders in ProcessPhasesBindingSource.
___

VB
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
       ' Filling Datagrid by user given selections and colouring the Phases by defined colours by function
       If cheProduct.Checked = True Or cheOwner.Checked = True Or CheArea.Checked = True Or CheLine.Checked = True Or CheAll.Checked = True Then
           Try
               If cheProduct.Checked = True Then ' Filling Datagrid by selected Product data
                   Me.CYCLE_TIMESTableAdapter.FillByProduct(Me.ProductDataDataSet1.CYCLE_TIMES, Product_NokiaComboBox.SelectedValue)
                   Call Phase_colors()
               ElseIf cheOwner.Checked = True Then ' Filling Datagrid by selected Owner data
                   Me.CYCLE_TIMESTableAdapter.FillByOwner(Me.ProductDataDataSet1.CYCLE_TIMES, Owner_ComboBox.SelectedValue)
                   Call Phase_colors()
               ElseIf CheArea.Checked = True Then ' Filling Datagrid by selected Area data
                   Me.CYCLE_TIMESTableAdapter.FillByArea(Me.ProductDataDataSet1.CYCLE_TIMES, Area_ComboBox.SelectedValue)
                   Call Phase_colors()
               ElseIf CheLine.Checked = True Then ' Filling Datagrid by selected Line data
                   Me.CYCLE_TIMESTableAdapter.FillByLine(Me.ProductDataDataSet1.CYCLE_TIMES, Line_ComboBox.SelectedValue)
                   Call Phase_colors()
               ElseIf CheAll.Checked = True Then ' Filling Datagrid by all data
                   Me.CYCLE_TIMESTableAdapter.Fill(Me.ProductDataDataSet1.CYCLE_TIMES)
                   Call Phase_colors()
               End If
           Catch ex As Exception
               ex.Message.ToString()
           End Try
       End If
       ' Sorting the Datagrid view Process Phase column by different orders defined by columns cell numbering in ProcesshasesNokiaBindingSource.
       If CheckBasic.Checked = True Then ' Selecting the column for picking the cell numbering used for sorting the process phase in Datagridview. *)
           Me.CYCLE_TIMESDataGridView.Sort(CYCLE_TIMESDataGridView.Columns("Process Phase"), ProcessPhasesNokiaBindingSource.Sort("Basic Process") + "asc")
       ElseIf CheckManual.Checked = True Then
           Me.CYCLE_TIMESDataGridView.Sort(CYCLE_TIMESDataGridView.Columns("Process Phase"), ProcessPhasesNokiaBindingSource.Sort("Rel5 Manual") + "asc")
       ElseIf CheckSemiauto.Checked = True Then
           Me.CYCLE_TIMESDataGridView.Sort(CYCLE_TIMESDataGridView.Columns("Process Phase"), ProcessPhasesNokiaBindingSource.Sort("Rel5 Filter Semiauto") + "asc")
       ElseIf CheckAuto.Checked = True Then
           Me.CYCLE_TIMESDataGridView.Sort(CYCLE_TIMESDataGridView.Columns("Process Phase"), ProcessPhasesNokiaBindingSource.Sort("Rel5 Filter Full Auto") + "asc")
       End If
   End Sub
Posted
Updated 24-Mar-16 19:14pm
v3
Comments
Sascha Lefèvre 21-Feb-16 15:11pm    
If you've tried everything and nothing worked, there can't be a solution to this.

But seriously: The section "What I have tried" is meant to show us what you have tried. In this case that would mean showing us your code which you have tried.
Member 12111641 22-Feb-16 3:40am    
Good comment. I'll make my problem definition more concrete.
CHill60 21-Feb-16 16:00pm    
Member 12111641 22-Feb-16 3:43am    
Just read. Understood hope the point! Not to make public forms. I sure have not done anything against this rule yet. :)

The DataGridView can be sorted via 2 methods;
a) Use DataGridView.Sort(DataGridViewColumn, SortDirection) method. This will limit you to sorting via a single column
b) If you are binding to a DataTable, you can either sort the DataTable, or create a DataView of the DataTable, that is sorted to what you require. This will allow you to sort by multiple columns.
To sort a DataTable you can use either of the following;
VB
Dim myView As DataView = origDataTable.DefaultView
myView.Sort = "Phase ASC, Order DESC"

OR
VB
Dim mySorted As DataTable = origDataTable.Select("Phase IS NOT NULL", "Phase ASC").CopyToDataTable


Kind Regards
 
Share this answer
 
Hello,

C#
Hello, in a meantime while waiting help I managed to solve the problem by me own as follows after heavy brainstorming.

My final solution with the problem was:

1. I added (hidden) Datagrid2 into the same Form where's the Datagrid1.
2. Created OrderID column into Datagrid2.
3. Created a function which picked the Datagrid1 Phase order from Datagrid2 into OrderID column.
4. Sorted the Datagrid1 Phase column by OrderID.

PS. Thanks for the tips - but maybe you did not understood the problem as this is bit upnormal sorting case. Anyway I added my solution here if someone meets similiar problem in their own challenges.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900