Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need some guidance as to how to approach the next phase and improve my existing problem. Basically I have a database and on selection through a drop down three differing but related pieces of data will be used. I need to do a calculation on the lblRate * by the txtBoxNumber to get the lblTotal. lblRate changes according to the dropdown selection. Currently I have three gridviews that are visible invisible according to the selection. I wonder if there is a better way to do this.

Please advise if you can Code below:

C#
namespace gridView
{
    public partial class Default : System.Web.UI.Page
    {
        OleDbConnection con;    
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                GridView1.Visible = true;
                GridView2.Visible = false;
                GridView3.Visible = false;                              
            }
            catch (OleDbException ex)
            {
                ex.Message.ToString();
            }
        }
       
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblItemDescription = (Label)e.Row.FindControl("lblItemDescription");
                Label lblRate = (Label)e.Row.FindControl("lblRate");
                Double PR = Convert.ToDouble(lblRate.Text);
                TextBox txtBoxNumber = (TextBox)e.Row.FindControl("txtBoxNumber");
                Label lblTotal = (Label)e.Row.FindControl("lblTotal");
                txtBoxNumber.Attributes.Add("onchange", "multiply(" + PR + ", '" + txtBoxNumber.ClientID + "','" + lblTotal.ClientID +  "')");
            }                                  
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {         
            if(DropDownList1.SelectedValue == "Platinum")
            {
                GridView1.Visible = true;
                GridView2.Visible = false;
                GridView3.Visible = false;                
            }
            else if (DropDownList1.SelectedValue == "Gold")
            {                
                GridView1.Visible = false;
                GridView2.Visible = true;
                GridView3.Visible = false;
                   
            }
            else if (DropDownList1.SelectedValue == "Silver")
            {
                GridView1.Visible = false;
                GridView2.Visible = false;
                GridView3.Visible = true;              
            }
        }
    }
}


ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="gridView.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

<script type="text/javascript">
    function multiply(Rate, number, total) {
        var num = parseFloat(document.getElementById(number).value);
        var tot = document.getElementById(total);
        var totValue = parseFloat(((Rate * num)));
        var totValueRound = Math.round(totValue, 4);
        var totValueRound = totValue;
        tot.innerHTML = totValueRound;        
    }
</script>
</head>
<body>
<form id="form1" runat="server">
    <div>   
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="AccessDataSource2" DataTextField="supportLevel" 
            DataValueField="supportLevel" onselectedindexchanged="DropDownList1_SelectedIndexChanged"  
            >
        </asp:DropDownList>

        <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
            DataFile="~/App_Data/supportServices.accdb" 
            SelectCommand="SELECT [supportLevel] FROM [supportLevel]"></asp:AccessDataSource>

        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/supportServices.accdb" 
            SelectCommand="SELECT * FROM [supportDevicesAndServices]" 
            >
        </asp:AccessDataSource>       
       
        <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 
            DataSourceID="AccessDataSource1" Visible="false" EnableViewState="False">
            <Columns>
            <asp:TemplateField HeaderText="Devices And Services" Visible="True">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server"
Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" Text='<%# Eval("platinumRate") %>' ></asp:Label>
                       
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Number">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBoxNumber" Width="100px"
runat="Server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                    
                        <asp:Label ID="lblTotal" runat="Server"  />
                        
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
 
  <asp:GridView ID="GridView2" runat="server"
AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 
            DataSourceID="AccessDataSource1" Visible="true" >
            <Columns>
            <asp:TemplateField HeaderText="Devices And Services" Visible="True">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server"
Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" Text='<%# Eval("goldRate") %>' ></asp:Label>
                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Number">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBoxNumber" Width="100px"
runat="Server"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="lblTotal" Text='<%# Eval("goldRateTotal") %>'  runat="Server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        
        <asp:GridView ID="GridView3" runat="server"
AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 
            DataSourceID="AccessDataSource1" >
            <Columns>
            <asp:TemplateField HeaderText="Devices And Services" Visible="True">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server"
Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" Text='<%# Eval("silverRate") %>' ></asp:Label>
                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Number">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBoxNumber" Width="100px"
runat="Server"   />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="lblTotal" Text='<%# Eval("silverRateTotal") %>'  runat="Server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>        
    </div>
</form>
</body>
</html>
Posted
Updated 24-Jan-12 10:01am
v2

1 solution

I would much rather see a single GridView like this:
XML
<asp:gridview id="GridView2" runat="server" autogeneratecolumns="False"
   OnRowDataBound="GridView1_RowDataBound" 
   DataSourceID="AccessDataSource1" Visible="true" >
   <columns>
      <asp:templatefield headertext="Devices And Services" visible="True">
         <itemtemplate>
            <%# Eval("itemDescription") %>
         </itemtemplate>
      </asp:templatefield>
      <asp:templatefield headertext="Rate">
         <itemtemplate>
            <asp:label id="lblRate" runat="server"></asp:label>
         </itemtemplate>
      </asp:templatefield>
      <asp:templatefield headertext="Number">
         <itemtemplate>
            <asp:textbox id="txtBoxNumber" width="100px" runat="Server" />
         </itemtemplate>
      </asp:templatefield>
      <asp:templatefield headertext="Total">
         <itemtemplate>
            <asp:label id="lblTotal" runat="Server" />
         </itemtemplate>
      </asp:templatefield>
   </columns>
</asp:gridview>

And I would make your code behind something like this:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      DataRow itemData = (DataRow) e.Row.DataItem;
      switch( DropDownList1.SelectedValue )
      {
         case "Platinum":				
            lblRate.Text = itemData["platinumRate"].ToString();
            break;
         case "Gold":
            lblRate.Text = itemData["goldRate"].ToString();
            break;
         case "Silver":
            lblRate.Text = itemData["silverRate"].ToString();
            break;
         default:
            lblRate.Text = 0.0; // Put your default value here
            break;
      }
      
	  TextBox txtBoxNumber = (TextBox)e.Row.FindControl("txtBoxNumber");				
      txtBoxNumber.Attributes.Add("onchange", "multiply(" + PR + ", '" + txtBoxNumber.ClientID + "','" + lblTotal.ClientID +  "')");
   }                                  
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{         
   GridView1.DataBind();
}


The idea behind this is to make your UI much cleaner and simpler. Logic that defines how the grid is displayed is now contained within the RowDataBound handler which also places that logic in a more appropriate location.
The one thing I haven't put in this code is object validation. You should always validate that objects like the "itemData" object and the "lblRate" and "txtBoxNumber" objects are not null before you try to use them. Never assume that they will be initialized properly.
 
Share this answer
 
v4
Comments
Andrew Chambers 24-Jan-12 17:19pm    
thanks marcus. great solution. i changed
DataRow itemData = (DataRow) e.Row.DataItem;
to
DataRowView itemData = (DataRowView)e.Row.DataItem;
and it worked.

Cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900