Gridview Checkbox Selected Row Data Display in Textbox


gridview checkbox checked selected row data display in outside textbox

In this articles i am going to show you how to get gridview selected row data and display that data outside of the textbox. Just design as i shown in above image otherwise you can copy and paste entire design source code or HTML Code from below code snippet to your Visual Studio.

HTML Markup : Design Page | HTML Code


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
    <style>

    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
            onpageindexchanging="GridView1_PageIndexChanging" PageSize="3" 
            onrowdatabound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <Columns>
            <asp:TemplateField>
             <ItemTemplate>

              <asp:CheckBox ID="chk" runat="server" AutoPostBack="True" OnCheckedChanged="chk_CheckedChanged" />
             </ItemTemplate>
            </asp:TemplateField>
                <asp:TemplateField HeaderText="Book ID">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtbookid" runat="server" Text='<%# Bindundefined"Book_Id") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblbookid" runat="server" Text='<%# Bindundefined"Book_Id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Book Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtbookname" runat="server" Text='<%# Bindundefined"Book_name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblbookname" runat="server" Text='<%# Bindundefined"Book_name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Book Author">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtbookauthor" runat="server" Text='<%# Bindundefined"Book_author") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblbookauthor" runat="server" Text='<%# Bindundefined"Book_author") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Book Publisher Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtbookpubname" runat="server" 
                            Text='<%# Bindundefined"Book_Publisher_name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblbookpubname" runat="server" Text='<%# Bindundefined"Book_Publisher_name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Book Published Date">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtbookpubdate" runat="server" 
                            Text='<%# Bindundefined"Book_Published_date") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblbookpubdate" runat="server" Text='<%# Bindundefined"Book_Published_date") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />
        </asp:GridView>
        <br /><br />
        <table id="table">
         <tr>
          <th colspan="2">Display Gridview Selected Book's Details :</th>
         </tr>
         <tr>
          <td>Book ID</td>
          <td><asp:TextBox ID="txtgetbid" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
          <td>Book Name</td>
          <td><asp:TextBox ID="txtgetbname" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
          <td>Book Author</td>
          <td><asp:TextBox ID="txtgetbauthor" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
          <td>Book Publisher Name</td>
          <td><asp:TextBox ID="txtgetbpubname" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
          <td>Book Published Date</td>
          <td><asp:TextBox ID="txtgetbpubdate" runat="server"></asp:TextBox></td>
         </tr>
        </table>
    
    </div>
    <asp:Label ID="lblerror" runat="server"></asp:Label>
    </form>
</body>
</html>

Gridview Properties

  • Allowing Paging : True
  • Page Size : 3
  • Double Click on PageIndexChanging Event

CSS Code


<style>
     #table
     {
         border-collapse:collapse;
         width:450px;
     }
     #table th
     {
         border:1px solid darkblue;
         background:darkblue;
         width:450px;
         color:#fafafa;
         padding:5px 15px 5px 15px;
         text-align:left;
     }     
     #table tr td:nth-childundefined1)
     { 
         border:1px solid darkblue;
         background:#ddd;
         width:180px;
         text-align:left;
         padding:5px 10px 5px 10px;
     }
     #table tr td:nth-childundefined2)
     {          
         width:270px;
         border:1px solid darkblue;
         text-align:left;
         padding-left:10px;
     }
    </style>

C# Code

C# Coding : Namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

C# Coding : Page Load Event

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCon"].ToString());
    string query;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGridData();
        }
    }
}

C# Code : Bind Gridview Control

this method is used to get whole data from database table and binding that data into gridview control.

    private void FillGridData()
    {
        query = "select * from Book";
        SqlDataAdapter da=new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "book");
        GridView1.DataSource = ds;
        GridView1.DataBind();        
    }

C# Code : Gridview PageIndexChanging Event

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        FillGridData();
    }

C# Code : Gridview CheckBox Checked Control Event

this is the most important method for this articles, because this articles was made based on this method creation. The main purpose of this method is how to perform gridview to catch or get gridview selected row data and display the same data into Textbox from outside of the gridview control.

    protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (((CheckBox)row.FindControl("chk")).Checked == true)
            {
                Label lblbookid = (Label)row.FindControl("lblbookid");
                Label lblbookname = (Label)row.FindControl("lblbookname");
                Label lblbookauthor = (Label)row.FindControl("lblbookauthor");
                Label lblbookpubname = (Label)row.FindControl("lblbookpubname");
                Label lblbookpubdate = (Label)row.FindControl("lblbookpubdate");
                txtgetbid.Text = lblbookid.Text;
                txtgetbname.Text = lblbookname.Text;
                txtgetbauthor.Text = lblbookauthor.Text;
                txtgetbpubname.Text = lblbookpubname.Text;
                txtgetbpubdate.Text = lblbookpubdate.Text;
            }
        }

    }
0 Komentar untuk "Gridview Checkbox Selected Row Data Display in Textbox"

Back To Top