DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns
DataGridView sort by two columns DataGridView sort by two columns
DataGridView sort by two columns
Go Back  Xtreme Visual Basic Talk > > > DataGridView sort by two columns


Reply
 
Thread Tools Display Modes
  #1  
Old 08-02-2007, 12:28 PM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default DataGridView sort by two columns


I've been trying to figure out how to sort two columns in the DataGridView and I'm having a little trouble finding any information on it. I pull some data into my datagridview using a stored procedure and I want the user to be able have two options; Sort By Item Number, and Sort by Line Number... but the Sort by Line Number has to have two columns sorted ascending. There's a Line Number field and a Sub Line Number field so I want to sort Line Number by ascending then sort the Sub Line Number ascending. I want to do this all in the datagridview without going back to the database. Any suggestions on this?
Reply With Quote
  #2  
Old 08-02-2007, 02:01 PM
gazmac's Avatar
gazmac gazmac is offline
Junior Contributor
 
Join Date: Apr 2003
Posts: 200
Default

Are you asking how to sort a DataGridView by two factors? For instance, telling a group of people to stand in order of both height and weight?
Reply With Quote
  #3  
Old 08-02-2007, 02:33 PM
Gruff's Avatar
GruffDataGridView sort by two columns Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

You can do multiple column sorts but you do it in the dataview object that feeds your datagridview control. Look up the DataView.Sort Property

It accepts a string of <Column Name> <Space> <Direction>.
Where Directiion = "ASC" or "DESC"
Multiple columns and directions are separated by commas

Example: DataView.Sort = "Height ASC, Weight ASC, Age ASC"

Works quickly and cleanly.

~T
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #4  
Old 08-03-2007, 08:02 AM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default

wow that works great... Here's the code I used for anyone interested...

Code:
   Dim test As DataTable = New DataTable("Test")

        Dim cmdSelectAll As New SqlClient.SqlCommand("GetProjectNumberParts", connStr)

        cmdSelectAll.CommandType = CommandType.StoredProcedure

        cmdSelectAll.Parameters.Add("@selectedOrder", SqlDbType.NChar, 10)
        cmdSelectAll.Parameters("@selectedOrder").Value = lbProjectNumbers.Text

        connStr.Open()

        test.Load(cmdSelectAll.ExecuteReader(CommandBehavior.CloseConnection))

        Dim dv As New DataView(test)
        dv.Sort = "Line Number (Wpo03/Obponr) , Line Suffix (Wpo02/Obposx) ASC"
        dgMain.DataSource = dv
Reply With Quote
  #5  
Old 08-03-2007, 09:10 AM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default

I have the code above and it works great but I need it to be more flexible.

What I'm trying to do is this.
I have a datatable populated from a Stored Procedure, but there are other columns I want added to the datatable. These will be columns that I fill it with data and not data from the database. How can I add a column to the middle of a datatable. If this is my datatable now...

Item Number, Qty, Description

How do I add a column to the middle like...

Item Number, Qty, New Column, Description
Reply With Quote
  #6  
Old 08-03-2007, 02:21 PM
Gruff's Avatar
GruffDataGridView sort by two columns Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

You manipulate the DataTable object's .Columns.Add() method.

With all due respect Mike you should be looking this stuff up in the visual studio Help. The information is in there.

Code:
Option Explicit Private Sub AddStuff() Dim Conn As Odbc.OdbcConnection Dim cmd As Odbc.OdbcCommand Dim DA As OdbcDataAdapter Dim DT As DataTable Dim DV1 As DataView Dim oRow1 As DataRow Dim sQuery As String = "SELECT * FROM Blah Blah Blah" Dim i As Integer ' I was using an ODBC connection for my project. ' Problably not what you are using. Conn = New Odbc.OdbcConnection(MAS200_CONNECTION_STR) Conn.Open() DA = New OdbcDataAdapter cmd = Conn.CreateCommand() cmd.CommandText = sQuery cmd.CommandTimeout = 30 DT = New DataTable DA.SelectCommand = cmd DA.Fill (DT) 'Add new columns to the existing Datatable. 'Here I am using datatypes already defined in another DataTable. DT.Columns.Add("PromiseDate", dv_LineDate.Table.Columns("PromiseDate").DataType) DT.Columns.Add("QtyOrdered", dv_LineDate.Table.Columns("QtyOrdered").DataType) DT.Columns.Add("QtyShipped", dv_LineDate.Table.Columns("QtyShipped").DataType) ' Add data to new columns i = DT.Rows.Count For Each oRow1 In DT.Rows ' Assumes you have the array of user data type (UDT) called myUDT_Array ' of the same size as the number of records returned oRow1("PromiseDate") = myUDT_Array(i).Pdate oRow1("QtyOrdered") = myUDT_Array(i).QOrder oRow1("QtyShipped") = myUDT_Array(i).QShip End If Next oRow1 DV1 = New DataView DV1 = DT.DefaultView 'Clean up objects End Sub

