In this article I will show you how to bind data to textbox
values in asp.net gridview control using c#.net. In this I will use asp.net,
gridview control, gridview itemtemplate and c#.net.
Some of my previous articles are as follows: Code
to Select All Checkbox in GridView in Asp.Net Using jQuery, Highlight
GridView Row on Mouseover Using CSS in Asp.Net C#.Net, Drag
Drop Cells in GridView Control Using Asp.net C# and jQuery, How
To Make a Single Row of DataGridview Bold Using C#.Net in Windows Application,
Code
to Export GridView to PDF in Asp.Net Using C# and Vb.Net, Binding
Gridview By Access DataBase Using C#.Net in Asp.Net.
So for this article first we will create a new asp.net application
and add the gridview control in it.
Now we will make autogeneratd column false
in gridview property.
After this we will add bound column and TemplateField to bind
the Textbox.
After adding the template filed and bound column in your grid will look as shown below
Now we will select
the tip of gridview. In this we will select edit template and add textbox in
itemtemplate. After that we will bind the field.
After that your
fields will look as shown below.
Now here is
your aspx page code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewBindWithTextBox.aspx.cs"
Inherits="ProjectDemo_Asp.et.GridViewBindWithTextBox"
%>
<!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>How to
Bind Data To TextBox Values in Asp.Net GridView Control Using C#.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="author_name" HeaderText="NAME" />
<asp:BoundField DataField="publisher_name" HeaderText="PUBLISHER NAME"
/>
<asp:TemplateField HeaderText="TITLE">
<ItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Eval("title") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
Now add the below
code in your .cs page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
namespace ProjectDemo_Asp.et
{
public partial class GridViewBindWithTextBox : System.Web.UI.Page
{
public
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
protected
void Page_Load(object
sender, EventArgs e)
{
DataTable
_objdt = new DataTable();
_objdt = GetDataFromDataBase();
if
(_objdt.Rows.Count > 0)
{
GridView1.DataSource =
_objdt;
GridView1.DataBind();
}
}
/// <summary>
/// Function for binding the data from database
/// In this i have used Access DB you can use SQL DB to bind
the data
/// </summary>
/// <returns></returns>
public
DataTable GetDataFromDataBase()
{
DataTable
_objdt = new DataTable();
string
querystring = "select * from Books;";
OleDbConnection
_objcon = new OleDbConnection(connectionstring);
OleDbDataAdapter
_objda = new OleDbDataAdapter(querystring,
_objcon);
_objcon.Open();
_objda.Fill(_objdt);
return
_objdt;
}
}
}
|
Now here is the final output.
0 comments:
Please let me know your view