Help: sorting problem on data type?

H

hb

Hi,

I use the "FOR XML AUTO,ELEMENTS" to get data from SQL server database:

string x=getProductListFromSql();

The xml scheme of x like:
<list>
<Product>
<productID>234</productID>
<productName>light</productName>
</Product>
<Product>
<productID>235</productID>
<productName>light bulb</productName>
</Product>
</list>

Then, I use the following code to bind data to datagrid:
public void BindData()

{
DataSet ds = new DataSet();

XmlDocument doc = new XmlDocument();

doc.LoadXml(x);

XmlNodeReader xnr = new XmlNodeReader(doc);

ds.ReadXml(xnr);

DataGrid1.DataSource = ds;

DataGrid1.DataBind();

}

The sorting function is like:
public void sortDG(object sender, DataGridSortCommandEventArgs e)

{

string sortKey = e.SortExpression;

Session["sortKey"] = sortKey;

BindData();

}


In DataGrid1, the boundcolumn like:
<asp:boundcolumn data="productID" sortexpression="productID"
headertext="ProductID" />

But I get a problem here: the sort function treats the "productID" as string
instead of integer.

I guess the cause is that I transferred data into xml string. Then all data
become string data type.

Please help me: How can I let the sorting function recognize "productID" as
integer?

Thank you

hb
 
L

Lewis Wang [MSFT]

Hi Hb,

We can create a new DataTable and define every column Type for it. Expect
the productID column whose type is int32, other columns' types are the same
as the old DataTable. Then we could copy all data from the old DataTable to
the new DataTable and bind the new DataTable to DataGrid1.

DataTable newtable=new DataTable ();
DataColumn cc1 = new DataColumn("productID ",
Type.GetType("System.Int32"),"");
c1.AutoIncrement=true;
DataColumn cc2 = new DataColumn("Item", Type.GetType("System.String"),"");
newtable.Columns.Add(cc1);
newtable.Columns.Add(cc2);

for(int i=0; i<t.Rows.Count;i++)
{
DataRow dr=newtable.NewRow ();
for(int j=0;j<t.Columns .Count;j++)
dr[j]=t.Rows[j];
newtable.Rows .Add(dr);
}
//Note: t is the old table, its " productID " column Type is String.

Hope this helps.

Best regards,
Lewis

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "hb" <[email protected]>
| Subject: Help: sorting problem on data type?
| Date: Thu, 4 Sep 2003 18:25:35 -0400
| Lines: 70
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: x403446fe.ip.e-nt.net 64.52.70.254
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:6506
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Hi,
|
| I use the "FOR XML AUTO,ELEMENTS" to get data from SQL server database:
|
| string x=getProductListFromSql();
|
| The xml scheme of x like:
| <list>
| <Product>
| <productID>234</productID>
| <productName>light</productName>
| </Product>
| <Product>
| <productID>235</productID>
| <productName>light bulb</productName>
| </Product>
| </list>
|
| Then, I use the following code to bind data to datagrid:
| public void BindData()
|
| {
| DataSet ds = new DataSet();
|
| XmlDocument doc = new XmlDocument();
|
| doc.LoadXml(x);
|
| XmlNodeReader xnr = new XmlNodeReader(doc);
|
| ds.ReadXml(xnr);
|
| DataGrid1.DataSource = ds;
|
| DataGrid1.DataBind();
|
| }
|
| The sorting function is like:
| public void sortDG(object sender, DataGridSortCommandEventArgs e)
|
| {
|
| string sortKey = e.SortExpression;
|
| Session["sortKey"] = sortKey;
|
| BindData();
|
| }
|
|
| In DataGrid1, the boundcolumn like:
| <asp:boundcolumn data="productID" sortexpression="productID"
| headertext="ProductID" />
|
| But I get a problem here: the sort function treats the "productID" as
string
| instead of integer.
|
| I guess the cause is that I transferred data into xml string. Then all
data
| become string data type.
|
| Please help me: How can I let the sorting function recognize "productID"
as
| integer?
|
| Thank you
|
| hb
|
|
|
 

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,951
Messages
2,570,113
Members
46,699
Latest member
Abigail89S

Latest Threads

Top