Google
Web Netomatix

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: 2871

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

2/26/2010 1:51:32 AM #

motorhome insurance

i agree! i’ve been searching for so long for a site where i could find everything that i want, and i’ve just found it!! really, i’ve visited your blog, and it’s amazing, i will keep visiting.we provide best static caravan Insurance,Park home insurance and for Holiday lodges too.

motorhome insurance United States

2/26/2010 11:06:16 PM #

used jeans

Great info.I like all your post.I will keep visiting this blog very often.It is good to see you verbalise from the heart and your clarity on this important subject can be easily observed. we are Major supplier of used clothing, used shoes, garments nd many different kinds of name brand clothing closeouts etc., many more are available here...

used jeans United States

2/27/2010 10:35:09 AM #

pool fencing

thanks for your tips....

pool fencing United States

3/1/2010 12:38:13 AM #

skiing gears

I bought skiing gears at cheap prices at this shop and thought its my obligation to let you guys know that you can also get them as well.since its skiing season :)

skiing gears United States

3/4/2010 9:57:49 PM #

Affiliate networks

I am 21 years old and I hope that the working model, and I want to work in this area and I hope that you accept me.

Affiliate networks United States

3/5/2010 4:14:58 AM #

Free Games

What is captcha code?, pls provide me captcha code codes or plugin, Thanks in advance.

Free Games United States

3/9/2010 12:08:41 AM #

milan hotels

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.Accommodation in Milan: cheap, budget, boutique or luxury accommodation. Accommodation available in Milan city centre and surroundings area of Milan. Secure and instant bookings!

milan hotels United States

3/10/2010 1:31:35 AM #

guaranteed personal loans

i would love to say thanks at least for a mind blasting blog

guaranteed personal loans United States

3/10/2010 1:33:21 AM #

guaranteed personal loans

I would be better if you would review my blog as well.

guaranteed personal loans United States

3/12/2010 2:39:49 AM #

autism characteristics

Thanks for revealing such precious information as it will be of great use for my older son.

autism characteristics United Kingdom

3/15/2010 5:19:03 AM #

Average whiplash payout

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Average compensation for whiplash Injury,,average whiplash compensation,average whiplash claim

Average whiplash payout United States

Add Comment


(Will show your Gravatar icon)

  Country flag


  • Comment
  • Live Preview
Loading