473,652 Members | 3,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(obje ct sender, System.EventArg s e)
{
DataGridTableSt yle dgts = new DataGridTableSt yle();
DataGridTextBox Column dgtbc1 = new DataGridTextBox Column();
DataGridTextBox Column dgtbc2 = new DataGridTextBox Column();

dgtbc1.Width = 300;
dgtbc1.HeaderTe xt = "Header1";
dgts.GridColumn Styles.Add(dgtb c1);

dgtbc2.Width = 300;
dgtbc2.HeaderTe xt = "Header2";
dgts.GridColumn Styles.Add(dgtb c2);

dataGrid1.Table Styles.Add(dgts );

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

dataGrid1.DataS ource = 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 1020
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
BindingManagerB ase 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
BindingManagerB ase, 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.co m

"ChrisM" <hi****@AskMeIf YouWantIt.com> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
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(obje ct sender, System.EventArg s e)
{
DataGridTableSt yle dgts = new DataGridTableSt yle();
DataGridTextBox Column dgtbc1 = new DataGridTextBox Column();
DataGridTextBox Column dgtbc2 = new DataGridTextBox Column();

dgtbc1.Width = 300;
dgtbc1.HeaderTe xt = "Header1";
dgts.GridColumn Styles.Add(dgtb c1);

dgtbc2.Width = 300;
dgtbc2.HeaderTe xt = "Header2";
dgts.GridColumn Styles.Add(dgtb c2);

dataGrid1.Table Styles.Add(dgts );

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

dataGrid1.DataS ource = 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.Bindi ngContext[dataGrid1.DataS ource].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.c om> wrote in
message news:OW******** ******@TK2MSFTN GP09.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
BindingManagerB ase 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
BindingManagerB ase, 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.co m

"ChrisM" <hi****@AskMeIf YouWantIt.com> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
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(obje ct sender, System.EventArg s e)
{
DataGridTableSt yle dgts = new DataGridTableSt yle();
DataGridTextBox Column dgtbc1 = new DataGridTextBox Column();
DataGridTextBox Column dgtbc2 = new DataGridTextBox Column();

dgtbc1.Width = 300;
dgtbc1.HeaderTe xt = "Header1";
dgts.GridColumn Styles.Add(dgtb c1);

dgtbc2.Width = 300;
dgtbc2.HeaderTe xt = "Header2";
dgts.GridColumn Styles.Add(dgtb c2);

dataGrid1.Table Styles.Add(dgts );

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

dataGrid1.DataS ource = 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
2364
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 displays the records sorted properly. If I go to the next page the records on that page are not sorted. The datagrid is bound to a dataview. I set the sort on the dataview in the sort event.
2
945
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 datagrid. HTML Datagrid1 TemplateColumn Table Header information Detail Information
2
368
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 you create a new Windows Application Put a DataGrid control on it. Put the following in the onload event of the form:
2
2351
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 inherited class I have one DataGridValueChangedEventHandler event handler. I am overriding Edit() to know the current state of the cell. I am overriding Paint() to know the if the user has been editing the cell. I am also overriding the Commit() to
1
2348
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 page. i.e. If I have: IntegerValue StringValue CurrencyValue 0 Item 0 0 1 Item 1 1.23
0
960
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 enabled sorting.even the item command is not firing for the inner grid.I am binding the datagrid onPrerender. <ASP:DATAGRID id="MainDataGrid" Runat="server" width="100%" AutoGenerateColumns="False" BorderWidth="0"> <COLUMNS> <ASP:TEMPLATECOLUMN>...
0
1204
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 enabled sorting.even the item command is not firing for the inner grid.I am binding the datagrid onPrerender. <ASP:DATAGRID id="MainDataGrid" Runat="server" width="100%" AutoGenerateColumns="False" BorderWidth="0"> <COLUMNS> <ASP:TEMPLATECOLUMN>...
2
1309
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 fixed this by only having ONE template column (plus the edit button column) and then have html tables in both the header and the edit items to have complete control over the design. This works fine. Now my boss wants Sorting in the column...
8
15161
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 that seems to work for 99% of the cases where I need to disable datagrid column sorting: Private Sub datagrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagrid.MouseDown Dim pt As New Point(e.X,...
3
3767
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 panel control in the OnInit method of the aspx Page. I am trying to implement a bidirectional sorting. I have a SortCommand method which fires OK. I have followed the code example given on the 4GuysFromRolla site, which reads the value from...
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.