473,321 Members | 1,778 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,321 software developers and data experts.

Battles with sorting DataGrid control.

Can anyone please tell me what I'm doing wrong here.
I have a Windows Form with a DataGrid on it, and I'm having real problems
with the Sorting.

It is easy to reproduce the problem I have.

If you create a new Windows Application
Put a DataGrid control on it.

Put the following in the onload event of the form:

private void Form1_Load(object sender, System.EventArgs e)
{
DataGridTableStyle dgts = new DataGridTableStyle();
DataGridTextBoxColumn dgtbc1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn dgtbc2 = new DataGridTextBoxColumn();

dgtbc1.Width = 300;
dgtbc1.HeaderText = "Header1";
dgts.GridColumnStyles.Add(dgtbc1);

dgtbc2.Width = 300;
dgtbc2.HeaderText = "Header2";
dgts.GridColumnStyles.Add(dgtbc2);

dataGrid1.TableStyles.Add(dgts);

DataTable dt = new DataTable("SuperTable");
dt.Columns.Add("One");
dt.Columns.Add("Two");

dataGrid1.DataSource = dt;
}

So by my understanding, I have created a tableStyle and added a couple of
textBoxColumns to it, then applied that style to my datagrid.
I have then created a DataTable, and made that the dataSource of my
dataTable.

If you run the app you should get an empty grid.

In Column1 put the letters a-h
In Column2 put the numbers 1-8
Click on the header of Column1 a couple of times so that the letters are
sorted a-h
Click on the 5 in Column2 and change it to 100.
Try clicking on the header of Column2 to sort the numbers correctly.
Why doesn't that work? The row with '100' in it stays in position, doesn't
get re-sorted.

Note: If you click on a few cells in the table, then it seems to sort itself
out,
but how can I re-produce that in code?

I'm sure that I've done somthing dumb, but I can't see it.
If anyone can cast light on this, I will be most grateful

Regards,

Chris.

Nov 16 '05 #1
2 994
Chris,

When you type the 100, and then click the header of the grid, I noticed
that the edit mode does not change. This would indicate that the value
hasn't been written to the underlying data table yet, which would explain
why the sort doesn't occur. When you move off the cell, the value is
committed, and then the sort is applied correctly.

In order to commit the edit in code, you have to find the
BindingManagerBase for the data binding (you can pass the data table that
the grid is bound to to the BindingContext property of the data grid to get
the binding manager base). Once you have that, call EndCurrentEdit on the
BindingManagerBase, and the value should be committed to the data table, and
the sort applied.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Can anyone please tell me what I'm doing wrong here.
I have a Windows Form with a DataGrid on it, and I'm having real problems
with the Sorting.

It is easy to reproduce the problem I have.

If you create a new Windows Application
Put a DataGrid control on it.

Put the following in the onload event of the form:

private void Form1_Load(object sender, System.EventArgs e)
{
DataGridTableStyle dgts = new DataGridTableStyle();
DataGridTextBoxColumn dgtbc1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn dgtbc2 = new DataGridTextBoxColumn();

dgtbc1.Width = 300;
dgtbc1.HeaderText = "Header1";
dgts.GridColumnStyles.Add(dgtbc1);

dgtbc2.Width = 300;
dgtbc2.HeaderText = "Header2";
dgts.GridColumnStyles.Add(dgtbc2);

dataGrid1.TableStyles.Add(dgts);

DataTable dt = new DataTable("SuperTable");
dt.Columns.Add("One");
dt.Columns.Add("Two");

dataGrid1.DataSource = dt;
}

So by my understanding, I have created a tableStyle and added a couple of
textBoxColumns to it, then applied that style to my datagrid.
I have then created a DataTable, and made that the dataSource of my
dataTable.

If you run the app you should get an empty grid.

In Column1 put the letters a-h
In Column2 put the numbers 1-8
Click on the header of Column1 a couple of times so that the letters are
sorted a-h
Click on the 5 in Column2 and change it to 100.
Try clicking on the header of Column2 to sort the numbers correctly.
Why doesn't that work? The row with '100' in it stays in position, doesn't get re-sorted.

Note: If you click on a few cells in the table, then it seems to sort itself out,
but how can I re-produce that in code?

I'm sure that I've done somthing dumb, but I can't see it.
If anyone can cast light on this, I will be most grateful

Regards,

Chris.

Nov 16 '05 #2
Nicholas,

Thank You 1000 times!

I had guessed that it was probably somthing to do with the data not getting
written back to the DataTable (In fact a DataView of a DataTable) but I had
no idea how to force it through. I have seen BindingContext in other places,
but until now, I haven't really understood what it meant.

