Click here to Skip to main content
15,912,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi ,

I have a Datatable which needs to be sorted on header click on column name Min% and I have tried the below code...

the datasource for the dgGridview is set earlier

C#
private int SortOrder=-1;

The below code is in dgGridview_ColumnHeaderclick event
C#
DataView dv = new DataView();
DataSet ds = new DataSet();
ds = (DataSet)dgGridview.DataSource;
dv = ds.Tables[0].DefaultView;
ListSortDirection sortDirection;
String temp;
ListSortDirection sortDirection;
String temp;
if (this.dgGridview.SortedColumn.Name == "Min%")
{
    if (!dv.Table.Columns.Contains("Percent_Decimal"))
    {
        DataColumn dcColumn = new DataColumn();
        dcColumn.DataType = System.Type.GetType("System.Decimal");
        dcColumn.ColumnName = "Percent_Decimal";
        dcColumn.Expression = "Convert([Min%], 'System.Decimal')";             
        foreach (DataRow row in dv.Table.Rows)
        {
            foreach (DataColumn column in dv.Table.Columns)
            {
                if(row[column].ToString().Contains("%"))
                {
                    temp = row[column].ToString().Replace("%"," ");
                    row[column] = temp;
                }
            }
        }
        dv.Table.AcceptChanges();
        dv.Table.Columns.Add(dcGridColumn);
        DataGridViewTextBoxColumn grdTextColumn = new DataGridViewTextBoxColumn();
        grdTextColumn.Name = "Percent_Decimal";
        grdTextColumn.DataPropertyName = "Percent_Decimal";
        dgvViewAllStockGuidelines.Columns.Add(grdTextColumn);
        dgvViewAllStockGuidelines.Columns["Percent_Decimal"].Visible = false;
}                
if (this.SortOrder == -1)
{
    sortDirection = ListSortDirection.Descending;
    dgGridview.Sort(dgGridview.Columns["Percent_Decimal"], sortDirection);
    dgGridview.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Descending;
    this.SortOrder = 1;
}
else
{
    sortDirection = ListSortDirection.Ascending;
    dgGridview.Sort(dgvViewAllStockGuidelines.Columns["Percent_Decimal"], sortDirection);
    dgGridview.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Ascending;
    this.SortOrder = -1;
}
dv.Table.Columns["Min%"].DataType = System.Type.GetType("System.String");                
String tem;
foreach (DataRow row in dv.Table.Rows)
{
    foreach (DataColumn column in dv.Table.Columns)
    {
        if (row[column].ToString().Contains(" ") )
        {
            tem = row[column].ToString().Replace(" ", "%");
            row[column] = tem;//error
        }
    }
}
dv.Table.AcceptChanges();
}

I am getting error: input string is not in correct format in the above mentioned line (in bold).

Please help me regarding the same.
Thanks!
Posted
Updated 11-May-11 23:18pm
v3

1 solution

Assuming your code actually works - I haven't checked - then your problem is simple: you are doing something silly, and I don't know why:
dcColumn.DataType = System.Type.GetType("System.Decimal");

tem = row[column].ToString().Replace(" ", "%");
row[column] = tem;//error
If you try to assign a string to a decimal field, it will try to convert it: But "1.23%" is not a valid decimal. Hence the runtime error "input string is not in correct format".

So why are you doing this?
 
Share this answer
 
Comments
siva455 12-May-11 5:25am    
that is the datatype of the column which i added during runtime ie "Percent_Decimal"(column name) but it"s throwing error for existing column(Min%) but anyway before making change im changing the datatype of existing column to string...

dv.Table.Columns["Min%"].DataType = System.Type.GetType("System.String");

please correct me if im wrong..
siva455 12-May-11 6:37am    
reply pls...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900