Link to home
Start Free TrialLog in
Avatar of cesemj
cesemj

asked on

How to automatically populate a field in a gridview based on the numeric values entered in two other fields

Hello,

How can I update a field bound to an sql data source. When I click on edit button to update the data in the gridview, I would like for the TotalManHours field to be populated after I type in a value for the TimeTocompleteWork (Number of hours to complete the work) field and the TotalEmployees (the total number of employees it took to do the job) field.  TotalManHours = TimeTocompleteWork *  TotalEmployees.  Is there a way to do this?
Thank you in advance
Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Hi

try this

aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="EditDeleteGrid.aspx.vb" Inherits="Database_EditDeleteGrid" %>

<!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>Untitled Page</title>
    <script language =javascript  type="text/javascript">
    function  Calculate(te,th,tnh)
    {
       tnh.value=te.value * th.value;
    }
   
    </script>  
</head>
<body >
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
            AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="Id" DataSourceID="SqlDataSource1" >
            <Columns>
           <asp:BoundField DataField="Id" ReadOnly=true  />
                <asp:BoundField DataField="TPerson" />
                <asp:BoundField DataField="THour" />
                  <asp:BoundField DataField="TManHour"    />
            </Columns>
        </asp:GridView>
   
    </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="your connection string"
            SelectCommand="SELECT * FROM Employee" UpdateCommand="UPDATE Employee SET TPerson =@TPerson, THour =@THour ,TManHour= @TManHour  WHERE Id=@Id" InsertCommand="INSERT INTO Employee(TPerson, THour,TManHour) VALUES (@TPerson, @THour,@TPerson * @THour)" DeleteCommand="DELETE FROM Employee WHERE (Id = @Id)" ProviderName="System.Data.SqlClient">
           
        </asp:SqlDataSource>
    </form></body>
</html>


codebehind

Partial Class Database_EditDeleteGrid
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim te As System.Web.UI.WebControls.TextBox
        Dim th As System.Web.UI.WebControls.TextBox
        Dim tnh As System.Web.UI.WebControls.TextBox
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.Cells(2).Controls.Count > 0 Then
            te = CType(e.Row.Cells(2).Controls(0), System.Web.UI.WebControls.TextBox)
            th = CType(e.Row.Cells(3).Controls(0), System.Web.UI.WebControls.TextBox)
            tnh = CType(e.Row.Cells(4).Controls(0), System.Web.UI.WebControls.TextBox)
            te.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")
            th.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")



        End If
    End Sub

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting

    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing

    End Sub
End Class
Avatar of cesemj
cesemj

ASKER

Hello,
I receive error message: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

Should I change te,th, & tnh to int field types instead of text?  Thank you in advance.
Avatar of cesemj

ASKER

Hello, since I am using a TemplateColumn I used Controls(1) instead of Controls(0) to reach the textboxes, but, no luck: I receive error message: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.
Avatar of cesemj

ASKER

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim te As System.Web.UI.WebControls.TextBox
        Dim th As System.Web.UI.WebControls.TextBox
        Dim tnh As System.Web.UI.WebControls.TextBox
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.Cells(2).Controls.Count > 0 Then
            te = CType(e.Row.Cells(2).Controls(1), System.Web.UI.WebControls.TextBox)
            th = CType(e.Row.Cells(3).Controls(1), System.Web.UI.WebControls.TextBox)
            tnh = CType(e.Row.Cells(4).Controls(1), System.Web.UI.WebControls.TextBox)
            te.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")
            th.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")



        End If
    End Sub
Avatar of cesemj

ASKER

Here are the columns I am using:
<asp:TemplateField HeaderText="te" SortExpression="te">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("te") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("te") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="Newte" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="th" SortExpression="th">
                <EditItemTemplate>
                    <asp:TextBox ID="Tbx7" runat="server" Text='<%# Bind("th") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lbl7" runat="server" Text='<%# Bind("th") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="Newth" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="tnh" SortExpression="tnh">
                <EditItemTemplate>
                    <asp:TextBox ID="Tbx8" runat="server" Text='<%# Bind("tnh") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lbl8" runat="server" Text='<%# Bind("tnh") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="Newtnh" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
Avatar of cesemj

ASKER

Thank you again,

I updated RowDataBound as you stated and now receive error: Object reference not set to an instance of an object.  Do you think I am receiving the message because the three field types are INT?