As far as making the sort more flexible. You can make a panel similar to that shown in the attached Picture.
The buttons on the left are toggles. They show DESC when pressed in. The listboxes each hold the list of columns from the dataviewgrid. A small bit of code disallows the same column being selected more than once. When the 'Apply' button is pressed the sort string is built from the settings. This gives the user up to four column of his choice to sort on.


~T
Attached Images
File Type: jpg SortUI.JPG (6.1 KB, 8 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 08-03-2007 at 05:35 PM.
Reply With Quote
  #7  
Old 08-06-2007, 06:10 AM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default

Thanks for the help... You're right, I should be using the help and I do most of time. I like hearing from other programmers since I'm the only one here at work.
Reply With Quote
  #8  
Old 08-06-2007, 09:37 AM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default

I've been reading the help section and I'm not making any progress on this...

I have a data table that is populated from the database. I have a data grid that is populated by the data view because I need sorting capabilities. The next thing I need to do which I'm having some trouble with is that I need to be able to add a few columns to the view. I need 2 checkbox columns, 1 button column and 3 textbox columns. I need 1 textbox column populated with the same data from another column, 1 textbox column populated with all 1's, and the other textbox column populated with data from another column but I have to use a string function mid() to get information I need from a cell. My questions are...

1. What is the best way to add these columns? Should I add the textbox columns directly into the DataTable and the control columns to the Data grid?

2. If I add data to a column this way...

Code:
        Dim addtest As New DataGridViewTextBoxColumn
        addtest.HeaderText = "Utest"
        addtest.DisplayIndex = 4
        dgMain.Columns.Add(addtest)
        Dim cell As DataGridViewCell = New DataGridViewTextBoxCell
        addtest.CellTemplate = cell

        Dim i As Integer
        For i = 0 To dgMain.Rows.Count - 1
            dgMain.Rows(i).Cells(4).Value = i
        Next
I have a problem accessing the data column I created.

Code:
 dgMain.Rows(i).Cells(4).Value = i
This code doesn't allow me to add information into the column I want. It skips over the column I created like it's not even there. How do I add information into my created column?
Reply With Quote
  #9  
Old 08-06-2007, 09:59 AM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default

Ok, I made some progess with the expression property

Code:
 addUQty.Expression = "IIF(TRIM([Alias Number]) = '', SUBSTRING([Item Number], 2, LEN([Item Number])), [Alias Number])"
Reply With Quote
  #10  
Old 08-06-2007, 01:21 PM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default

Here's some code I have to create a column with the same information as another column from the database. The reason why I'm doing this is to show the user the quantity from the database and have the user edit the new column to change the quantity. I have it as an expression column because that's the only way I knew how to copy one column to the next. I can't edit this column now. Any suggestions on what I can do here?


Code:
Dim addUQty As DataColumn
            addUQty = New DataColumn
            addUQty.DataType = System.Type.GetType("System.String")
            addUQty.ColumnName = "UQty"
            addUQty.Expression = "[Ordered Quantity - Basic U/M] * 1"
            dtMain.Columns.Add(addUQty)
Reply With Quote
  #11  
Old 08-07-2007, 08:36 AM
vbMike vbMike is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 258
Default

I finally have it... here's the code for anyone interested...

Code:
            For i = 0 To dgMain.Rows.Count - 1
                dgMain.Rows(i).Cells(2).Value = dgMain.Rows(i).Cells(1).Value
            Next
I used this code after I created the DataView.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns DataGridView sort by two columns
DataGridView sort by two columns
DataGridView sort by two columns
 
DataGridView sort by two columns
DataGridView sort by two columns
 
-->