Multiple row summary in datadgrid footer. (RESOLVED!)
Hi everyone,
I am having a datagrid and I want to add a summary footer to it. The thing is I want to display multiple rows containing textboxes in the footer not only one. When I added template columns with textboxes to my footer and used DataBind.Eval function to bind those rows in the footer, it didn't work, textboxes are empty. Is there a special data binding function for the datagrid footer.
My code is below. For testing purposes I use the same field to populate textboxes.
HTML Code:
<asp:DataGrid id="dgFooter" runat="server" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3" ShowFooter="True" AutoGenerateColumns="False">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtBox" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "InstallEach")%>' Width="70px">
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Textbox1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "InstallEach")%>' Width="70px">
</asp:TextBox>
<br />
<asp:TextBox ID="Textbox2" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "InstallEach")%>' Width="70px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String = "server=desktop;database=Beta;uid=sa;pwd=;"
Dim Conn As New SqlConnection(strConn)
Dim cmd As New SqlCommand("Select ProductID, InstallEach From Products", Conn)
Dim daGrid As New SqlDataAdapter(cmd)
Dim dsGrid As New DataSet
daGrid.Fill(dsGrid, "Products")
With dgFooter
.DataSource = dsGrid.Tables("Products").DefaultView
.DataBind()
End With
daGrid.Dispose()
dsGrid.Dispose()
Conn.Close()
End Sub
Thanks in advance, mumick.
Re: Multiple row summary in datadgrid footer
Controls in the footerTemplate are not passed the data container when the control is bound.
You can access the controls in the template from the code-behind however, which may help you - depends on what you are showing in the footer and what you are trying to accomplish. Sounds like you may want to not show the datagrid's footer, but instead add your own <table>.
Re: Multiple row summary in datadgrid footer
This page tell you more about my problem
http://mumick2003.tripod.com/model/
Thanks in advance, mumick
Re: Multiple row summary in datadgrid footer
I see.
Well, one way would be to use a DataGrid inside the DataGrid's footer.
I know that sounds strange, but it is workable.
I guess my first question would be is the footer summary area dynamic... I mean, if I put a value in the rows above, does a calculation run client-side and fill in a value in the summary footer below - or does it not create a summary values until the user posts-back to the server.
Other than that - let me give you a little deeper background around template data-bound controls : repeaters, datalists, and datagrids.
When the page is requested, if its not a post-back, you instruct the server to go a database, pull some relative data out, and then bind a control to the data.
Binding seems rather mystical - but this is what happens:
1) You set a datasource
2) You instruct the control to DataBind()
3) When you do step 2, the control fires off a notification, that it is data-binding, and it also fires of an event every-time it binds one row of data to the control.
Two things begin to happen: For every row created, the control fires off a ItemCreated event (this is where it will create its child controls - normally whatever you put in between the <itemTemplate></itemTemplate> in repeater controls, or the <templateColumn></templateColumn> in a datagrid.
Once the child controls have been created, the next step is Databinding. On the databind, the ItemDataBound is raised, which is in charge of reading the dataRow and assigns the data to the childControl using whatever value it was instructed to use. For example, on the front-side when you put DataBinder.Eval(container.dataitem,"firstname") .. this is where the datagrid is instructed to apply the datarow column 'firstName' to the child control you put the attribute in.
Now, the datagrid will always fire the ItemCreated event every time the page is posted back to. But, ItemDataBound event is only fired when the programmer calls DataGrid.DataBind(). So, for a page that posted back to itself 5 times, ItemCreated would be called 5 times, but ItemDataBound probably called only once (the first time the page was accessed)...
This is important, because it highlights the fact that a fingerprint of the datasource is not kept between post-backs. Once you data-bind, the DataGrid could care less what the original datasource was, and keeps no reference to it.
The values in the datagrid are actually just the datagrid's children keeping their own viewstate. When you change a value in a column cell, your really just changing the Text property of a TextBox - which keeps track of its own value between post-backs.
Now, given the above two paragraphs - you may think what good is a datagrid that does not keep track of the datasource - how would you update a set of data then?
Well, there are two ways - one, you keep a copy of the data in a datatable that you store in session or viewstate, or two - you scrape the datagrid and reconstitute the data. (because you can always grab the original untouched data from the database, and you have your modified data in your datagrid to compare it to).
Anyway, I guess the jist of what I'm getting at is - perhaps the footer section shouldn't be a datagrid, but simply an htmltable, with labels in it, that are dynamically updated by javascript on the front-side, and are re-calculated for security sake on the server-side.
Re: Multiple row summary in datadgrid footer
Thanks for the deep answer, now I got a feeling of what is going on behind the scene.
My footer will be dynamically calculated as the rest of the datagrid body only after the user hits update button not when value has changed.
Have you had something like my project before, if you had what would you use as a temp storage for the user selected data and why: create temp table in db, session, viewstate.
Thanks in advance, mumick
What resource would you recommend to read in order to get a better feeling of different databidning techniques and data manipulation.
Re: Multiple row summary in datadgrid footer
As far as what storage to use
The choice really comes down to how much data you plan on storing for a user, and how much volume (traffic) the website will have, and if the data is meant to be held between different pages.
The generally safe rule is to go to a database back-end, and set the database server afire with requests using compiled stored procedures.
Session state is good for keeping data across different pages - but if the website gets more than 5000 visits in a day - SessionState should be abandoned in favor of storing data in a database.
ViewState is good for keeping data across posts to the same page - it only requires processing time on the server to encode and decode for the time it takes to process a request - and consumes no extra memory server-side, whereas SessionState does.
For your purpose - if the website will only have 10-15 concurrent users at any one time, I would simply go with storing the data in SessionState - especially if you don't have access to a database server.
If your website will have 100's of concurrent users - you should incorporate a database server.
However, ViewState is encoded/decoded rather quickly (as long as its not very large - and I've seen viewstates reach almost a page in notepad). You can store the data in viewstate, sessionstate, or simply store it in a temp table in the db - etc... it really depends on your requirements for speed, the resources at your disposal, and your timeline to complete the project.
Re: Multiple row summary in datadgrid footer
Thanks again, your answers help me understand data manipulation better.
First of all it’s going to be an internal web application for our sales department which consists of 30 people.
The data amount will be from 10 to 100 records per user to deal with.
The volume will not be very high; I’d say low from 1 to 100 visits per day.
The data will be transferred between the pages, but I am not saying that I want to transfer the whole data grid across the pages, no, just a reference to it in form of dynamic query string or stored procedure. The data grid itself I save to a temp table in database.
In my case I have at most 30 concurrent connections, but to say truth it’s going to be 5-10. I have full access to the database since I am developing one.
So, as I understood from your explanation even if I have a small number of users it's still a good practice to save everything to the database instead of session or view states variables. Why? Speed, security of data, ease of manipulation? Right now my app creates a temp table in database individually for each user using “tableprefix+SessionID+Username” string for the table name. When user decides that they are ready to save a price quote to cart I create a permanent table in my database and my temp table is deleted automatically. What do you think about it?
Thanks again for discussing it with me, I appreciate it!
mumick :afrog:
Re: Multiple row summary in datadgrid footer
With the database route, you always have the ability to easily scale an application, keep backups, have the ability to move to a web farm. For 30 users, its really not necessary - but as long as you have already built it and it works - you might as well leave it at is. There is no absolutes, and its quite possible future requests to your web app might necessitate the usage of the db as a back-end store.
Creating temp-tables in the database is fine in my book. .Net has the ability to use SQL server as a state-server built-in, so you can simply say Session[_userId]=3, and .Net takes care of getting the data serialized and deserialized in and out of the database.
Re: Multiple row summary in datadgrid footer
Thanks a lot for you answers, they helped me.
mumick :D