Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Formatting data in a GridView

Formatting data in a GridView

Scheduled Pinned Locked Moved ASP.NET
databasequestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    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]); } } }

    T M 2 Replies Last reply
    0
    • M mjackson11

      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]); } } }

      T Offline
      T Offline
      thatraja
      wrote on last edited by
      #2

      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...
      **

      M 1 Reply Last reply
      0
      • T thatraja

        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...
        **

        M Offline
        M Offline
        mjackson11
        wrote on last edited by
        #3

        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"); }

        1 Reply Last reply
        0
        • M mjackson11

          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]); } } }

          M Offline
          M Offline
          mjackson11
          wrote on last edited by
          #4

          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);
          		}
          	}
          }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups