Sorting problem

A

Arda Coskun

Hi,

I did sorting on Datagrid control for integers. However, there is a problem;
when I sort ascending it makes> 23, 3, 44, 5 . But I want 3, 5, 23, 44.

How can I solve this?

I'm loading XML data, how can I tell the datagrid that the score column is
containing integers?



DataSet oData = new DataSet();

oData.ReadXml(Server.MapPath("HighScores.xml"));

DataView oView = new DataView(oData.Tables[0]);



oView.Sort = "Score DESC";

DataGrid1.DataSource = oView;

DataGrid1.DataBind();





Thanks,
 
S

Steven Cheng[MSFT]

Hi Sarp,

From your description, I think the sorting problem is likely caused by the
DataColumns' value in the Datatable, you can have a check in the certain
columns's DataType. Because when sorting , different data type has
different sorting behavior. As for the condition you met that the rows is
sorted as
23, 3, 44, 5

It is likely that the columns is sorted as "string" type rather than int or
long (number type). Please have a check and try manually set the
DataColumns' DAtaType in the DataTable.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
A

Arda Coskun

Thanks Mr. Cheng, but how I didn't create a DataTable, how can I set that
columns behavious as int?
 
A

Arda Coskun

I did;

DataSet oData = new DataSet();

oData.Tables.Add("HighScores");

oData.ReadXml(Server.MapPath("HighScores.xml"));

oData.Tables[0].Columns[2].DataType = Type.GetType("System.Int16");

DataView oView = new DataView(oData.Tables[0]);

oView.Sort = "Score DESC";

DataGrid1.DataSource = oView;

DataGrid1.DataBind();

But it gives an error, you cannot change the type of column after you
created it. What can I do, thanks?
 
A

Alvin Bruney [MVP]

what you will need to do is either parse the xml before you load it and
change the type to int16 for the appropriate node or read in the xml as
before, create another datatable with the correct schema and shallow copy
the information to the new table

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Arda Coskun said:
I did;

DataSet oData = new DataSet();

oData.Tables.Add("HighScores");

oData.ReadXml(Server.MapPath("HighScores.xml"));

oData.Tables[0].Columns[2].DataType = Type.GetType("System.Int16");

DataView oView = new DataView(oData.Tables[0]);

oView.Sort = "Score DESC";

DataGrid1.DataSource = oView;

DataGrid1.DataBind();

But it gives an error, you cannot change the type of column after you
created it. What can I do, thanks?

Arda Coskun said:
Thanks Mr. Cheng, but how I didn't create a DataTable, how can I set that
columns behavious as int?
 
A

Arda Coskun

I want to change the score column of my table. Here the code that I tried;

oData.Tables[0].Columns.Add("Name");

oData.Tables[0].Columns.Add("Country");

oData.Tables[0].Columns.Add("Score");

oData.Tables[0].Columns.Add("Level");

oData.Tables[0].Columns[2].DataType = Type.GetType("System.Int16");

oData.ReadXml(Server.MapPath("HighScores.xml"));

but it showed me an empty datagrid.

Could you please show me and example Alvin, thanks.

Alvin Bruney said:
what you will need to do is either parse the xml before you load it and
change the type to int16 for the appropriate node or read in the xml as
before, create another datatable with the correct schema and shallow copy
the information to the new table

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Arda Coskun said:
I did;

DataSet oData = new DataSet();

oData.Tables.Add("HighScores");

oData.ReadXml(Server.MapPath("HighScores.xml"));

oData.Tables[0].Columns[2].DataType = Type.GetType("System.Int16");

DataView oView = new DataView(oData.Tables[0]);

oView.Sort = "Score DESC";

DataGrid1.DataSource = oView;

DataGrid1.DataBind();

But it gives an error, you cannot change the type of column after you
created it. What can I do, thanks?

Arda Coskun said:
Thanks Mr. Cheng, but how I didn't create a DataTable, how can I set that
columns behavious as int?


Hi Sarp,

From your description, I think the sorting problem is likely caused
by
the
DataColumns' value in the Datatable, you can have a check in the
certain
columns's DataType. Because when sorting , different data type has
different sorting behavior. As for the condition you met that the
rows
is
sorted as
23, 3, 44, 5

It is likely that the columns is sorted as "string" type rather than
int
or
long (number type). Please have a check and try manually set the
DataColumns' DAtaType in the DataTable.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

Hi Arda,

I think the problem may caused by the xml file's schema or format not
compatiable with the DataTable. Can you modify the xml file's format? If
so, you can try first creating a DataTable which define the "score" column
as a integer type and write it out as xml and then use this format. Other
wise, I think you shoud consider Alvin's suggestion that create a new
columns or new datatable and manually generate the new integer datas from
the imported string data.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
A

Arda Coskun

Hi Steven,

I couldn't acheive a solution with your suggested ideas, but I used an XSD
file and indicate each column's behaviour. However, I also faced with
another problem about writing an xml with an xsd via XmlTextWriter, I'd be
happy if you can provide me an example.

Thanks for your kind reply.
 
S

Steven Cheng[MSFT]

Hi Arda,

Thanks for your followup. In fact the "writing an xml with an xsd" I mean
is just manually create a DataSet which contains a DataTable which have a
columns(type is int) and then write the Dataset's scheme out to file to
have a look at t he certain columns 's schema definition. And the .net 's
DataSet class contains the following method which will can help us directly
get a DataSet 's schema xml string or output the scheme to a file.
DataSet.GetXmlSchema
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatasetclassgetxmlschematopic.asp

DataSet.WriteXmlSchema
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataDataSetCla
ssWriteXmlSchemaTopic.asp?frame=true

in addition, when creating an new DataSet, we can also apply a existing
schema on it. Just use the
DataSet.InferXmlSchema
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatasetclassgetxmlschematopic.asp


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top