how to sort datagridview programmically

T

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..... :)
 
B

Bob Graham

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:[email protected]]
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..... :)
 
R

RobinS

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top