C# .NET - C# - Sort a DataGridView by two columns

Asked By Aldo Liaks
14-Jul-08 05:53 AM

Hi guys,

 

my dataGridView has the following columns:

 

column 1   column2      column3

1                    group A        red

2                    group B        blue

3                    group A        blue

4                    group A        blue

5                    group B        red

6                    group B        blue

7                    group B        red

8                    group A        red

 

I need to sort by column2 and then by column3

In order to get:

 

column1    column2    column3

3                  groupA       blue

4                  groupA       blue

1                  groupA       red

8                  groupA       red

2                  groupB       blue

6                  groupB       blue

5                  groupB       red

7                  groupB       red

 

How can I do that?

 

Thanks,

Aldo.

try this...  try this...

14-Jul-08 06:01 AM

Hi,

try the below code to sort using mutplie columns...

Dataset ds = (Dataset) DataGridView1.DataSource;

DataView x = ds.Tables[0].DefaultView;

x.Sort = "Column2, Colmun3 asc";

DataGridView1.DataSource = ds;

Use DataView for that  Use DataView for that

14-Jul-08 06:35 AM

and the sort proprty of dataview.

By using SORT you can achive this like;

DataView dv = ds.Tables[0].DefaultView;
dv.Sort =
"col2,col3 asc"; // for ascending
dv.Sort = "col2,col3  desc"; // for descending
GridView1.DataSource = dv;
GridView1.Databind();

See these links to know more abt sort of dataview;

http://www.developerfusion.co.uk/show/4686/

http://davidhayden.com/blog/dave/archive/2006/02/11/2798.aspx

http://www.dotnetjohn.com/articles.aspx?articleid=77

http://www.asp101.com/articles/jayram/multicolumnsort/default.asp

Best Luck!!!!!!!!!!!!
Sujit.

Try this code using DataView  Try this code using DataView

14-Jul-08 07:21 AM

You can make use of DataView and sort property for this..

Try the below code

DataView myData = ds.Tables[0].DefaultView;
myData.Sort = "column2,column3 asc";

GridView1.DataSource = myData;
GridView1.Databind();

exeption  exeption
14-Jul-08 09:19 AM

Guys,

I am using

void SortDGVby2Col(DataGridView dgvToSort, string col1, string col2, string order)

{

DataSet ds = (DataSet)dgvToSort.DataSource;

DataView dv = ds.Tables[0].DefaultView;

dv.Sort = col1 + ", " + col2 + order;

dgvToSort.DataSource = ds;

//dgvToSort.Databind();

}

but getting exception because dgvToSort.DataSource is null.

Could that be because I added the data programmatically?

What should I change?

Thanks.

