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. Computed columns in Gridview

Computed columns in Gridview

Scheduled Pinned Locked Moved ASP.NET
csharpasp-net
4 Posts 4 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.
  • N Offline
    N Offline
    Nekkantidivya
    wrote on last edited by
    #1

    Hi, I am working Asp.net gridview. I bind data to this gridview dynamically. I have 5 columns in my gridview. Now I want to display a new column in the gridview which displays the difference of the 3rd and 4th columns i.e.,(Column6= Column3-Column4). Can we do this directly in a gridview to display this result. If anyone have any idea to do this please reply me. Thanks in advance.

    A D A 3 Replies Last reply
    0
    • N Nekkantidivya

      Hi, I am working Asp.net gridview. I bind data to this gridview dynamically. I have 5 columns in my gridview. Now I want to display a new column in the gridview which displays the difference of the 3rd and 4th columns i.e.,(Column6= Column3-Column4). Can we do this directly in a gridview to display this result. If anyone have any idea to do this please reply me. Thanks in advance.

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      Have you checked the last ref link that I have provided to you. You need to do similar to achieve this.

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      1 Reply Last reply
      0
      • N Nekkantidivya

        Hi, I am working Asp.net gridview. I bind data to this gridview dynamically. I have 5 columns in my gridview. Now I want to display a new column in the gridview which displays the difference of the 3rd and 4th columns i.e.,(Column6= Column3-Column4). Can we do this directly in a gridview to display this result. If anyone have any idea to do this please reply me. Thanks in advance.

        D Offline
        D Offline
        Dinesh Mani
        wrote on last edited by
        #3

        This can be done on the datasource and on the gridview. DataSource - Just add another column to your grid view and store the calculated value for each row. Do this before you bind the data. GridView - Just add a template column and use the expression evaluator <%# %> to calculate and bind the data at runtime.

        1 Reply Last reply
        0
        • N Nekkantidivya

          Hi, I am working Asp.net gridview. I bind data to this gridview dynamically. I have 5 columns in my gridview. Now I want to display a new column in the gridview which displays the difference of the 3rd and 4th columns i.e.,(Column6= Column3-Column4). Can we do this directly in a gridview to display this result. If anyone have any idea to do this please reply me. Thanks in advance.

          A Offline
          A Offline
          Anurag Gandhi
          wrote on last edited by
          #4

          Apart from other two solution posted, you can also do it in RowDataBound Event. code will go something like this:

          protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {
          if(e.Row.RowType == DataControlRowType.Header)
          {
          TableHeaderCell cell = new TableHeaderCell();
          cell.Text = "Column 6 Name";
          e.Row.Cells.Add(cell);
          }
          else if (e.Row.RowType == DataControlRowType.DataRow)
          {
          TableCell cell = new TableCell();
          decimal result = Convert.ToDecimal(e.Row.Cells[2].Text) - Convert.ToDecimal(e.Row.Cells[3].Text);
          cell.Text = result.ToString();
          e.Row.Cells.Add(cell);
          }
          else
          {
          TableCell cell = new TableCell();
          e.Row.Cells.Add(cell); //Empty Cell.//
          }
          }

          Anurag Gandhi. http://www.gandhisoft.com Life is a computer program and every one is the programmer of his own life.

          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