Formatting data in a GridView
-
I am pulling data out of a SQL database into a GridView. The column names are dynamic so I never know what they will be (except the first column). I would like to format the data (they are floating point numbers.) I tried to use the RowDataBound event but it doesn't seem to do anything. Is there a way to format these numbers in a {F2} format? (I know that this code doesn't do it but I have tried a ton of things and none of them worked.) This seems like an extremely clunky way to format the data. (We cannot format the data in SQL for other reasons.) Thx Mark Jackson
protected void gvFcstDmd_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int i = 0; DataRowView drv = e.Row.DataItem as DataRowView; for (i = 1; i < drv.Row.ItemArray.Count(); i++) // start at one to not format the first column { drv[i] = Convert.ToString((Decimal)drv.Row.ItemArray[i]); } } }
-
I am pulling data out of a SQL database into a GridView. The column names are dynamic so I never know what they will be (except the first column). I would like to format the data (they are floating point numbers.) I tried to use the RowDataBound event but it doesn't seem to do anything. Is there a way to format these numbers in a {F2} format? (I know that this code doesn't do it but I have tried a ton of things and none of them worked.) This seems like an extremely clunky way to format the data. (We cannot format the data in SQL for other reasons.) Thx Mark Jackson
protected void gvFcstDmd_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int i = 0; DataRowView drv = e.Row.DataItem as DataRowView; for (i = 1; i < drv.Row.ItemArray.Count(); i++) // start at one to not format the first column { drv[i] = Convert.ToString((Decimal)drv.Row.ItemArray[i]); } } }
Yes. Here you go How to format GridView Rows/Columns in Design/Run time[^]
thatraja
**My Tip/Tricks
My Dad had a Heart Attack on this day so don't...
** -
Yes. Here you go How to format GridView Rows/Columns in Design/Run time[^]
thatraja
**My Tip/Tricks
My Dad had a Heart Attack on this day so don't...
**I looked at this before. We don't know the column names (they are dates) so I cannot access the columns using Cell["Label"]. I tried to use an index to loop thru the cells but that causes the thing to eliminate all but the first row. I must say Microsoft's documentation for this stuff is appalling. Thx Mark Jackson
for (i = 1; i < e.Row.Cells.Count; i++) { e.Row.Cells[i].Text = Convert.ToDouble(e.Row.Cells[i]).ToString("F2"); }
-
I am pulling data out of a SQL database into a GridView. The column names are dynamic so I never know what they will be (except the first column). I would like to format the data (they are floating point numbers.) I tried to use the RowDataBound event but it doesn't seem to do anything. Is there a way to format these numbers in a {F2} format? (I know that this code doesn't do it but I have tried a ton of things and none of them worked.) This seems like an extremely clunky way to format the data. (We cannot format the data in SQL for other reasons.) Thx Mark Jackson
protected void gvFcstDmd_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int i = 0; DataRowView drv = e.Row.DataItem as DataRowView; for (i = 1; i < drv.Row.ItemArray.Count(); i++) // start at one to not format the first column { drv[i] = Convert.ToString((Decimal)drv.Row.ItemArray[i]); } } }
Got it to work. Man this seems clunky tho
protected void gvFcstDmd\_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int i; TableCell c; for (i = 1; i < e.Row.Cells.Count; i++) { c = e.Row.Cells\[i\]; c.HorizontalAlign = HorizontalAlign.Right; Double d = Double.Parse(c.Text) \* 1000; c.Text = String.Format("{0,6:N0}",d); } } }