Pass (Send) Checked (Selected) ASP.Net GridView Row to multiline TextBox (TextArea) using C# and VB.Net

Last Reply 28 days ago By pandeyism

Posted one month ago

I have a gridview with checkbox column, on when the checkbox column is checked, the rows will be transfered to a group of texboxes outside the gridview. 

using the jquery function, I was able to auto calculate the checked rows, however I have not been able to pass the other row values to the textboxes. 

Can anyone explain to me or point out what I'm doing wrong?

this the jquery function:

        <script type="text/javascript"> 
            $(function () {
                $('[id*=ItemChk1]').on('change', function () {
                    var value = 0;
                    $('[id*=ItemChk1]:checked').each(function () {
                        var row = $(this).closest('tr');
                        value = value + parseInt(row.find('[id*=lbRM]').html());
                    });
                    $('[id*=TotalLabel]').html(value); 
                    $('[id*=SumTotal]').val(value);
                });
            });
        </script>

 this the HTML code for the Gridview:

                <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                    runat="server" AutoGenerateColumns="False" ShowFooter="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="HeadChk1" runat="server" CssClass="HeadChk1" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="ItemChk1" runat="server" CssClass="ItemChk1" OnCheckedChanged="SaveEmployees"   />
                            </ItemTemplate>
                        </asp:TemplateField>
                       <%-- <asp:BoundField DataField="Id" HeaderText="Id">
                            <ItemStyle Width="30px" />
                        </asp:BoundField>--%>
                        <asp:TemplateField HeaderText="Servis">
                            <FooterTemplate>
                                Total
                            </FooterTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("cat") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("cat") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Harga">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("RM_biasa") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                               <%-- <asp:Label ID="TotalLabel" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" CssClass="TotalLabel"
                                    Font-Size="14pt" ForeColor="Blue"></asp:Label>--%>
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lbRM" CssClass="Label1" runat="server" Text='<%# Bind("RM_biasa") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Rawatan">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("treat") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("treat") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                    </Columns>
                    <HeaderStyle BackColor="#3AC0F2" ForeColor="White" />
                </asp:GridView>

this is the C# code to pass the gridview row using selectedIdexChanged:

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                // check row is datarow
                if (item.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (item.FindControl("ItemChk1") as CheckBox);
                    if (chk.Checked)
                    {
                       
                        string conn = servTxt.Text;
                    }
                }
            }
        }

 this is the C# code to pass the gridview row using checkChanged:

        protected void SaveEmployees(object sender, EventArgs e)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                // check row is datarow
                if (item.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (item.FindControl("ItemChk1") as CheckBox);
                    if (chk.Checked)
                    {
                       //textbox is id is servTxt
                        servTxt.Text = (Label)item.FindControl("Label3");
                    }
                }
            }
        }
Posted 28 days ago Modified on 26 days ago

Hi ryan78,

Refer below sample.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True"
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="HeadChk1" runat="server" CssClass="HeadChk1" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="ItemChk1" runat="server" CssClass="ItemChk1" AutoPostBack="true"
                    OnCheckedChanged="SaveEmployees" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Servis">
            <FooterTemplate>
                Total :
                <asp:Label ID="SumTotal" runat="server" />
            </FooterTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("cat") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("cat") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Harga">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("RM_biasa") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lbRM" CssClass="Label1" runat="server" Text='<%# Bind("RM_biasa") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rawatan">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("treat") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("treat") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowSelectButton="true" />
    </Columns>
</asp:GridView>
<br />
<asp:TextBox runat="server" ID="servTxt" TextMode="MultiLine" Height="124px" Width="269px" />

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("cat", typeof(string)), new DataColumn("RM_biasa", typeof(int)), new DataColumn("treat", typeof(string)) });
        dt.Rows.Add("C1", 10, "t1");
        dt.Rows.Add("C2", 20, "t2");
        dt.Rows.Add("C3", 30, "t3");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

decimal total = 0;
protected void SaveEmployees(object sender, EventArgs e)
{
    DisplayDetailsWithTotal();
}

private void DisplayDetailsWithTotal()
{
    servTxt.Text = "";
    string treat = "";
    string val = "";
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (row.FindControl("ItemChk1") as CheckBox);
            if (chk.Checked)
            {
                total = total + Convert.ToInt32((row.FindControl("lbRM") as Label).Text);
                treat = (row.FindControl("Label3") as Label).Text;
                val = (row.FindControl("lbRM") as Label).Text;
                servTxt.Text += treat + " " + val + "\n";
            }
        }
    }
    servTxt.Text += "Total" + " " + total;
    GridView1.FooterRow.Cells[1].Text = "Total";
    GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
    GridView1.FooterRow.Cells[2].Text = total.ToString("N2");
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    DisplayDetailsWithTotal();
}

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("cat", GetType(String)), New DataColumn("RM_biasa", GetType(Integer)), New DataColumn("treat", GetType(String))})
        dt.Rows.Add("C1", 10, "t1")
        dt.Rows.Add("C2", 20, "t2")
        dt.Rows.Add("C3", 30, "t3")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Private total As Decimal = 0

Protected Sub SaveEmployees(ByVal sender As Object, ByVal e As EventArgs)
    DisplayDetailsWithTotal()
End Sub

Private Sub DisplayDetailsWithTotal()
    servTxt.Text = ""
    Dim treat As String = ""
    Dim val As String = ""
    For Each row As GridViewRow In GridView1.Rows
        If row.RowType = DataControlRowType.DataRow Then
            Dim chk As CheckBox = (TryCast(row.FindControl("ItemChk1"), CheckBox))
            If chk.Checked Then
                total = total + Convert.ToInt32((TryCast(row.FindControl("lbRM"), Label)).Text)
                treat = (TryCast(row.FindControl("Label3"), Label)).Text
                val = (TryCast(row.FindControl("lbRM"), Label)).Text
                servTxt.Text += treat & " " & val & vbLf
            End If
        End If
    Next
    servTxt.Text += "Total" & " " & total
    GridView1.FooterRow.Cells(1).Text = "Total"
    GridView1.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Right
    GridView1.FooterRow.Cells(2).Text = total.ToString("N2")
End Sub

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    DisplayDetailsWithTotal()
End Sub

Screenshot