Sorting problem

Discussion in 'ASP .Net Datagrid Control' started by Arda Coskun, Aug 21, 2004.

  1. Arda Coskun

    Arda Coskun Guest

    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,
     
    Arda Coskun, Aug 21, 2004
    #1
    1. Advertisements

  2. 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
     
    Steven Cheng[MSFT], Aug 21, 2004
    #2
    1. Advertisements

  3. Arda Coskun

    Arda Coskun Guest

    Thanks Mr. Cheng, but how I didn't create a DataTable, how can I set that
    columns behavious as int?
     
    Arda Coskun, Aug 22, 2004
    #3
  4. Arda Coskun

    Arda Coskun Guest

    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, Aug 23, 2004
    #4
  5. 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
     
    Alvin Bruney [MVP], Aug 23, 2004
    #5
  6. Arda Coskun

    Arda Coskun Guest

    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.

     
    Arda Coskun, Aug 24, 2004
    #6
  7. 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
     
    Steven Cheng[MSFT], Aug 25, 2004
    #7
  8. Steven Cheng[MSFT], Aug 27, 2004
    #8
  9. Arda Coskun

    Arda Coskun Guest

    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.
     
    Arda Coskun, Aug 27, 2004
    #9
  10. 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
     
    Steven Cheng[MSFT], Aug 28, 2004
    #10
  11. Hi Arda,

    Have you got any futher ideas on this issue? If you have anything unclear
    or if there are anything else we can help, please feel free to post here.
    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
     
    Steven Cheng[MSFT], Sep 1, 2004
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.