none
How to sort DataGridView with positive and negative decimal values? RRS feed

  • Question

  • Hi All,

         I need to sort datagridview with negative and positive decimal values. Presently the datagridview default sorting working as the negative sign was not considering.
    i.e. it is sorting the vaues like this:

    1.789
    2.678
    -3.401
    4.009
    -5.567
    6.008

    but our requirement is like this:
    -5.567
    -3.401
    1.789
    2.678
    4.009
    6.008


    This is a windows application. And also I need the same requirement for listview and DataTable.


    Any Help must be appreciated.

    Thanking You,
    Jayaram.
    Tuesday, December 29, 2009 2:41 PM

Answers

  • You are creating new datatable called dt1 then you're adding columns to the datatable but you are not defining the datatype for each column when you do that the default datatype is string.

    You need to define your datatype something like this

    dt1.Columns.Add(

    "Value", GetType(System.Decimal))

    dt1.Columns.Add(

    "enddate", GetType(System.DateTime))


    John
    Thursday, December 31, 2009 4:14 AM

All replies

  • Are you currently using Datatable and bind it to datagridview?
    if yes, you need to make sure that the datatype of your decimal values is decimal

    myDataTable.Columns.Add("Values", GetType(System.Decimal))

    John
    Tuesday, December 29, 2009 2:58 PM
  • Hi John,

         Thank you for the response. Your code working for my datatable sorting. And I need to sort the datagrid and listview having positive and negative decimals. i.e when I click on header of a particulat control (datagridview/listview) then the columns must be sortable.


    Thanking You,



    Regards,
    Jayaram.
    Wednesday, December 30, 2009 10:25 AM
  • Hi Jayaram,
    DataGridView by default should sort any column by clicking on the header. You need to make sure that your datatype (if it's coming from the datatable) is decimal not string not characters etc...

    Here are different ways of applying sort method

            'Sort DataTable
            dtCat.DefaultView.Sort = "DecimalColumn ASC"
            'Set the Sort Mode for a DataGridView Column
            Me.DataGridView1.Columns("DecimalColumn").SortMode = DataGridViewColumnSortMode.Automatic
            'Sort the DataGridView DecimalColumn
            Me.DataGridView1.Sort(Me.DataGridView1.Columns("DecimalColumn"), ComponentModel.ListSortDirection.Ascending)

    Here is the link for ListView Sort http://msdn.microsoft.com/en-us/kb/kb00319399.aspx


    Let me know


    John
    Wednesday, December 30, 2009 12:58 PM
  • Hi John,

          Thank you for the response. By using the default sort of the datagridview the sorting not working for negative decimal values. I am displaying the datagridview records from the dataset. I am generating the datagridview columns dynamicallly, so we are un able to find whether the column is string type or decimal type. Listview also not working for negative decimal values.


    Thanking You,


    Regards,
    Jayaram
    Wednesday, December 30, 2009 2:59 PM
  • Are you using Typed or Untyped DataSet?

    Can you post your code please?

    Thanks
    John
    Wednesday, December 30, 2009 3:03 PM
  • Hi John,


         Thank you for the prompt response. Below I am pasting the main parts of my code.

     

    string quer = "select * from screenerdata where pPublishDate='" + screenerdate + "'";

     

    MySqlDataAdapter da = new MySqlDataAdapter(quer, con);

     

    MySqlCommand cmd = new MySqlCommand(quer, con);

    da.Fill(ds);
    ......

     

    DataTable dt1 = new DataTable();

    dt1.Columns.Add(

    "symbol");

    dt1.Columns.Add(

    "paramCode");

    dt1.Columns.Add(

    "enddate");

    dt1.Columns.Add(

    "pubdate");

    dt1.Columns.Add(

    "value");

     

    foreach (DataRow dr1 in ds.Tables[0].Rows)

    {

     

    if (dr1.ItemArray[1].ToString() == paramCode1)

    {

     

    DataRow myNewRow;

    myNewRow = dt1.NewRow();

    myNewRow[

    "symbol"] = dr1.ItemArray[0].ToString();

    myNewRow[

    "paramCode"] = dr1.ItemArray[1].ToString();

    myNewRow[

    "enddate"] = dr1.ItemArray[2].ToString();

    myNewRow[

    "pubdate"] = dr1.ItemArray[3].ToString();

    myNewRow[

    "value"] = dr1.ItemArray[4].ToString();

    dt1.Rows.Add(myNewRow);

    }

    }


    .......
    ......

     

    foreach (DataRow dr3 in dt1.Rows)

    {

     

    foreach (DataRow dr4 in dt2.Rows)

    {

     

    if (cmbResultslimit.Text == "ALL")

    {

    cmbResultslimit.Text = dr3.Table.Rows.Count.ToString();

    }

     

    if (listView1.Items.Count < System.Convert.ToInt16(cmbResultslimit.Text))

    {

     

    if (dr3.ItemArray[0].ToString() == dr4.ItemArray[0].ToString())

    {

     

    if (System.Convert.ToDouble(dr3.ItemArray[4]) >= System.Convert.ToDouble(dr4.ItemArray[4]))

    {

     

    int count = listView1.Columns.Count;

     

    MySqlCommand cmd3 = con.CreateCommand();

     

    MySqlDataAdapter da1 = new MySqlDataAdapter();

     

    DataSet ds1 = new DataSet();

     

    if (con.State == ConnectionState.Closed)

    {

    con.Open();

    }

     

    // cmd3.CommandText = "select symbol,paramCode,`values` from screenerdata where symbol='" + dr3.ItemArray[0] + "' and penddate=(select max(penddate) from screenerdata where symbol='" + dr3.ItemArray[0] + "')";

    cmd3.CommandText =

    "select symbol,paramCode,`values` from screenerdata where symbol='" + dr3.ItemArray[0] + "'";

    da1.SelectCommand = cmd3;

    da1.Fill(ds1);

     

    string query3 = "select symbolName from securities where symbolId='" + dr3.ItemArray[0] + "'";

     

    MySqlCommand cmd4 = new MySqlCommand(query3, con);

     

    string paramCode3 = cmd4.ExecuteScalar().ToString();

     

    ListViewItem item = new ListViewItem();

    item = listView1.Items.Add(dr3.ItemArray[0].ToString());

    item.SubItems.Add(paramCode3);

    item.SubItems.Add(

    "1");

     

    for (int i = 0; i <= count - 1; i++)

    {

     

    if (listView1.Columns[i].Text != "Company Name" && listView1.Columns[i].Text != "Symbol" && listView1.Columns[i].Text != "Rank" && listView1.Columns[i].Text != "" && listView1.Columns[i].Text != "Industry Name" && listView1.Columns[i].Text != "Exchange")

    {

     

    string query4 = "select parameterCode from paramcode where parameterName='" + listView1.Columns[i].Text + "'";

     

    MySqlCommand cmd5 = new MySqlCommand(query4, con);

     

    string paramCode4 = cmd5.ExecuteScalar().ToString();

     

    foreach (DataRow dr5 in ds1.Tables[0].Rows)

    {

     

    if (paramCode4 == dr5.ItemArray[1].ToString())

    {

    item.SubItems.Add(dr5.ItemArray[2].ToString());

    }

    }

    }

     

    if (listView1.Columns[i].Text == "Industry Name")

    {

     

    string querysec = "select sectorId from securities where symbolId='" + dr3.ItemArray[0] + "'";

     

    MySqlCommand cmdsec = new MySqlCommand(querysec, con);

     

    string sec = cmdsec.ExecuteScalar().ToString();

     

    string queryin = "select industryName from industries where sectorId='" + sec + "'";

     

    MySqlCommand cmdin = new MySqlCommand(queryin, con);

     

    string ind = cmdin.ExecuteScalar().ToString();

    item.SubItems.Add(ind);

    }

     

    if (listView1.Columns[i].Text == "Exchange")

    {

     

    string queryex = "select exchangeId from securities where symbolId='" + dr3.ItemArray[0] + "'";

     

    MySqlCommand cmdex = new MySqlCommand(queryex, con);

     

    string exchng = cmdex.ExecuteScalar().ToString();

    item.SubItems.Add(exchng);

    }

    }

    ds1.Clear();

     

    if (con.State == ConnectionState.Open)

    {

    con.Close();

    }

    }

    }

    }

     

    else

    {

     

    return;

    }

    }

    }



    Thanking You,

    Regards,
    Jayaram.

    Wednesday, December 30, 2009 3:39 PM
  • string quer = "select * from screenerdata where pPublishDate='" + screenerdate + "'";  

    MySqlDataAdapter da = new MySqlDataAdapter(quer, con);

    MySqlCommand cmd = new MySqlCommand(quer, con);

    da.Fill(ds);
    ...... 

    DataTable dt1 = new DataTable();

    dt1.Columns.Add(

     

    "symbol");

    dt1.Columns.Add(

     

    "paramCode");

    dt1.Columns.Add(

     

    "enddate");

    dt1.Columns.Add(

     

    "pubdate");

    dt1.Columns.Add(

     

    "value");

     

     

    foreach (DataRow dr1 in ds.Tables[0].Rows)

     

    if (dr1.ItemArray[1].ToString() == paramCode1)

     

    DataRow myNewRow;

     

    "symbol"] = dr1.ItemArray[0].ToString();

    myNewRow[

     

    "paramCode"] = dr1.ItemArray[1].ToString();

    myNewRow[

     

    "enddate"] = dr1.ItemArray[2].ToString();

    myNewRow[

     

    "pubdate"] = dr1.ItemArray[3].ToString();

    myNewRow[

     

    "value"] = dr1.ItemArray[4].ToString();

    dt1.Rows.Add(myNewRow);

    }

    }


    .......
    ......

    myNewRow = dt1.NewRow();

    myNewRow[

    {

     

    {

     



    So which column you want the user to sort is it the Value Column?
    dt1.Columns.Add("value");



    John
    Wednesday, December 30, 2009 7:29 PM
  • Hi John,


           I want all the columns must be sortable. In this some columns having text and some columns having numerics. We don't know the order because they loading dynamically.


    Thanking You,



    Regards,
    Jayaram.

    Thursday, December 31, 2009 4:04 AM
  • You are creating new datatable called dt1 then you're adding columns to the datatable but you are not defining the datatype for each column when you do that the default datatype is string.

    You need to define your datatype something like this

    dt1.Columns.Add(

    "Value", GetType(System.Decimal))

    dt1.Columns.Add(

    "enddate", GetType(System.DateTime))


    John
    Thursday, December 31, 2009 4:14 AM
  • Hi John,

         Thank you for the response and Happy New Year. As per your suggestion I have added the datatype to each column and loaded the results into listview but sorting sorting was not working. The listview contains postive and nagative decimal and integer values.




    Thanking You,



    Regards,
    Jayarm
    Tuesday, January 5, 2010 9:59 AM