Help: sorting problem on data type?

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Help: sorting problem on data type?

    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


    hb Guest

  2. Similar Questions and Discussions

    1. Converting from data type varchar to data type money
      Hi all, Tearing my hair out trying to figure this out. If anyone can provide any help i would greatly appreciate it. When I try to do an insert...
    2. Converting data type varchar to data type money
      Hi all, Tearing my hair out trying to figure this out. If anyone can provide any help i would greatly appreciate it. When I try to do an insert...
    3. ASP.NET Sorting data
      I am creating a web application (ASP.NET) to store contact details of all staff etc. I am able to pull all the data out of my sql database and...
    4. Sorting and data
      Hey, I have a datagrid, which has sorting enabled. Now whenever I sort the datagrid, then the first few lines in the datagrid, doesnt show the...
    5. Sorting by Data
      Hi, I currently have a Transactions form that outputs, in continuous form view, all of the transactions for a given contact. I would like to...
  3. #2

    Default RE: Help: sorting problem on data type?

    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[i][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" <hongbo@goodoffices.com>
    | 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: <ODED9NzcDHA.1656@TK2MSFTNGP10.phx.gbl>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | NNTP-Posting-Host: x403446fe.ip.e-nt.net 64.52.70.254
    | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
    | Xref: cpmsftngxa06.phx.gbl
    microsoft.public.dotnet.framework.aspnet.datagridc ontrol:6506
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    |
    | 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
    |
    |
    |

    Lewis Wang [MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139