471,071 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

how to sort datagridview programmically

tee
Hi,

How do i sort datagridview programmically, current i have the following code
but i keep get the error saying "DataGridView control must be bound to an
IBindingList object to be sorted.", What is IBindingList and how would i go
about using it.

Here are my code

private void dataGridView1_ColumnHeaderMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridViewColumn newColumn =
dataGridView1.Columns[e.ColumnIndex];

SortOrder direction;
if (dataGridView1.SortOrder == SortOrder.Ascending)
{
dataGridView1.Sort(newColumn,
ListSortDirection.Ascending);
direction = SortOrder.Ascending;
}
else
{
dataGridView1.Sort(newColumn,
ListSortDirection.Descending);
direction = SortOrder.Descending;
}

newColumn.HeaderCell.SortGlyphDirection = direction;

}
else
{
MessageBox.Show("right");
}
}
Many many thanks in advances..... :)

Jan 19 '07 #1
2 29357

You might be better off binding your grid to a DataView, it offers very
easy sorting and filtering. You might also want to do this based on the
columns datafield rather than index. I use a third part grid mostly, so
I can't help you with exact code but this is one possible solution for
you. I believe you might be able to access the default dataview for your
dataset without explicitly creating a dataview and binding your grid to
it.

Bob
-----Original Message-----
From: tee [mailto:th******@hotmail.com]
Posted At: Friday, January 19, 2007 1:25 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: how to sort datagridview programmically
Subject: how to sort datagridview programmically

Hi,

How do i sort datagridview programmically, current i have the following
code
but i keep get the error saying "DataGridView control must be bound to
an
IBindingList object to be sorted.", What is IBindingList and how would
i go
about using it.

Here are my code

private void dataGridView1_ColumnHeaderMouseClick(object sender,

DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridViewColumn newColumn =
dataGridView1.Columns[e.ColumnIndex];

SortOrder direction;
if (dataGridView1.SortOrder == SortOrder.Ascending)
{
dataGridView1.Sort(newColumn,
ListSortDirection.Ascending);
direction = SortOrder.Ascending;
}
else
{
dataGridView1.Sort(newColumn,
ListSortDirection.Descending);
direction = SortOrder.Descending;
}

newColumn.HeaderCell.SortGlyphDirection = direction;

}
else
{
MessageBox.Show("right");
}
}
Many many thanks in advances..... :)

Jan 19 '07 #2
Brian Noyes has an example in his book (Data Binding with Windows
Forms 2.0) of binding a list of business objects to a DataGridView
and then adding sorting and filtering to it. Check out chapter 9.

Is it kosher to post the site for the sample code that goes with
the book?

Robin S.
-----------------------------------------
"tee" <th******@hotmail.comwrote in message
news:%2***************@TK2MSFTNGP04.phx.gbl...
Hi,

How do i sort datagridview programmically, current i have the
following code but i keep get the error saying "DataGridView control
must be bound to an IBindingList object to be sorted.", What is
IBindingList and how would i go about using it.

Here are my code

private void dataGridView1_ColumnHeaderMouseClick(object
sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridViewColumn newColumn =
dataGridView1.Columns[e.ColumnIndex];

SortOrder direction;
if (dataGridView1.SortOrder == SortOrder.Ascending)
{
dataGridView1.Sort(newColumn,
ListSortDirection.Ascending);
direction = SortOrder.Ascending;
}
else
{
dataGridView1.Sort(newColumn,
ListSortDirection.Descending);
direction = SortOrder.Descending;
}

newColumn.HeaderCell.SortGlyphDirection = direction;

}
else
{
MessageBox.Show("right");
}
}
Many many thanks in advances..... :)

Jan 20 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by milk-jam | last post: by
1 post views Thread by sapkal.manish | last post: by
3 posts views Thread by joey.powell | last post: by
2 posts views Thread by Claes Wedin | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.