TextBox inside ASP.Net GridView TextChanged event using C# and VB.Net

Last Reply one year ago By pandeyism

Posted one year ago

Hello Sir,

Im trying to add a textbox in gridview and fire a textchnaged event. Below is my code.

                                <asp:GridView runat="server" Width="900px" AutoGenerateColumns="false"  ID="gvEmployees" OnRowDataBound="gvEmployees_RowDataBound1">
                                    <Columns>
                                        <asp:BoundField DataField="date1" HeaderText="Monday" DataFormatString="{0:MM/dd/yyyy}" />
                                         <asp:BoundField DataField="from_time" HeaderText="Start Time" />
                                         <asp:BoundField DataField="to_time" HeaderText="To Time" />
                                        <asp:BoundField DataField="time_per_day" HeaderText="Normal Hours" />
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:TextBox ID="mon1" runat="server" AutoPostBack="true" OnTextChanged="mon1_TextChanged"></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Total">
                                            <ItemTemplate>
                                                <asp:Label ID="lblTotal" runat="server" />
                                            </ItemTemplate>
                                            <FooterTemplate>
                                                <b>Grand Total:&nbsp;</b>
                                                <asp:Label ID="lblGrandTotal" runat="server" />
                                            </FooterTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>

 

    protected void mon1_TextChanged(object sender, EventArgs e)
    {
         GridViewRow currentRow = (GridViewRow)((TextBox)sender).Parent;
        TextBox txt = (TextBox)currentRow.FindControl("mon");
        double count = Convert.ToDouble(txt.Text);
        double mon = Convert.ToDouble(TextBox1.Text) + count;
        TextBox1.Text = mon.ToString();
    }

Its giving me an error: 

Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.Web.UI.WebControls.GridViewRow'.

OnRowdatabound function is below:

    protected void gvEmployees_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            double hours = double.Parse(e.Row.Cells[3].Text.Trim());
            Label total = e.Row.FindControl("lblTotal") as Label;
            total.Text = (hours).ToString();
            grandTotal += double.Parse(total.Text);
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            (e.Row.FindControl("lblGrandTotal") as Label).Text = grandTotal.ToString();
        }
    }

The value that is enetered in the gridview's textbox i'm trying to add it with the grandtotal got from onrowdatabound function.

Please help me.

Thanks

Posted one year ago

Hi democloud,

Showing grandTotal in footer you need set property showfooter true and get gridview value ontextchange event so refer below code..

HTML

<asp:GridView runat="server" AutoGenerateColumns="false" ID="gvEmployees" OnRowDataBound="gvEmployees_RowDataBound1"
    ShowFooter="true">
    <Columns>
        <asp:BoundField DataField="date1" HeaderText="Monday" DataFormatString="{0:MM/dd/yyyy}" />
        <asp:BoundField DataField="from_time" HeaderText="Start Time" />
        <asp:BoundField DataField="to_time" HeaderText="To Time" />
        <asp:BoundField DataField="time_per_day" HeaderText="Normal Hours" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="mon1" runat="server" AutoPostBack="true" OnTextChanged="mon1_TextChanged"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server" />
            </ItemTemplate>
            <FooterTemplate>
                <b>Grand Total:&nbsp;</b>
                <asp:Label ID="lblGrandTotal" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:TextBox runat="server" ID="TextBox1" />

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("date1", typeof(DateTime)),
                        new DataColumn("from_time", typeof(DateTime)),
                        new DataColumn("to_time",typeof(DateTime)) ,
                        new DataColumn("time_per_day",typeof(double))});
        dt.Rows.Add("03/07/2019", "03/07/2019 09:30", "03/07/2019 06:30", 9);
        dt.Rows.Add("02/07/2019", "02/07/2019 09:30", "02/07/2019 06:30", 9);
        gvEmployees.DataSource = dt;
        gvEmployees.DataBind();
    }
}

double grandTotal;
protected void gvEmployees_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        double hours = double.Parse(e.Row.Cells[3].Text.Trim());
        Label total = e.Row.FindControl("lblTotal") as Label;
        total.Text = (hours).ToString();
        grandTotal += double.Parse(total.Text);
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        (e.Row.FindControl("lblGrandTotal") as Label).Text = grandTotal.ToString();
    }
}

protected void mon1_TextChanged(object sender, EventArgs e)
{
    GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
    TextBox txt = (TextBox)row.FindControl("mon1");
    double count = Convert.ToDouble(txt.Text);
    TextBox1.Text = count.ToString();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("date1", GetType(DateTime)), New DataColumn("from_time", GetType(DateTime)), New DataColumn("to_time", GetType(DateTime)), New DataColumn("time_per_day", GetType(Double))})
        dt.Rows.Add("03/07/2019", "03/07/2019 09:30", "03/07/2019 06:30", 9)
        dt.Rows.Add("02/07/2019", "02/07/2019 09:30", "02/07/2019 06:30", 9)
        gvEmployees.DataSource = dt
        gvEmployees.DataBind()
    End If
End Sub

Private grandTotal As Double

Protected Sub gvEmployees_RowDataBound1(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim hours As Double = Double.Parse(e.Row.Cells(3).Text.Trim())
        Dim total As Label = TryCast(e.Row.FindControl("lblTotal"), Label)
        total.Text = (hours).ToString()
        grandTotal += Double.Parse(total.Text)
    End If

    If e.Row.RowType = DataControlRowType.Footer Then
            TryCast(e.Row.FindControl("lblGrandTotal"), Label).Text = grandTotal.ToString()
    End If
End Sub

Protected Sub mon1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
    Dim txt As TextBox = CType(row.FindControl("mon1"), TextBox)
    Dim count As Double = Convert.ToDouble(txt.Text)
    TextBox1.Text = count.ToString()
End Sub

Screenshot