I have a gridview on a winform and the datasource is a datatable, after I sort though the underlying datatable does not update and this makes other operations wrong. using the underlying datatable values and the row number for it instead of the visible data in the grid.
any assistance appreciated as this is a very late discovery.
I just want to add I am using the q3 2010 set grid control. I do not really have any extra sorting settings, other than allow sort true. My expectation when I hit the column header is that it will do a switches between asc and desc as default.
any assistance appreciated as this is a very late discovery.
I just want to add I am using the q3 2010 set grid control. I do not really have any extra sorting settings, other than allow sort true. My expectation when I hit the column header is that it will do a switches between asc and desc as default.
12 Answers, 1 is accepted
0
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 11:45 AM
Hello,
I have prepared a sample for you to show DataTable sorting and binding to RadGridView.
Designer File
Form1.cs
Hope you find that helpful
Richard
I have prepared a sample for you to show DataTable sorting and binding to RadGridView.
Designer File
namespace
RadControlsWinFormsApp2
{
partial
class
Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.IContainer components;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing && (components !=
null
))
{
components.Dispose();
}
base
.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void
InitializeComponent()
{
this
.radGridView1 =
new
Telerik.WinControls.UI.RadGridView();
this
.radButton1 =
new
Telerik.WinControls.UI.RadButton();
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
.radButton1)).BeginInit();
this
.SuspendLayout();
//
// radGridView1
//
this
.radGridView1.Location =
new
System.Drawing.Point(0, 0);
this
.radGridView1.Name =
"radGridView1"
;
this
.radGridView1.Size =
new
System.Drawing.Size(451, 308);
this
.radGridView1.TabIndex = 0;
this
.radGridView1.Text =
"radGridView1"
;
//
// radButton1
//
this
.radButton1.Location =
new
System.Drawing.Point(287, 330);
this
.radButton1.Name =
"radButton1"
;
this
.radButton1.Size =
new
System.Drawing.Size(130, 24);
this
.radButton1.TabIndex = 1;
this
.radButton1.Text =
"Sort Patient DESC"
;
this
.radButton1.Click +=
new
System.EventHandler(
this
.radButton1_Click);
//
// Form1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(448, 375);
this
.Controls.Add(
this
.radButton1);
this
.Controls.Add(
this
.radGridView1);
this
.Name =
"Form1"
;
this
.Text =
"Form1"
;
this
.Load +=
new
System.EventHandler(
this
.Form1_Load);
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
.radButton1)).EndInit();
this
.ResumeLayout(
false
);
}
#endregion
private
Telerik.WinControls.UI.RadGridView radGridView1;
private
Telerik.WinControls.UI.RadButton radButton1;
}
}
Form1.cs
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
RadControlsWinFormsApp2
{
public
partial
class
Form1 : Form
{
private
DataTable table;
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
this
.radGridView1.DataSource = GetTable();
}
DataTable GetTable()
{
table =
new
DataTable();
table.Columns.Add(
"Dosage"
,
typeof
(
int
));
table.Columns.Add(
"Drug"
,
typeof
(
string
));
table.Columns.Add(
"Patient"
,
typeof
(
string
));
table.Columns.Add(
"Date"
,
typeof
(DateTime));
table.Rows.Add(25,
"Indocin"
,
"David"
, DateTime.Now);
table.Rows.Add(50,
"Enebrel"
,
"Sam"
, DateTime.Now);
table.Rows.Add(10,
"Hydralazine"
,
"Christoff"
, DateTime.Now);
table.Rows.Add(21,
"Combivent"
,
"Janet"
, DateTime.Now);
table.Rows.Add(100,
"Dilantin"
,
"Melanie"
, DateTime.Now);
DataView dv =
new
DataView(table);
dv.Sort =
"Dosage Desc"
;
table = dv.ToTable();
return
table;
}
private
void
radButton1_Click(
object
sender, EventArgs e)
{
DataView dv =
new
DataView(table);
dv.Sort =
"Patient Desc"
;
table = dv.ToTable();
this
.radGridView1.DataSource = table;
}
}
}
Hope you find that helpful
Richard
0
regina
Top achievements
Rank 1
answered on 25 Feb 2011, 02:17 PM
Me.RadGridView_Documents.MasterTemplate.AllowAddNewRow = False
Me.RadGridView_Documents.MasterTemplate.AllowDeleteRow = False
GridViewCheckBoxColumn1.FieldName = "export"
GridViewCheckBoxColumn1.HeaderText = "Export"
GridViewCheckBoxColumn1.Name = "export"
GridViewCheckBoxColumn1.Width = 42
GridViewTextBoxColumn1.FieldName = "to_addressees"
GridViewTextBoxColumn1.HeaderText = "To"
GridViewTextBoxColumn1.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft
GridViewTextBoxColumn1.Name = "column_to_addressees"
GridViewTextBoxColumn1.ReadOnly = True
GridViewTextBoxColumn1.Width = 95
GridViewTextBoxColumn2.FieldName = "author"
GridViewTextBoxColumn2.HeaderText = "From"
GridViewTextBoxColumn2.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft
GridViewTextBoxColumn2.Name = "column_author"
GridViewTextBoxColumn2.ReadOnly = True
GridViewTextBoxColumn2.Width = 95
GridViewTextBoxColumn3.FieldName = "item"
GridViewTextBoxColumn3.HeaderText = "Description"
GridViewTextBoxColumn3.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft
GridViewTextBoxColumn3.Name = "column_item"
GridViewTextBoxColumn3.Width = 280
GridViewDateTimeColumn1.FieldName = "doc_date"
GridViewDateTimeColumn1.Format = System.Windows.Forms.DateTimePickerFormat.[Short]
GridViewDateTimeColumn1.FormatInfo = New System.Globalization.CultureInfo("")
GridViewDateTimeColumn1.FormatString = ""
GridViewDateTimeColumn1.HeaderText = "Document Date"
GridViewDateTimeColumn1.Name = "column_DocDate"
GridViewDateTimeColumn1.ReadOnly = True
GridViewDateTimeColumn1.Width = 95
GridViewTextBoxColumn4.FieldName = "document_num"
GridViewTextBoxColumn4.HeaderText = "Doc #"
GridViewTextBoxColumn4.IsVisible = False
GridViewTextBoxColumn4.Name = "column_document_num"
GridViewTextBoxColumn4.ReadOnly = True
GridViewTextBoxColumn4.TextAlignment = System.Drawing.ContentAlignment.MiddleRight
GridViewTextBoxColumn5.FieldName = "custText1"
GridViewTextBoxColumn5.HeaderText = "Doc Type"
GridViewTextBoxColumn5.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft
GridViewTextBoxColumn5.IsVisible = False
GridViewTextBoxColumn5.Name = "column_custText1"
GridViewTextBoxColumn5.ReadOnly = True
GridViewTextBoxColumn5.Width = 60
GridViewTextBoxColumn6.FieldName = "custText2"
GridViewTextBoxColumn6.HeaderText = "In/Out"
GridViewTextBoxColumn6.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft
GridViewTextBoxColumn6.IsVisible = False
GridViewTextBoxColumn6.Name = "column_custText2"
GridViewTextBoxColumn6.ReadOnly = True
GridViewTextBoxColumn6.Width = 40
GridViewTextBoxColumn7.FieldName = "custText3"
GridViewTextBoxColumn7.HeaderText = "Docketing"
GridViewTextBoxColumn7.IsVisible = False
GridViewTextBoxColumn7.Name = "column_custText3"
GridViewTextBoxColumn7.ReadOnly = True
GridViewTextBoxColumn7.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter
GridViewTextBoxColumn7.Width = 70
GridViewTextBoxColumn8.FieldName = "custText4"
GridViewTextBoxColumn8.HeaderText = "Notes"
GridViewTextBoxColumn8.HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleLeft
GridViewTextBoxColumn8.IsVisible = False
GridViewTextBoxColumn8.Name = "column_custText4"
GridViewTextBoxColumn8.ReadOnly = True
GridViewTextBoxColumn8.Width = 80
GridViewTextBoxColumn9.FieldName = "source_path"
GridViewTextBoxColumn9.HeaderText = "Origin"
GridViewTextBoxColumn9.Name = "column_source_path"
GridViewTextBoxColumn9.ReadOnly = True
GridViewTextBoxColumn9.Width = 220
GridViewTextBoxColumn10.FieldName = "date_filed"
GridViewTextBoxColumn10.HeaderText = "Profile Date"
GridViewTextBoxColumn10.Name = "column_date_filed"
GridViewTextBoxColumn10.ReadOnly = True
GridViewTextBoxColumn10.Width = 95
GridViewTextBoxColumn11.FieldName = "cont_id"
GridViewTextBoxColumn11.HeaderText = "cont_id"
GridViewTextBoxColumn11.IsVisible = False
GridViewTextBoxColumn11.Name = "cont_id"
GridViewTextBoxColumn12.FieldName = "application_id"
GridViewTextBoxColumn12.HeaderText = "application_id"
GridViewTextBoxColumn12.IsVisible = False
GridViewTextBoxColumn12.Name = "application_id"
Me.RadGridView_Documents.MasterTemplate.Columns.AddRange(New Telerik.WinControls.UI.GridViewDataColumn() {GridViewCheckBoxColumn1, GridViewTextBoxColumn1, GridViewTextBoxColumn2, GridViewTextBoxColumn3, GridViewDateTimeColumn1, GridViewTextBoxColumn4, GridViewTextBoxColumn5, GridViewTextBoxColumn6, GridViewTextBoxColumn7, GridViewTextBoxColumn8, GridViewTextBoxColumn9, GridViewTextBoxColumn10, GridViewTextBoxColumn11, GridViewTextBoxColumn12})
Me.RadGridView_Documents.MasterTemplate.EnableGrouping = False
Me.RadGridView_Documents.Name = "RadGridView_Documents"
Me.RadGridView_Documents.Padding = New System.Windows.Forms.Padding(0, 0, 0, 1)
0
regina
Top achievements
Rank 1
answered on 25 Feb 2011, 02:27 PM
that is interesting the code block killed the rest of my post.
Richard thank you, but sadly no that does not help as I do have the correct code in place to bind the datatable to the grid, the issue is that after I have ordered the data from the database and added a checkbox to the table I then bind it to the grid, it has two hidden fields and this data is used to pass information when a row is selected to a preview pane that is actually a webbrowser. this all works very well until after someone selects a column header resort, just for the default function of asc or desc. then the visual data in the grid appears reordered but the underlying data that is now firing the preview is the original datatable order like there is a missing accept changes somewhere in code cannot see.
here is an example of the signature for preview, do not think this is part of it:
and I am not catching or participating in the sort other than allowing it in the defined grid.
thank you and I apologize for not being more precise previously.
Richard thank you, but sadly no that does not help as I do have the correct code in place to bind the datatable to the grid, the issue is that after I have ordered the data from the database and added a checkbox to the table I then bind it to the grid, it has two hidden fields and this data is used to pass information when a row is selected to a preview pane that is actually a webbrowser. this all works very well until after someone selects a column header resort, just for the default function of asc or desc. then the visual data in the grid appears reordered but the underlying data that is now firing the preview is the original datatable order like there is a missing accept changes somewhere in code cannot see.
here is an example of the signature for preview, do not think this is part of it:
Private Sub RadGridView_Documents_Click_1(ByVal sender As Telerik.WinControls.UI.RadGridView, ByVal e As Telerik.WinControls.UI.CurrentRowChangedEventArgs) Handles RadGridView_Documents.CurrentRowChanged
and I am not catching or participating in the sort other than allowing it in the defined grid.
thank you and I apologize for not being more precise previously.
0
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 02:27 PM
Hello Regina,
Thanks you for this but I am unsure as to the issue. The RadGridView will sort asc/desc when you click on the column headers. Please cold you let me know your desired functionality and I'll do my best to help
Richard
Thanks you for this but I am unsure as to the issue. The RadGridView will sort asc/desc when you click on the column headers. Please cold you let me know your desired functionality and I'll do my best to help
Richard
0
regina
Top achievements
Rank 1
answered on 25 Feb 2011, 02:32 PM
My desired function is that the grid actually alter the data completely on the sort and not just visually.
right now in function I see the expected change, but the underlying data that continues to move through code is unaltered. when I click on the row, no matter what I see in that row the actual underlying data is the original order of the data previous to the sort. this will not work in terms of the end user expectation. They are going through documents and records, and they expect to see a match on the titles and information in the grid in the preview, but because of the lost ordering, in that the sort does not appear to commit any change to the actual data in the control there is an issue.
right now in function I see the expected change, but the underlying data that continues to move through code is unaltered. when I click on the row, no matter what I see in that row the actual underlying data is the original order of the data previous to the sort. this will not work in terms of the end user expectation. They are going through documents and records, and they expect to see a match on the titles and information in the grid in the preview, but because of the lost ordering, in that the sort does not appear to commit any change to the actual data in the control there is an issue.
0
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 03:50 PM
Hello Regina,
I'm afraid I've been unable to replicate your issue. Clicking on the column header sorts the visual cells and the underlying value. If you can reproduce this in a test project, I'd ask that you post the full version here so it can be looked at.
Look forward to hearing back from you
Richard
I'm afraid I've been unable to replicate your issue. Clicking on the column header sorts the visual cells and the underlying value. If you can reproduce this in a test project, I'd ask that you post the full version here so it can be looked at.
Look forward to hearing back from you
Richard
0
Hello Regina,
We have never experienced the issue you report and I am not able to reproduce it. Please open a new support ticket and attach a sample project demonstrating the issue. It will help me investigate your case and provide you with further assistance.
Best regards,
Alexander
the Telerik team
We have never experienced the issue you report and I am not able to reproduce it. Please open a new support ticket and attach a sample project demonstrating the issue. It will help me investigate your case and provide you with further assistance.
Best regards,
Alexander
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
loraine
Top achievements
Rank 1
answered on 14 Apr 2011, 02:13 PM
hi!
i have a radgridview...
i want to sort them in ascending/descending depending on the button that the user clicks.
i also have a combobox that contains the column names of the radgridview which the user chooses to sort the data based on the column names...
unfortunately, i don't know how to do it...
can you help m with this one?
thanks :)
i have a radgridview...
i want to sort them in ascending/descending depending on the button that the user clicks.
i also have a combobox that contains the column names of the radgridview which the user chooses to sort the data based on the column names...
unfortunately, i don't know how to do it...
can you help m with this one?
thanks :)
0
Hello Loraine,
Thank you for your question.
You should create a Sort Descriptor for every column you would like to sort by. Please let us know if this help article helps you to achieve your requirements.
Best regards,
Alexander
the Telerik team
Thank you for your question.
You should create a Sort Descriptor for every column you would like to sort by. Please let us know if this help article helps you to achieve your requirements.
Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
loraine
Top achievements
Rank 1
answered on 19 Apr 2011, 01:25 PM
thanks Telerik!
i have solved this problem :)
i have solved this problem :)
0
IT
Top achievements
Rank 1
answered on 18 Jul 2015, 01:21 AM
Have you ever solved this problem? I have the same issue. Thank you!
0
Hi Cornel,
Thank you for writing.
If you would like your DataTable to be always sorted as the grid to which it serves as a data source, you would need to create a data view out of your table and apply it to the sort descriptors currently applied in RadGridView. Please check my code snippet below:
I hope this helps. Should you have further questions please do not hesitate to write back.
Regards,
Hristo Merdjanov
Telerik
Thank you for writing.
If you would like your DataTable to be always sorted as the grid to which it serves as a data source, you would need to create a data view out of your table and apply it to the sort descriptors currently applied in RadGridView. Please check my code snippet below:
public
partial
class
Form1 : Form
{
private
DataTable table;
public
Form1()
{
InitializeComponent();
this
.Load += Form1_Load;
this
.radGridView1.SortChanged += radGridView1_SortChanged;
}
private
void
radGridView1_SortChanged(
object
sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
{
DataView dv =
new
DataView(table);
dv.Sort =
this
.radGridView1.SortDescriptors.Expression;
table = dv.ToTable();
}
private
void
SortDataTable()
{
DataView dv =
new
DataView(table);
dv.Sort =
this
.radGridView1.SortDescriptors.Expression;
table = dv.ToTable();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
this
.radGridView1.DataSource =
this
.GetTable();
}
private
DataTable GetTable()
{
table =
new
DataTable();
table.Columns.Add(
"Dosage"
,
typeof
(
int
));
table.Columns.Add(
"Drug"
,
typeof
(
string
));
table.Columns.Add(
"Patient"
,
typeof
(
string
));
table.Columns.Add(
"Date"
,
typeof
(DateTime));
table.Rows.Add(25,
"Indocin"
,
"David"
, DateTime.Now);
table.Rows.Add(50,
"Enebrel"
,
"Sam"
, DateTime.Now);
table.Rows.Add(10,
"Hydralazine"
,
"Christoff"
, DateTime.Now);
table.Rows.Add(21,
"Combivent"
,
"Janet"
, DateTime.Now);
table.Rows.Add(100,
"Dilantin"
,
"Melanie"
, DateTime.Now);
return
table;
}
}
I hope this helps. Should you have further questions please do not hesitate to write back.
Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items