Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
ASP.NET gridview dropdown with related records in textbox
WhatsApp
Satyapriya Nayak
13y
42
k
0
4
25
Blog
Dropdown textbox with gridview.zip
Here we add the Grid View control to the web page, then add four template columns: one for the Dropdown List and three for the Textbox's. Set the AutoPostBack property to "True" for the Dropdown List, and create the SelectedIndexChanged event.
Now we have to handle the SelectedIndexChanged event of the DropDownList. Here we have to detect the exact Row of the GridView from where the SelectedIndexChanged event is fired, so we have to compare the "ClientID" of the DropDownList with the dropdowns available in all the rows of the GridView. Once the CliendID is matched, we can bind the TextBoxs with the data of that Row.So here we bind the dropdownlist with one column data and when user select any value its corresponding records are automatic bind with the TextBoxs present inside the gridview.
Table creation with data :
The Default.aspx code is given below :
<%
@
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
>
Untitled Page
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server">
<
div
>
<
asp
:
GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="false"
OnRowDataBound
="GridView1_RowDataBound">
<
Columns
>
<
asp
:
TemplateField
HeaderText
="EmployeeID">
<
ItemTemplate
>
<
asp
:
DropDownList
Width
="100"
runat
="server"
id
="ddlTest"
AutoPostBack
="true"
OnSelectedIndexChanged
="ddlTest_SelectedIndexChanged">
</
asp
:
DropDownList
>
</
ItemTemplate
>
</
asp
:
TemplateField
>
<
asp
:
TemplateField
HeaderText
="FirstName">
<
ItemTemplate
>
<
asp
:
TextBox
ID
="txt1"
runat
="server"></
asp
:
TextBox
>
</
ItemTemplate
>
</
asp
:
TemplateField
>
<
asp
:
TemplateField
HeaderText
="LastName">
<
ItemTemplate
>
<
asp
:
TextBox
ID
="txt2"
runat
="server"></
asp
:
TextBox
>
</
ItemTemplate
>
</
asp
:
TemplateField
>
<
asp
:
TemplateField
HeaderText
="Address">
<
ItemTemplate
>
<
asp
:
TextBox
ID
="txt3"
runat
="server"></
asp
:
TextBox
>
</
ItemTemplate
>
</
asp
:
TemplateField
>
</
Columns
>
</
asp
:
GridView
>
</
div
>
</
form
>
</
body
>
</
html
>
The Default. aspx.cs code is given below:
using
System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
public
partial
class
_Default
: System.Web.UI.
Page
{
string
connStr =
ConfigurationManager
.ConnectionStrings[
"ConnectionString"
].ConnectionString;
SqlDataAdapter
sqlda =
new
SqlDataAdapter
();
SqlCommand
com =
new
SqlCommand
();
DataTable
dt;
string
str;
protected
void
Page_Load(
object
sender,
EventArgs
e)
{
if
(!IsPostBack)
{
bindgrid();
}
}
private
void
bindgrid()
{
SqlConnection
conn =
new
SqlConnection
(connStr);
dt =
new
DataTable
();
com.Connection = conn;
com.CommandText =
"SELECT * FROM Employees"
;
sqlda =
new
SqlDataAdapter
(com);
sqlda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected
void
GridView1_RowDataBound(
object
sender,
GridViewRowEventArgs
e)
{
if
(e.Row.RowType ==
DataControlRowType
.DataRow)
{
Control
ctrl = e.Row.FindControl(
"ddlTest"
);
if
(ctrl !=
null
)
{
DropDownList
dd = ctrl
as
DropDownList
;
SqlConnection
conn =
new
SqlConnection
(connStr);
dt =
new
DataTable
();
com.Connection = conn;
com.CommandText =
"SELECT * FROM Employees"
;
sqlda =
new
SqlDataAdapter
(com);
sqlda.Fill(dt);
dd.DataTextField =
"EmployeeID"
;
dd.DataValueField =
"EmployeeID"
;
dd.DataSource = dt;
dd.DataBind();
}
}
}
protected
void
ddlTest_SelectedIndexChanged(
object
sender,
EventArgs
e)
{
DropDownList
ddl = sender
as
DropDownList
;
foreach
(
GridViewRow
row
in
GridView1.Rows)
{
Control
ctrl = row.FindControl(
"ddlTest"
)
as
DropDownList
;
if
(ctrl !=
null
)
{
DropDownList
ddl1 = (
DropDownList
)ctrl;
if
(ddl.ClientID == ddl1.ClientID)
{
TextBox
txt1 = row.FindControl(
"txt1"
)
as
TextBox
;
TextBox
txt2 = row.FindControl(
"txt2"
)
as
TextBox
;
TextBox
txt3 = row.FindControl(
"txt3"
)
as
TextBox
;
SqlConnection
conn =
new
SqlConnection
(connStr);
conn.Open();
str =
"select * from Employees where EmployeeID='"
+ ddl1.SelectedItem.Text +
"'"
;
com =
new
SqlCommand
(str, conn);
SqlDataReader
reader = com.ExecuteReader();
while
(reader.Read())
{
txt1.Text = reader[
"FirstName"
].ToString();
txt2.Text = reader[
"LastName"
].ToString();
txt3.Text = reader[
"Address"
].ToString();
}
reader.Close();
conn.Close();
}
}
}
}
Output
Thanks for reading
ASP.NET gridview dropdown with related records in textbox
Up Next
Nested GridView In ASP.NET Using C# (Parent And Child Gridview)
Ebook Download
View all
Diving Into ASP.NET WebAPI
Read by 21.4k people
Download Now!
Learn
View all
Membership not found