Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
More grid woes
Message
From
05/07/2005 11:14:42
 
 
To
All
General information
Forum:
ASP.NET
Category:
WebForms
Title:
More grid woes
Environment versions
Environment:
ASP.NET
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01029008
Message ID:
01029008
Views:
54
Still battling with the ASP.NET GridView control.

The problem this time is IFormatProvider and my lack of understanding of it.

Say I have a simple 2-column grid I want to bind to a DataTable at runtime. The data comes from another table where I have to extract info depending on some conditions.

Simplified:
string sDescrip="";
string sValue="";
DataTable dt = new DataTable();
DataRow dr;

// add two columns
dt.Columns.Add(new DataColumn("Description", typeof(String)));
dt.Columns.Add(new DataColumn("Value", typeof(double)));
// I have a foreach() loop here to work through another DataTable
// and decide if want want to extract info from it for the grid (complex reasons)
// say the condition is: iWantItCondition (for each item)
if (iWantItCondition==true)
{
// oRow[oColumn.Columname] is the current row in the current column in the source table

  // we move here to a column to get decription -- [code snipped]
  sDescrip=oRow[oColumn.ColumnName].ToString();

  // we move here to another column to get value -- [code snipped]
  sValue=oRow[oColumn.ColumnName].ToString();
  dr = dt.NewRow();
  dr[0] = sDescrip;

  // here's the problem
  // I want to format the string depending on what it is (sometimes a percentage, sometimes currency)
  // for example's simplicity let's deal only with Currency here

  // this line doesn't work:
  // Runtime error: "Input string was not in a correct format.
  //    Couldn't store <$134.00> in Value Column. Expected type is Double."
  dr[1] = double.Parse(sValue).ToString("C")

  ////// debug:  this line works and it shows onscreen as $134.00 correctly
  Response.Write(sValue + ": " + double.Parse(sValue).ToString("C") + "<br>");
  ////// debug

  dt.Rows.Add(dr);
}

// after done building the datarows, bind them to a grid
this.GridViewOthers.DataSource = dt;
this.GridViewOthers.DataBind();
Why does the Response.Write work fine with proper Currency formatting, but the assignment to data row 2nd column dr[1] does not? dr[1] is defined as a double. The formatted value I want to store is parsed as a double.

Do I have to save everything as a string to get formatting? The help samples for IFormatProvider are as clear as mud.

Any ideas appreciated.


Alex Feldstein, MCP, Microsoft MVP
VFP Tips: English - Spanish
Website - Blog - Photo Gallery


"Once again, we come to the Holiday Season, a deeply religious time that each of us observes, in his own way, by going to the mall of his choice." -- Dave Barry
Next
Reply
Map
View

Click here to load this message in the networking platform