By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

Gridview Calculation

parshupooja
100+
P: 159
Hi all,

I need help. I am using asp.net C# . I have binded gridview with 5 columns. I have Dropdownlist in 4 columns Morning in, morning out, afternoon in and afternoon out and label in 5 column named as totalwhere i want to show calculation(morningout-morningin+afternoonout-afternoonin) but whenever i try to run this i get error in bolded section that "input string is not in correct format". However I have acheived this calculation on Gridview Databound event if I use Textboxes instead of Dropdowns.
protected void gvOnRowDataBound(object sender, GridViewRowEventArgs e)
{

if (GridView1.Rows.Count > 0)
{
foreach (GridViewRow grdts in GridView1.Rows)
{ DropDownList labelmin = (DropDownList)grdts.FindControl("DropDownList1");
DropDownList labelmout = (DropDownList)grdts.FindControl("DropDownList2");
DropDownList labelain = (DropDownList)grdts.FindControl("DropDownList3");
DropDownList labelaout = (DropDownList)grdts.FindControl("DropDownList4");
Label labelTotalHr = (Label)grdts.FindControl("lblTotalHr");
Label lbltotal = (Label)grdts.FindControl("lblTotal");

int hr = (Int32.Parse(labelmout.SelectedValue.Trim()) - Int32.Parse(labelmin.SelectedValue.Trim())) + (Int32.Parse(labelaout.SelectedValue.Trim()) - Int32.Parse(labelain.SelectedValue.Trim()));
labelTotalHr.Text = hr.ToString();
}
}


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvOnRowDataBound" CellPadding="6" CssClass="GridStyle" Width="600" ShowFooter="True" OnDataBound="TextBox1_SelectedIndexChanged" OnDataBinding="GridView1_DataBinding" OnSelectedIndexChanged="GridView1_SelectedIndexCha nged">
<RowStyle CssClass="GridItem" />
<AlternatingRowStyle CssClass="GridAltItem" />
<Columns>
</asp:TemplateField>
<asp:TemplateField HeaderText="Morning In">

<ItemTemplate>
<asp:DropDownList runat="server" id=DropDownList1 SelectedValue='<%# Bind("MorningIn") %>'>
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1">1 AM</asp:ListItem>
<asp:ListItem Value="2">2 AM</asp:ListItem>
<asp:ListItem Value="3">3 AM</asp:ListItem>
<asp:ListItem Value="4">4 AM</asp:ListItem>
<asp:ListItem Value="5 ">5 AM</asp:ListItem>
<asp:ListItem Value="6">6 AM</asp:ListItem>
<asp:ListItem Value="7 ">7 AM</asp:ListItem>
<asp:ListItem Value="8">8 AM</asp:ListItem>
<asp:ListItem Value="9">9 AM</asp:ListItem>
<asp:ListItem Value="10">10 AM</asp:ListItem>
<asp:ListItem Value="11">11 AM</asp:ListItem>
<asp:ListItem Value="12">12 AM</asp:ListItem>
<asp:ListItem Value="13">1 PM</asp:ListItem>
<asp:ListItem Value="14">2 PM</asp:ListItem>
<asp:ListItem Value="15">3 PM</asp:ListItem>
<asp:ListItem Value="16">4 PM</asp:ListItem>
<asp:ListItem Value="17">5 PM</asp:ListItem>
<asp:ListItem Value="18">6 PM</asp:ListItem>
<asp:ListItem Value="19">7 PM</asp:ListItem>
<asp:ListItem Value="20">8 PM</asp:ListItem>
<asp:ListItem Value="21">9 PM</asp:ListItem>
<asp:ListItem Value="22">10 PM</asp:ListItem>
<asp:ListItem Value="23">11 PM</asp:ListItem>
<asp:ListItem Value="24">12 PM</asp:ListItem>
</asp:DropDownList>

</ItemTemplate>
<HeaderTemplate>
Morning
<br />
In<br />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Out">
<ItemTemplate>
<asp:DropDownList runat="server" id=DropDownList2 SelectedValue='<%# Bind("MorningOut") %>'>
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1 ">1 AM</asp:ListItem>
<asp:ListItem Value="2">2 AM</asp:ListItem>
<asp:ListItem Value="3">3 AM</asp:ListItem>
<asp:ListItem Value="4">4 AM</asp:ListItem>
<asp:ListItem Value="5 ">5 AM</asp:ListItem>
<asp:ListItem Value="6">6 AM</asp:ListItem>
<asp:ListItem Value="7 ">7 AM</asp:ListItem>
<asp:ListItem Value="8">8 AM</asp:ListItem>
<asp:ListItem Value="9">9 AM</asp:ListItem>
<asp:ListItem Value="10">10 AM</asp:ListItem>
<asp:ListItem Value="11">11 AM</asp:ListItem>
<asp:ListItem Value="12">12 AM</asp:ListItem>
<asp:ListItem Value="13">1 PM</asp:ListItem>
<asp:ListItem Value="14">2 PM</asp:ListItem>
<asp:ListItem Value="15">3 PM</asp:ListItem>
<asp:ListItem Value="16">4 PM</asp:ListItem>
<asp:ListItem Value="17">5 PM</asp:ListItem>
<asp:ListItem Value="18">6 PM</asp:ListItem>
<asp:ListItem Value="19">7 PM</asp:ListItem>
<asp:ListItem Value="20">8 PM</asp:ListItem>
<asp:ListItem Value="21">9 PM</asp:ListItem>
<asp:ListItem Value="22">10 PM</asp:ListItem>
<asp:ListItem Value="23">11 PM</asp:ListItem>
<asp:ListItem Value="24">12 PM</asp:ListItem>
</asp:DropDownList> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="In">
<ItemTemplate>
<asp:DropDownList runat="server" id=DropDownList3 SelectedValue='<%# Bind("AfternoonIn") %>'>
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1 ">1 AM</asp:ListItem>
<asp:ListItem Value="2">2 AM</asp:ListItem>
<asp:ListItem Value="3">3 AM</asp:ListItem>
<asp:ListItem Value="4">4 AM</asp:ListItem>
<asp:ListItem Value="5 ">5 AM</asp:ListItem>
<asp:ListItem Value="6">6 AM</asp:ListItem>
<asp:ListItem Value="7 ">7 AM</asp:ListItem>
<asp:ListItem Value="8">8 AM</asp:ListItem>
<asp:ListItem Value="9">9 AM</asp:ListItem>
<asp:ListItem Value="10">10 AM</asp:ListItem>
<asp:ListItem Value="11">11 AM</asp:ListItem>
<asp:ListItem Value="12">12 AM</asp:ListItem>
<asp:ListItem Value="13">1 PM</asp:ListItem>
<asp:ListItem Value="14">2 PM</asp:ListItem>
<asp:ListItem Value="15">3 PM</asp:ListItem>
<asp:ListItem Value="16">4 PM</asp:ListItem>
<asp:ListItem Value="17">5 PM</asp:ListItem>
<asp:ListItem Value="18">6 PM</asp:ListItem>
<asp:ListItem Value="19">7 PM</asp:ListItem>
<asp:ListItem Value="20">8 PM</asp:ListItem>
<asp:ListItem Value="21">9 PM</asp:ListItem>
<asp:ListItem Value="22">10 PM</asp:ListItem>
<asp:ListItem Value="23">11 PM</asp:ListItem>
<asp:ListItem Value="24">12 PM</asp:ListItem>
</asp:DropDownList> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Out">
<ItemTemplate>
<asp:DropDownList runat="server" id=DropDownList 4 SelectedValue='<%# Bind("AfternoonOut") %>' >
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1 ">1 AM</asp:ListItem>
<asp:ListItem Value="2">2 AM</asp:ListItem>
<asp:ListItem Value="3">3 AM</asp:ListItem>
<asp:ListItem Value="4">4 AM</asp:ListItem>
<asp:ListItem Value="5 ">5 AM</asp:ListItem>
<asp:ListItem Value="6">6 AM</asp:ListItem>
<asp:ListItem Value="7 ">7 AM</asp:ListItem>
<asp:ListItem Value="8">8 AM</asp:ListItem>
<asp:ListItem Value="9">9 AM</asp:ListItem>
<asp:ListItem Value="10">10 AM</asp:ListItem>
<asp:ListItem Value="11">11 AM</asp:ListItem>
<asp:ListItem Value="12">12 AM</asp:ListItem>
<asp:ListItem Value="13">1 PM</asp:ListItem>
<asp:ListItem Value="14">2 PM</asp:ListItem>
<asp:ListItem Value="15">3 PM</asp:ListItem>
<asp:ListItem Value="16">4 PM</asp:ListItem>
<asp:ListItem Value="17">5 PM</asp:ListItem>
<asp:ListItem Value="18">6 PM</asp:ListItem>
<asp:ListItem Value="19">7 PM</asp:ListItem>
<asp:ListItem Value="20">8 PM</asp:ListItem>
<asp:ListItem Value="21">9 PM</asp:ListItem>
<asp:ListItem Value="22">10 PM</asp:ListItem>
<asp:ListItem Value="23">11 PM</asp:ListItem>
<asp:ListItem Value="24">12 PM</asp:ListItem>
</asp:DropDownList> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Hours">
<HeaderTemplate>
Total
<br />
Hours
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblTotalHr" runat="server"></asp:Label>

</ItemTemplate>
</asp:TemplateField>
</Columns>

</asp:GridView>
Thank You in advance
Sep 23 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.