Introduction:
In
this article I will explain how to bind data to textbox in inside of gridview in
asp.net.
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. |
|||
|
|||
11 comments :
dhdhdh
hi
Text='<%# Eval("Location") %>'
can't use in vb.net
jghjh
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??
hi..
Can you please tell me now how to save the column "location" in different table.
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
Hi Mr.Suresh
How can we edit update delete using textbox in girdview .
Thankyou
Thanks!!
hiii i want to add one column in gridview which contains textbox inside it and how to retrive value entered by user into textbox...plz rply
Note: Only a member of this blog may post a comment.