[NullReferenceException: Object reference not set to an instance of an object.]
  frmEditDeleteGrid.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) +161
   System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +105
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +233
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2232
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +11
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +111
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +29
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +41
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360

 


 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim te As System.Web.UI.WebControls.TextBox
        Dim th As System.Web.UI.WebControls.TextBox
        Dim tnh As System.Web.UI.WebControls.TextBox
te = CType(e.Row.FindControl("TextBox7"), System.Web.UI.WebControls.TextBox)
th = CType(e.Row.FindControl("tbx7"), System.Web.UI.WebControls.TextBox)
tnh = CType(e.Row.FindControl("tbx8"), System.Web.UI.WebControls.TextBox)
'        If e.Row.RowType = DataControlRowType.DataRow And e.Row.Cells(2).Controls.Count > 0 Then
'            te = CType(e.Row.Cells(2).Controls(1), System.Web.UI.WebControls.TextBox)
'            th = CType(e.Row.Cells(3).Controls(1), System.Web.UI.WebControls.TextBox)
'            tnh = CType(e.Row.Cells(4).Controls(1), System.Web.UI.WebControls.TextBox)
            te.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")
            th.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")
 
 
 
        End If
    End Sub

Open in new window

Hi

Let me go thru your code. get back to u soon
Hi

try this

aspx

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
            AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="Id" DataSourceID="SqlDataSource1" >
            <Columns>
         
   
           
           <asp:TemplateField HeaderText="te" SortExpression="te">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("TPerson") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("TPerson") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="Newte" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="th" SortExpression="th">
                <EditItemTemplate>
                    <asp:TextBox ID="Tbx7" runat="server" Text='<%# Bind("THour") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lbl7" runat="server" Text='<%# Bind("THour") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="Newth" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="tnh" SortExpression="tnh">
                <EditItemTemplate>
                    <asp:TextBox ID="Tbx8" runat="server" Text='<%# Bind("TManHour") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lbl8" runat="server" Text='<%# Bind("TManHour") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="Newtnh" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
           
                    </Columns>
           
        </asp:GridView>

codebehind

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim te As System.Web.UI.WebControls.TextBox
        Dim th As System.Web.UI.WebControls.TextBox
        Dim tnh As System.Web.UI.WebControls.TextBox

        If e.Row.RowType = DataControlRowType.DataRow AndAlso TypeOf e.Row.Cells(1).Controls(1) Is System.Web.UI.WebControls.TextBox Then
            te = CType(e.Row.Cells(1).Controls(1), System.Web.UI.WebControls.TextBox)
            th = CType(e.Row.Cells(2).Controls(1), System.Web.UI.WebControls.TextBox)
            tnh = CType(e.Row.Cells(3).Controls(1), System.Web.UI.WebControls.TextBox)
            te.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")
            th.Attributes.Add("onchange", "Calculate(" + te.ClientID + "," + th.ClientID + "," + tnh.ClientID + ")")



        End If
    End Sub
Avatar of cesemj

ASKER

After I made the update, I receive error: Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox'.  Please tell me what did I do wrong?
Did u do in the same way as given in the sample. if not  problem is with the index. i mean controls(index) that you have to find the exact position of the control ie Textbox. debug the source and find the exact postion. if you cld not ,post the entire source code aspx and codebehind  
Avatar of cesemj

ASKER

Thank you,

I did it the same way in the example, just changed the names to describe the textboxes meaning:

te = HoursPerUnit
th = PeoplePerUnit
tnh = TotalHoursPerUnit

Thank you in advance,

<%@ Page Language="VB" MasterPageFile="~/MembersDefault.master" AutoEventWireup="false" CodeFile="frmHR.aspx.vb" Inherits="Members_frmHRBuild" title="HRBuilder" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
    HR Builder<br />
<script language =javascript  type="text/javascript">
function Calculate(HoursPerUnit, PeoplePerUnit, TotalHoursPerUnit)
{
    
    TotalHoursPerUnit.value = HoursPerUnit.value * PeoplePerUnit.value;
 
}
   
