Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

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

May 26, 2012
Introduction:

In this article I will explain how to bind data to textbox in inside of gridview in asp.net.

Description:

In previous post I explained how to bind xml data to dropdownlist or gridview in asp.net. Now I will explain how to bind data to textbox in gridview in asp.net. To implement this concept first design your aspx page like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Asp.net Bind Data to Textbox and dropdownlist in gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:TextBox ID="txtLocation" runat="server" BackColor="Orange" Text='<%# Eval("Location") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Now add following namespaces in codebehind

C# Code


using System;
using System.Data;
using System.Data.SqlClient;
After that add following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select TOP 10 UserName,LastName,Location from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
VB.NET Code


Imports System.Data
Imports System.Data.SqlClient
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
' This method is used to bind gridview from database
Protected Sub BindGridview()
Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select TOP 10 UserName,LastName,Location from UserInformation", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
End Sub
End Class
Demo




If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

10 comments :

Anonymous said...

dhdhdh

sandeep kumar said...

hi

Plugfai Pankaew said...

Text='<%# Eval("Location") %>'
can't use in vb.net

Anonymous said...

jghjh

csharpsence said...

WHAT IS THIS CODE
Text='<%# Eval("Location") %>' ..

IN C# I M GETTING ERROR IF I KEEP THIS, AND GETTING OUTPUT IF I REMOVE THIS. CAN ANYONE EXPLAIN PLEASE??

Anonymous said...

hi..
Can you please tell me now how to save the column "location" in different table.

DIVINE DIGVIJAY said...

sir agar link button k click pe lastname ek alag textbox me ajaye jo gridview se alag asp page pe ho...kese hoga??? plz help me

osama ahmed said...
This comment has been removed by the author.
osama ahmed said...

Hi Mr.Suresh
How can we edit update delete using textbox in girdview .
Thankyou

Rebe Arias said...

Thanks!!

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.