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