</script>
    <br />
    <asp:LinkButton ID="CheckAll" runat="server">Check All</asp:LinkButton>
    <asp:LinkButton ID="UncheckAll" runat="server">Uncheck All</asp:LinkButton><br />
    &nbsp;<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="DetailMUMID"
        DataSourceID="SqlDataSource1" AllowSorting="True" ShowFooter="True" AllowPaging="True" EmptyDataText="There is not any data.">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="RecordSelector" runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    &nbsp;
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DetailID" InsertVisible="False" SortExpression="DetailID">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("DetailID") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label32" runat="server" Text='<%# Bind("DetailID") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:LinkButton ID="AddRecord" runat="server" CommandName="Insert">Add</asp:LinkButton>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TemplateHRID" SortExpression="TemplateHRID" Visible="False">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox32" runat="server" Text='<%# Bind("TemplateHRID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label33" runat="server" Text='<%# Bind("TemplateHRID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="WorkOrderID" SortExpression="WorkOrderID">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox31" runat="server" Text='<%# Bind("WorkOrderID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label31" runat="server" Text='<%# Bind("WorkOrderID") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="NewWorkOrderID" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Loc" SortExpression="Loc">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="LocddlODS"
                        DataTextField="Loc" DataValueField="Loc">
                    </asp:DropDownList><asp:ObjectDataSource ID="LocddlODS" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblLoc" TypeName="tblLocBLL"></asp:ObjectDataSource>
                    &nbsp;
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Loc") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    &nbsp;<asp:DropDownList ID="NewLoc" runat="server" DataSourceID="ddlLoc"
                        DataTextField="Loc" DataValueField="Loc">
                    </asp:DropDownList><asp:SqlDataSource ID="ddlLoc" runat="server" ConnectionString="<%$ ConnectionStrings:HRConnectionString %>"
                        SelectCommand="SELECT [LocID], [Loc] FROM [tblLoc]"></asp:SqlDataSource>
                    &nbsp;
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="SystemName" SortExpression="SystemName">
                <EditItemTemplate>
                    <asp:DropDownList ID="sysidddl" runat="server" DataSourceID="sysods1"
                        DataTextField="SystemName" DataValueField="SystemName">
                    </asp:DropDownList><asp:ObjectDataSource ID="sysods1" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblTSystems" TypeName="tblTSystemsBLL"></asp:ObjectDataSource>
                    &nbsp;&nbsp;
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("SystemName") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="NewSystemName" runat="server" DataSourceID="sysODS2"
                        DataTextField="SystemName" DataValueField="SystemName">
                    </asp:DropDownList><asp:ObjectDataSource ID="sysODS2" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblTSystems" TypeName="tblTSystemsBLL"></asp:ObjectDataSource>
                    &nbsp;
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="SubSystem" SortExpression="SubSystem">
                <EditItemTemplate>
                    &nbsp;<asp:DropDownList ID="subsysddl" runat="server" DataSourceID="SubSysODS"
                        DataTextField="SubSystem" DataValueField="SubSystem">
                    </asp:DropDownList><asp:ObjectDataSource ID="SubSysODS" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblSubSystemBySystemID" TypeName="tblSubSystemBLL">
                        <SelectParameters>
                            <asp:Parameter Name="SystemID" Type="Int32" />
                        </SelectParameters>
                    </asp:ObjectDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("SubSystem") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    &nbsp;<asp:DropDownList ID="NewSubSystem" runat="server" DataSourceID="subsysddlODS2"
                        DataTextField="SubSystem" DataValueField="SubSystem">
                    </asp:DropDownList><asp:ObjectDataSource ID="subsysddlODS2" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblSubSystem" TypeName="tblSubSystemBLL"></asp:ObjectDataSource>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Component" SortExpression="Component">
                <EditItemTemplate>
                    &nbsp;<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="cpODS"
                        DataTextField="Component" DataValueField="Component">
                    </asp:DropDownList><asp:ObjectDataSource ID="cpODS" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblComponent" TypeName="tblComponentBLL"></asp:ObjectDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Component") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="NewComponent" runat="server" DataSourceID="CoddlODS2"
                        DataTextField="Component" DataValueField="Component">
                    </asp:DropDownList><asp:ObjectDataSource ID="CoddlODS2" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblComponent" TypeName="tblComponentBLL"></asp:ObjectDataSource>
                    &nbsp;
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="LocType" SortExpression="LocType">
                <EditItemTemplate>
                    <asp:DropDownList ID="LocTypeddl" runat="server" DataSourceID="LocTddlODS"
                        DataTextField="LocType" DataValueField="LocType">
                    </asp:DropDownList><asp:ObjectDataSource ID="LocTddlODS" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblLocType" TypeName="tblLocTypeBLL"></asp:ObjectDataSource>
                    &nbsp;
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("LocType") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="NewLocType" runat="server" DataSourceID="LocTddlODS2"
                        DataTextField="LocType" DataValueField="LocType">
                    </asp:DropDownList><asp:ObjectDataSource ID="LocTddlODS2" runat="server" OldValuesParameterFormatString="original_{0}"
                        SelectMethod="GettblLocType" TypeName="tblLocTypeBLL"></asp:ObjectDataSource>
                    &nbsp;
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="LevelI" SortExpression="LevelI">
                <EditItemTemplate>
                    <asp:CheckBox ID="NewLevelI" runat="server" Checked='<%# Bind("LevelI") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="NewLevelI" runat="server" Checked='<%# Bind("LevelI") %>' Enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:CheckBox ID="NewLevelI" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="LevelII" SortExpression="LevelII">
                <EditItemTemplate>
                    <asp:CheckBox ID="NewLevelII" runat="server" Checked='<%# Bind("LevelII") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="NewLevelII" runat="server" Checked='<%# Bind("LevelII") %>' Enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:CheckBox ID="NewLevelII" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="LevelIII" SortExpression="LevelIII">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("LevelIII") %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:CheckBox ID="NewLevelIII" runat="server" />
                </FooterTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("LevelIII") %>' Enabled="false" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="IndexNumber" SortExpression="IndexNumber">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("IndexNumber") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("IndexNumber") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="NewIndexNumber" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="HoursPerUnit" SortExpression="HoursPerUnit">
                <EditItemTemplate>
                    <asp:TextBox ID="tbxHrsPerUnit" runat="server" Text='<%# Bind("HoursPerUnit") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("HoursPerUnit") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="NewHoursPerUnit" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="PeoplePerUnit" SortExpression="PeoplePerUnit">
                <EditItemTemplate>
                    <asp:TextBox ID="tbxPPerUnit" runat="server" Text='<%# Bind("PeoplePerUnit") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lbl7" runat="server" Text='<%# Bind("PeoplePerUnit") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="NewPeoplePerUnit" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TotalHoursPerUnit" SortExpression="TotalHoursPerUnit">
                <EditItemTemplate>
                    <asp:TextBox ID="tbxTHrsPerUnit" runat="server" Text='<%# Bind("TotalHoursPerUnit") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Lbl8" runat="server" Text='<%# Bind("TotalHoursPerUnit") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="NewTotalHoursPerUnit" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
 
