Getting Total Values Of A Certain Column From Gridview
Him I am using a ASP.NET/VB.NET with SQL-Server-2012. I have a a GridView column with 3 fields and 1 template field as shown below:
Solution 1:
You must use the databinding events to sum the values. See this example and adapt to your needs:
private Decimal OrderTotal;
protectedvoidGridView1_DataBinding(object sender, EventArgs e)
{
OrderTotal = 0.0M;
}
protectedvoidGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Keep adding the subtotal here
OrderTotal += Subtotal;
}
}
protectedvoidGridView1_DataBound(object sender, EventArgs e)
{
//Set a control with the total sum
LabelOrderTotal.Text = OrderTotal.ToString("C");
}
Basically you keep adding the values in the RowDataBound
event and in the DataBound
event you set a label with the total sum. Alternatively, you can iterate over your grid in the DataBound
event and add everything up.
Solution 2:
I personally would do it by performing the addition (as well as the multiplication actually) in the RowDataBound event...
Solution 3:
Iterate through all DataRow
rows on the DataBound
event of the gridview and add the value of TextBox3 to a running total variable.
ProtectedSub grdItems_DataBound(ByVal sender asObject, ByVal e as EventArgs)
Dim row As GridViewRow = NothingDim runningTotal AsDouble = 0.0For i AsInteger = 0to grdItems.Rows.Count
row = grdItems.Rows(i)
If row.RowType = DataControlRowType.DataRow Then' Add error handling here
runningTotal += Ctype(CType(row.FindControl("TextBox3"), Label).Text, Double)
EndIfNext i
EndSub
Solution 4:
You can try this...
decimal totalScore=0;
protectedvoidgd__RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label TextBox3= (Label)e.Row.FindControl("TextBox3");
decimal points = Decimal.Parse(TextBox3.Text);
totalScore += points;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
lblTotal.Text = totalScore.ToString();
}
}
Solution 5:
<asp:TemplateField HeaderText="first_name" SortExpression="first_name">
<EditItemTemplate>
<asp:TextBox ID="TextBox_first_name" runat="server"Text='<%# Bind("first_name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label_first_name" runat="server"Text='<%# Bind("first_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="middle_name" SortExpression="middle_name">
<EditItemTemplate>
<asp:TextBox ID="TextBox_middle_name" runat="server"Text='<%# Bind("middle_name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label_middle_name" runat="server"Text='<%# Bind("middle_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="last_name" SortExpression="last_name">
<EditItemTemplate>
<asp:TextBox ID="TextBox_last_name" runat="server"Text='<%# Bind("last_name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label_last_name" runat="server"Text='<%# Bind("last_name") %>'></asp:Label>
</ItemTemplate>
ProtectedSub GridView_clients_RowCommand(sender AsObject, e As GridViewCommandEventArgs) Handles GridView_clients.RowCommand
If e.CommandName = "Select"ThenDim index AsInteger = Convert.ToInt32(e.CommandArgument) 'gets the row'........................................Dim client_first_name AsString = DirectCast(GridView_clients.Rows(index).FindControl("Label_first_name"), Label).Text.ToString
Dim client_middle_name AsString = DirectCast(GridView_clients.Rows(index).FindControl("Label_middle_name"), Label).Text.ToString
Dim client_last_name AsString = DirectCast(GridView_clients.Rows(index).FindControl("Label_last_name"), Label).Text.ToString
'MsgBox(client_first_name)'MsgBox(client_middle_name)'MsgBox(client_last_name)
lb_test.Text = client_first_name & " " & client_middle_name & " " & client_last_name
EndIfEndSub
from Craig Lewis Johnson from FSSA - Indiana
Post a Comment for "Getting Total Values Of A Certain Column From Gridview"