EndCurrentEdit works great, now my DataGrid does what I was expecting it to.
For anyone else with this problem:
dataGrid1.BindingContext[dataGrid1.DataSource].EndCurrentEdit();

In my actual app, values in the grid are being changed by code, rather than
being typed in, but I assume that the pricinple is similar. Your proposed
solution certainly seems to work for me anyway.

Once again thanks, you won't believe how crazy this has been driving me!

ChrisM

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OW**************@TK2MSFTNGP09.phx.gbl...
Chris,

When you type the 100, and then click the header of the grid, I noticed that the edit mode does not change. This would indicate that the value
hasn't been written to the underlying data table yet, which would explain
why the sort doesn't occur. When you move off the cell, the value is
committed, and then the sort is applied correctly.

In order to commit the edit in code, you have to find the
BindingManagerBase for the data binding (you can pass the data table that
the grid is bound to to the BindingContext property of the data grid to get the binding manager base). Once you have that, call EndCurrentEdit on the
BindingManagerBase, and the value should be committed to the data table, and the sort applied.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Can anyone please tell me what I'm doing wrong here.
I have a Windows Form with a DataGrid on it, and I'm having real problems with the Sorting.

It is easy to reproduce the problem I have.

If you create a new Windows Application
Put a DataGrid control on it.

Put the following in the onload event of the form:

private void Form1_Load(object sender, System.EventArgs e)
{
DataGridTableStyle dgts = new DataGridTableStyle();
DataGridTextBoxColumn dgtbc1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn dgtbc2 = new DataGridTextBoxColumn();

dgtbc1.Width = 300;
dgtbc1.HeaderText = "Header1";
dgts.GridColumnStyles.Add(dgtbc1);

dgtbc2.Width = 300;
dgtbc2.HeaderText = "Header2";
dgts.GridColumnStyles.Add(dgtbc2);

dataGrid1.TableStyles.Add(dgts);

DataTable dt = new DataTable("SuperTable");
dt.Columns.Add("One");
dt.Columns.Add("Two");

dataGrid1.DataSource = dt;
}

So by my understanding, I have created a tableStyle and added a couple of textBoxColumns to it, then applied that style to my datagrid.
I have then created a DataTable, and made that the dataSource of my
dataTable.

If you run the app you should get an empty grid.

In Column1 put the letters a-h
In Column2 put the numbers 1-8
Click on the header of Column1 a couple of times so that the letters are
sorted a-h
Click on the 5 in Column2 and change it to 100.
Try clicking on the header of Column2 to sort the numbers correctly.
Why doesn't that work? The row with '100' in it stays in position,

doesn't
get re-sorted.

Note: If you click on a few cells in the table, then it seems to sort

itself
out,
but how can I re-produce that in code?

I'm sure that I've done somthing dumb, but I can't see it.
If anyone can cast light on this, I will be most grateful

Regards,

Chris.


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Billy Jacobs | last post by:
I have a datagrid control in a web application in which I have both paging and sorting enabled. The sorting works only on the current page. If I am on page 1 and sort by any field, the page...
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
2
by: ChrisM | last post by:
Can anyone please tell me what I'm doing wrong here. I have a Windows Form with a DataGrid on it, and I'm having real problems with the Sorting. It is easy to reproduce the problem I have. If...
2
by: Raj | last post by:
Hi, When we are sorting the DataGrid Boolean column the grid is becoming redcross. I have my own PPMIPDataGridBoolColumn class inherited from System.Windows.Forms.DataGridBoolColumn. In this...
1
by: Jeremy | last post by:
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current...
0
by: Dheeraj Verma via DotNetMonster.com | last post by:
Hi all, I have a asp data grid under which i have another datagrid which is my user control and is autogenerated.Now I am not able to implement sorting in my inner datagrid eventhough I have...
0
by: Dheeraj Verma via DotNetMonster.com | last post by:
Hi all, I have a asp data grid under which i have another datagrid which is my user control and is autogenerated.Now I am not able to implement sorting in my inner datagrid eventhough I have...
2
by: Lars Netzel | last post by:
I have a Datagrid using the Edit/Update mode to alter data in the datasource. I needed to have a bit more flexible interface in the editmode with a design that couldn't work in the columns. So I...
8
by: simchajoy2000 | last post by:
I thought the only thing I had to do to disable column sorting in VB.NET was to set datagrid.AllowSorting = False. Unfortunately this has never worked for me. I discovered another set of code...
3
by: Mark Gilkes | last post by:
Hi, I have an aspx page in which I am building a DataGrid control in the code-behind dynamically. The DataGrid is declared along with BoundColumns and bound to the datasource, then added to a...
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
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.