Code Behind:
------------
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim HoursPerUnit As System.Web.UI.WebControls.TextBox
        Dim PeoplePerUnit As System.Web.UI.WebControls.TextBox
        Dim TotalHoursPerUnit As System.Web.UI.WebControls.TextBox
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.Cells(14).Controls.Count > 1 Then
            HoursPerUnit = CType(e.Row.Cells(14).Controls(1), System.Web.UI.WebControls.TextBox)
            PeoplePerUnit = CType(e.Row.Cells(15).Controls(1), System.Web.UI.WebControls.TextBox)
            TotalHoursPerUnit = CType(e.Row.Cells(16).Controls(1), System.Web.UI.WebControls.TextBox)
 
            HoursPerUnit.Attributes.Add("onchange", "Calculate(" + HoursPerUnit.ClientID + "," + PeoplePerUnit.ClientID + "," + TotalHoursPerUnit.ClientID + ")")
            PeoplePerUnit.Attributes.Add("onchange", "Calculate(" + HoursPerUnit.ClientID + "," + PeoplePerUnit.ClientID + "," + TotalHoursPerUnit.ClientID + ")")
 
        End If
    End Sub
 
    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
 
    End Sub
 
    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
 
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub

Open in new window

Avatar of cesemj

ASKER

Hello,

Should I move the code from GridView1_RowDataBound to one of the other sections below since the error happens before the page opens and that the caculation will only take place after I click the edit button and enter data into the HoursPerUnit (te) and PeoplePerUnit (th) fields?

1) Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
    End Sub

2)    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
    End Sub

or

3)  Protected Sub GridView1_RowUpdating
ASKER CERTIFIED SOLUTION
Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cesemj

ASKER

Thank you again for your guidance and direction, I went the code again and made sure I had the right control index like you said.  I was thrown off because the templates start off at 0.