Bind Data to asp.net textbox control in inside of gridview Using C# Example

Bind Data to asp.net textbox control in inside of gridview:



In this post we describe how to bind textbox control which is use to inside the grid.In the past post we learn how to bind dropdownlist and how to use dropdownlist with in grid. And also give a example of tooltip on control. In this tutorial blog we give many Examples rerated to grid view these are :

Gridview Related Post:

Example of C# for Bind Data


Now we consider one more gridview example in which we consider textbox with in grid and bind it by sql data table.

Bind Data to asp.net textbox:

Consider a table which has two columns StateID and StateName. Make sql connection and fetch data form sql server.

Code for textbox binding Example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Exampleoftemplate.aspx.cs" Inherits="Exampleoftemplate" %>

<!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>textbox control in inside of gridview</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <br />       
            <h3>Bind Data to asp.net textbox control in inside of gridview Example </h3>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            Height="258px" Width="559px" >
        <Columns>
         <asp:TemplateField HeaderText="SNo" ItemStyle HorizontalAlign="Center">
                            <ItemTemplate>
                                <%#Container.DataItemIndex+1 %>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
         <asp:TemplateField HeaderText="State ID" ItemStyle HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:Label ID="lblStateID" runat="server" Text='<%# Eval("StateID") %>'></asp:Label>
                               
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
         <asp:TemplateField HeaderText="Name of State" ItemStyle HorizontalAlign="Center">
                            <ItemTemplate>
                               
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("StateName") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                      

        </Columns>
            <HeaderStyle BackColor="#3333FF" BorderColor="#FF99FF" BorderStyle="Groove"
                BorderWidth="1px" ForeColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

C# Code for textbox control binding Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Exampleoftemplate : System.Web.UI.Page
{
    string _connectionstring;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _connectionstring = @"Data Source=PARIJAT PC\PARIJAT;Initial Catalog=Database1;Integrated Security=True;Pooling=False";// create connection....
            BindData();// make function .......and call here........
        }
    }

    private void BindData()
    {
        try
        {

            string _sql = "select StateID,StateName from States";//sql Query
            SqlConnection _connection = new SqlConnection(_connectionstring);
            SqlCommand _command = new SqlCommand(_sql, _connection);
            SqlDataAdapter _adapter = new SqlDataAdapter(_command);
            DataTable datatable = new DataTable();
            _adapter.Fill(datatable);

            GridView1.DataSource = datatable;  //bind grid .........here                              GridView1.DataBind();

        }
        catch
        {
            throw;
        }
    }
   
}

Other Asp.net Related Post:


Comments

Popular posts from this blog