473,246 Members | 1,928 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 29613

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: milk-jam | last post by:
I'm trying to set my datagridview so that the first row will be left blank and to use it as a filtering filed for the datagridview. Until now I was using 2 datagridview the upper one with a header...
0
by: inpuarg | last post by:
Is it possible to sort datagridview programmatically in virtualmode ? ..Sort() method isn 't working ?
1
by: sapkal.manish | last post by:
Question : How can I find actual row position of DataTable in DataGridView when DataTable is sorted / My source code like this Private WithEvent dgvInwDet as new DataGridView Private...
4
by: Matt | last post by:
I have been searching all over the web for a way to sort a DataGridView based on the actual text being shown in a ComboBox column as opposed to the underlying value (an ID in this case). Can anyone...
3
by: joey.powell | last post by:
I have a datagridview where I set its "DataSource" property to a DataSet and its "DataMember" property to a table within the DataSet. I need for the DataGridView to "sort" through the contents of...
2
by: Rick | last post by:
VS 2005/Pro I'm using a DataGridView in a Windows form and allowing the user to sort the columns by clicking on the header column. Is there some method offered by the grid to return back to...
2
by: Claes Wedin | last post by:
Hi, We have a DataGridView and the user wants to be able to change the order on the rows by drag and drop. The best would be if a thick line showed where the row will be placed when moving over...
3
by: Frederik | last post by:
Hi group, I have a DataGridView that is mainly consisting of data bound columns. One column however is unbound. I'm not able to sort the grid on this column! Is that normal behaviour? If so,...
3
by: frenzy99 | last post by:
Can some one help me with this problem. I want to sort DataGridView based on the order in which values were added, not on the basis of any particular column, unlike the functionality provided by...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.