How to format grid view row based on values from previous row

by Admin 7. July 2009 14:46
grid row format

Download Sample Project

This was a question was asked by one of my site visitors, Mike. Following is the text of the question:

This is a similar question to one you have already answered in formatting Grid Views. However there are 2 main differences. First: I want to change the value of a column (not the format) in any row if the row preceding it has a certain value in the same column.

This question translates to How do you change value of a data grid column based on values of previous row(s). I generalized this question to cover all previous rows and not just the proceeding row. Answer to all such questions relies on handling events like RowDataBound or RowCreated events. When a data grid or grid view renders, RowDataBound fires when row is being data bound and then RowCreated is fired after it has been data bound and row has been created. So depending on at what stage of rendering you want to change behavior of a row, you will subscribe either of these events. In the sample project, I am subscribing to RowDataBound event.

Next step is to access values from previous rows. Here you have choice. One, you can keep some local vaiable that stores values from previous row(s) and then use them in current row event handling. Two, you can access the previous GridRow based on index. In this sample i will discuss the approach of accessing previous row based on index and then extracting values from certain cells.

In RowDataBound event handler, GridViewRowEventArgs provides you access to DataItem associated with current row only. You do not have access to DataItem associated with previous rows. But at this point, previous rows have been prepared for rendering. You have access to all the cell values associated with previous row. You can access GridRow object of previous rows and extract text from cells that you are interested in. In the sample project, I am accessing ListPrice from fourth column and then displaying it in current row along with price associated with current row. Well, this does not sound like something that is very interesting or useful. But it serves the purpose of demonstrating you will accomplish the task.

Here is the code snippet from sample project.


protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  double price = -1.0;
  double prevPrice = -1.0 ;
  // Access the previous row.
  if (e.Row.RowIndex != 0)
  {
   GridViewRow prevRow = this.productsGrid.Rows[e.Row.RowIndex - 1];
   if (null != prevRow && 
    prevRow.RowType == DataControlRowType.DataRow)
   {
    double.TryParse(prevRow.Cells[3].Text, out prevPrice);
   }
  }
  var thisRowData = e.Row.DataItem as DataRowView;
  if (!Convert.IsDBNull(thisRowData["ListPrice"]))
  {
   double.TryParse(thisRowData["ListPrice"].ToString(), out price);
  }
  var ctrl = e.Row.FindControl("prevPriceLabel") as Label;
  if (null != ctrl)
  {
   ctrl.Text = string.Format("{0} - {1}", prevPrice, price);
  }
 }
}

Feel free to send me any request for any other grid view implementation you would like to be answered or implemented.

Views: 12017

Tags: , ,

.Net | ASP.Net | DataGrid | GridView

Comments

7/28/2009 5:01:11 AM #

United States

11/25/2009 6:27:10 AM #

payday loans

Searching for this for some time now - i guess luck is more advanced than search engines :)

payday loans United States

12/5/2009 6:09:44 AM #

payday loans

Like your writing! Still you can do some things to improve it.

payday loans United States

12/6/2009 5:44:33 PM #

paydayloans

Interesting ... as always - is your blog making any cash advance ? ;)

paydayloans United States

12/21/2009 7:43:43 AM #

Maine Payday Loans

Nice post . keep up the good work

Maine Payday Loans United States

12/30/2009 9:57:15 AM #

cash loans

I just hope to have understood this the way it was meant

cash loans United States

1/2/2010 4:05:46 AM #

payday loans

Nice post . keep up the good work

payday loans United States

1/11/2010 12:49:43 AM #

Alcohol Tester

Thank you so much for sharing this information. I am sure a lot of developers will benefit from it.

Alcohol Tester United States

1/18/2010 7:50:24 AM #

SEO Company India

This is a great post and Thank you very much for sharing this valuable information about How to format grid view row based on values from previous row with us.

Thanks.

SEO Company India India

1/18/2010 1:48:33 PM #

instant payday advance

I have always admired the ability to bite off more than one can chew and then chew it.

instant payday advance United States

1/23/2010 4:43:34 AM #

casino cash codes

I can a have an inserted row obtain default values from controls in the previous row within a repeating table
and I wish to generate a line chart that summarises the progress of a project by
monitoring the number of tools completed per week. Two of the columns,
'targetDate' & 'actualDate' in 'tblSummary' that user inputs

casino cash codes United States

2/4/2010 5:07:44 AM #

ucvhost

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

ucvhost United States

2/6/2010 7:18:33 AM #

ucvhost

How to use Remote Desktop Connection for the two systems which have same IP Address?

ucvhost United States

2/10/2010 5:38:50 AM #

Web content writing service

The people who get on in this world are the people who get up and look for the circumstances they want, and, if they can't find them, make them.

Web content writing service United States

2/12/2010 3:05:38 AM #

ucvhost

i. I wanted to drop you a quick note to express my thanks. I've been following your blog for a month or so and have picked up a ton of good information as well as enjoyed the way you've structured your site.

ucvhost United States

2/15/2010 11:17:44 PM #

submit free articles

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

submit free articles United States

2/18/2010 11:18:23 PM #

vincere alla roulette online

Nice article. Too bad that for debugging JS in IE6 you need another MS program.But for large JS applications, it’s a bliss to know where the error comes from.

vincere alla roulette online United States

2/22/2010 3:52:07 PM #

Plumber Perth

Its good especially for the spreed-sheet users,thanks for the code

Plumber Perth United States

2/25/2010 11:55:24 AM #

 compare life insurance quotes

Thanks for the code I will make improvements on it and share it here too :)

compare life insurance quotes United States

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli