473,387 Members | 1,745 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,387 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 29632

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.