| During my training career, I have been asked one common question several times that how we can calculate summary totals inside GridView control and how we can show it at the footer of GridView control. So I have decided to write a tutorial on this topic. In this tutorial I will show you how you can calculate and display totals not only in GridView footer but also on any control outside GridView control. |
| |
| For the purpose of this tutorial I have setup a simple ASP.NET page with data bound GridView control and a Label control on it. Following is the HTML source code of the GridView and Label control: In the above code, I am using TemplateField column inside GridView control which has two templates ItemTemplate and FooterTemplate containing Label controls to show data. The Labels inside ItemTemplate are bound with UnitPrice and UnitsInStock fields using ASP.NET binding expression <%# %> syntax. The Labels inside FooterTemplate are left blank for showing totals later from the code. I am also attaching an event handler for the RowDataBound event with the GridView. This event is where I will calculate the total for GridView data later. The RowDataBound event is raised whenever a row in the GridView is bound to data. It is raised for the header, the footer, and data rows. This event provides an opportunity to access each row before the page is finally sent to the client for display. The Page_Load event of the page binds data with the GridView control as follows: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } private void BindData() { string constr = "Server=TestServer;Database=TestDatabase;uid=test;pwd=test;"; string query = "SELECT ProductID, ProductName, UnitPrice, UnitsInStock, OnSale FROM Products"; SqlDataAdapter da = new SqlDataAdapter(query, constr); DataTable table = new DataTable(); da.Fill(table); GridView1.DataSource = table; GridView1.DataBind(); } To calculate totals first you need to declare some variables outside the RowDataBound event. Two variables will keep track of the total of all the values bound inside GridView control and the third variable totalItems will be used to calculate the average price. decimal totalPrice = 0M; decimal totalStock = 0M; int totalItems = 0; As I mentioned above, RowDataBound event raised for all header, data or footer rows. I put one if condition to make sure currently it is binding DataRow. Then I am getting the reference of lblPrice and lblUnitsInStock labels using FindControl method. These labels have price and stock values of the current row in their text property. I am converting the text into decimal and then adding the value in totalPrice and totalStock variables. Once all the Data rows are finished I have similar check for Footer Row in which I am getting the reference of lblTotalPrice and lblTotalUnitsInStock Labels available in the FooterTemplate inside GridView and showing the totals on those labels. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lblPrice = (Label)e.Row.FindControl("lblPrice"); Label lblUnitsInStock = (Label)e.Row.FindControl("lblUnitsInStock"); decimal price = Decimal.Parse(lblPrice.Text); decimal stock = Decimal.Parse(lblUnitsInStock.Text); totalPrice += price; totalStock += stock; totalItems += 1; } if (e.Row.RowType == DataControlRowType.Footer) { Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice"); Label lblTotalUnitsInStock = (Label)e.Row.FindControl("lblTotalUnitsInStock"); lblTotalPrice.Text = totalPrice.ToString(); lblTotalUnitsInStock.Text = totalStock.ToString(); lblAveragePrice.Text = (totalPrice / totalItems).ToString("F"); } } |
Blog Archive
Tuesday, January 26, 2010
Tuesday, January 19, 2010
The first step to learning English is having a good dictionary
The first step to learning English is having a good dictionary.
What do we mean by good dictionary?
Well, firstly, if you really want to improve your fluency with the language you should use an “English – English” dictionary. Many people find it comfortable using a “Hindi –English” or “Marathi –English” dictionary. Do “not” use this kind of dictionary. Use an “English – English” dictionary only!
There are many advantages of an English - English dictionary. In this kind of dictionary, the meanings are explained in English like:
to criticize = to say negative things about; to talk about the mistakes of
So you have to understand the meaning of the meaning too. This helps you to familiarize your self with the language and also exposes you to new English words that you should find out the meanings of.
To really accelerate your English learning process, go in for an English-English dictionary. Not only should you have this kind of dictionary. You must also use it. Use it every time you come across a new word.
Also, keep in mind that when you buy your English-English dictionary, you buy a version that is for learners, not the version for native speakers. Usually, "students version" or "learners version" etc. will be written on the dictionary.
The most important thing that the dictionary you buy must contain:
The dictionary that you buy “must” contain example sentences along with every word. For example:
reaction: Response to an earlier activity.
E.g. Sentence: What was your reaction when you heard the news?
Having an example sentence is very useful. Some times the meaning provided with the word may be a little confusing. So you may misunderstand what the word means. The sample sentence clears all such doubts.
Not only this, having a sample sentence is very important because it tells you how the word can be used in conjunction with other words. How the different words connect to form a sentence. Reading the sample sentence “programs the brain” to use the word properly.
So, when you purchase a dictionary, make sure it has a lot of sample sentences. It would be even better if you purchase two different dictionaries with a lot of different sample sentences. This will give you a much better understanding of the word you look up.
The best way to use the sample sentences in the dictionary is to learn them. Whenever you are looking up some new word and you come across a sample sentence, you must read it again and again until you know the sentence by-heart.
If you do this, you will be in a position to use the word or the phrase in sentences of your own correctly. Forming correct sentences becomes easy if you learn the sample sentences after reading them again and again.
What else must your dictionary contain?
Now-a-days, many dictionaries, come with a CD. These CD's contain the software version of the dictionary. This is especially convenient if you have easy access to a computer.
You don’t have to waste too much time looking for the word you want. It is quite easy to look it up using the software dictionary. Also, the software dictionary may have audio files for the words. These audio files can be played and the correct pronunciation for a word can be known. Try to look out for a dictionary that comes along with this kind of CD.
If you are unable to find this kind of dictionary that comes with a CD, do not worry.
There are many online dictionaries available. They too allow you to search for any word conveniently. Some of them even provide audio files containing pronunciations of all the different words. One such dictionary is: Merriam-Webster Online
So in conclusion, when you go to buy your dictionary, look out for these things:
- It's an English - English dictionary
- It's a students or learners version
- It has a lot of sample sentences
- It comes with a CD that is a software dictionary and preferably has pronunciations for the words
One dictionary that stands out from the rest is the “The Collins COBUILD Advanced Learner's English Dictionary (4th edition)”. Try to get your hands on it.