Check where u fill DS there are may be not prperly fill  Check where u fill DS there are may be not prperly fill
15-Jul-08 12:33 AM
Check where u fill DS there are may be not prperly fill  thats why they giving error like dgvToSort.DataSource is null.
Create New Account
help
Sort DataGridView Respected Sir How to sort datagridview data using vb.net source code? ans plz. hi, use this code DGV.Sort(DGV.Columns(0), System.ComponentModel.ListSortDirection.Ascending) 'Where DGV is a DataGridView hi, check the below code <asp:GridView ID = "TaskGridView" runat = "server" AllowSorting = "true" OnSorting = "TaskGridView_Sorting Tables(0).DefaultView If ( Not String .IsNullOrEmpty(Expression)) Then If (SortDirection.ToUpper() = "ASC" ) Then SortDirection = "DESC" Else SortDirection = "ASC" End If SortableView.Sort = Expression & " " & SortDirection End If Catch ex As Exception Throw ex End Try Return SortableView End Function Protected Sub grdSortTest_Sorting( ByVal sender As Object set the sorting expression opposite to Viewstate as if its Asc we will make it Desc let me know thxs When you bind your DataGridView to your datasource, use a bindingsource. Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers() m_CustomersGrid.DataSource = m_CustomersBindingSource
SelectedRow vs. sortieren DataGridView .NET Framework Hallo NG, 1. Frage: - -- -- -- -- ich habe ein Problem mit dem Sortieren eines DGV denn sie wurde gelöscht). Dabei soll ebensfalls Rücksicht auf die zuvor eingestellte Sortierform (Asc. oder Desc.) genommen werden sowie auf die entsprechende (vor dem DataSourceChanged) sortierte Spalte. Wie kann man das lösen? Viele Grüße Maik VB.NET - German Discussions DGVC.DataPropertyName (1) DataGridViewCellCancelEventArgs (1) DataGridView.DataSource (1) DataGrid.DataSource (1) MyBase.SortedColumn (1) MyBase.DataSource (1) DataRowView (1) System.EventArgs diesem Fall ein DataView-Objekt, welches implizit erzeugt worden ist und das Bindeglied zwischen Deinem DataGridView und dem eigentlichen Datenspeicher, der DataTable dient. Diese DataView ist vereinfacht ausgedrückt eine Liste von de -> Visual Basic -> VB.net gibt es noch eine ganze Reihe weiterer Beispiele mit dem DataGridView und auch mit dem älteren DataGrid. Datenquelle ist dabei meist eine DataTable und DataGridView.DataSource bzw. DataGrid.DataSource ist eine in diesen Beispielen explizit zur DataTable erzeugte DataView. Neben Position = RowPos End Sub Hier besteht das Problem, dass ich z.B. ein eigene abgeleitete DataGridView-Klasse habe, und der Code dafür dynamisch sein müsste. Wenn ich jetzt z.B. die Column(10) erweitern würde, dann müsste ich für jeden Case auch noch zusätzlich den Desc-Sortiervorgang abfangen, da sonst eine Exception ausgelöst wird, weil es z.B. bei bei
my front end i want to include my database and table using binding source and datagridview controls. Use this code to bind the database to the datagrid view Imports System.Data Connection SampleDataAdapter.SelectCommand = SampleCommand SampleDataAdapter.Fill(SampleSource) TableView = SampleSource.Tables(0).DefaultView Catch ex As Exception Throw ex End Try Return TableView End Function Private Sub Button1_Click( ByVal sender As System Click DataGridView1.DataSource = GetData() End Sub End Class Reference :- http: / / www.vbnettutorial.net / ?Id = 125&Desc = VB.Net-DataGridView Hi The DataGridView control supports the standard Windows Forms data binding model, so it will bind to a the location of your data. For more information about the data sources supported by the DataGridView control, see DataGridView Control Overview (Windows Forms) . There is extensive support for this task in Visual Studio. For more information, see How to: Bind Data to the Windows Forms DataGridView Control Using the Designer and How to: Bind Data to the Windows Forms DataGridView Control
size of each assembly thanks to the following CQL query: SELECT ASSEMBLIES ORDER BY NbILinstructions DESC, NbNamespaces DESC, NbTypes DESC, NbMethods DESC Name # IL instructions # Namespaces # Types # Methods System.Windows.Forms 560331 20 2214 27754 System.Web query to know where your biggest methods are: SELECT TOP 5 METHODS ORDER BY NbILinstructions DESC Name # IL instructions Full Name MDTransform(Uint32*, Uint32*, Byte*) 5294 System.Security.Cryptography.RIPEMD160Managed. MDTransform IF Count > 0 IN SELECT METHODS WHERE NbILinstructions > 200 AND ! NameLike "Generated" ORDER BY NbILinstructions DESC Or maybe, all generated methods are in dedicated assemblies, namespaces or types: WARN IF Count SELECT METHODS OUT OF NAMESPACES "MyApp.Generated1", "MyApp.Generated2" WHERE NbILinstructions > 200 ORDER BY NbILinstructions DESC Or maybe, you prefer to mention each one explicitly: WARN IF Count > 0 IN SELECT NbILinstructions > 200 AND !( NameIs "Method1(int32)" OR FullNameIs "MyApp.MyType.Method2(String)" ) ORDER BY NbILinstructions DESC You can also mix all these features in the same constraint: WARN IF Count > 0