Programmatically assign dropdownList to a GridView control

P

Philjo

Folks,



I am looking for some good samples which describe how we can
programmatically assign the following stuffs to a GridView control.



1. Association of a dropdownList with its data source to a grid column using
EditItemTemplate

2. Can I change the auto generated Edit, Update or Delete buttons in the
grid to an Image?



Thanks in advance

Philjo
 
K

Ken Cox [Microsoft MVP]

Hi Philjo,

When you say "programmatically" do you mean adding the code based on an
event? Otherwise, it is quite easy to include a dropdownlist that's bound to
a field. See the code below.

Likewise, what do you mean by "autogenerated"? You can uses images in
template columns.

Perhaps you could show us the code you have so far?

Anyway, I've inserted some code below in case it helps.

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="hd" runat="server">
<title>Dropdownlist in GridView - Ken Cox [MVP]</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView
id="gvProducts"
DataSourceID="sqldsProducts"
DataKeyNames="ProductId"
AutoGenerateColumns="False"
AutoGenerateEditButton="False"
Runat="server">
<Columns>
<asp:templatefield showheader="False">
<edititemtemplate>
<asp:linkbutton id="LinkButton1" runat="server"
causesvalidation="True" commandname="Update" text="Update"></asp:linkbutton>
<asp:linkbutton id="LinkButton2" runat="server"
causesvalidation="False" commandname="Cancel"
text="Cancel"></asp:linkbutton>
</edititemtemplate>
<itemtemplate>
<asp:imagebutton id="ImageEdit" runat="server"
causesvalidation="False" commandname="Edit"
imageurl="http://eus.amrinteractive.com.au/EUS/config/Edit.JPG" />
</itemtemplate>
</asp:templatefield>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<%#Eval("ProductName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox id="txtProductName"
runat="server"
text='<%# Bind("ProductName")%>'>
</asp:textbox>
<asp:requiredfieldvalidator
id="rqdProductName"
runat="server"
controltovalidate="txtProductName"
text="Please provide a product name.">
</asp:requiredfieldvalidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<%#Eval("CategoryName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
id="ddlCategory"
DataSourceID="sqldsCategories"
DataTextField="CategoryName"
DataValueField="CategoryId"
SelectedValue='<%#Bind("CategoryId")%>'
Runat="server" />
</EditItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

<asp:sqldatasource id="sqldsProducts"
runat="server"
connectionstring=
'<%$ ConnectionStrings:NorthwindConnectionString %>'
selectcommand="SELECT Products.ProductId,
Products.ProductName, Products.CategoryId,
Categories.CategoryName
FROM Products JOIN Categories
ON Categories.CategoryId = Products.CategoryId"
updatecommand="UPDATE Products SET ProductName=@ProductName,
CategoryId=@CategoryId
WHERE ProductId=@ProductId">
</asp:sqldatasource>

<asp:sqldatasource id="sqldsCategories"
runat="server"
connectionstring=
'<%$ ConnectionStrings:NorthwindConnectionString %>'
selectcommand="SELECT CategoryId,
CategoryName FROM Categories">
</asp:sqldatasource>
</div>
</form>
</body>
</html>
 
P

Philjo

Ken,

Thanks for your info.

Basically I am not looking for a code that defined at the design time like
below.

I am curiously looking for a sample code that defines the template columns,
association of dropdownlist to a column while editing etc. from the code
behind using C#.

Also the "autogenerated links", I mentioned below is for the links which are
automatically created by the designer when we set the properties
AutoGenerateDeleteButton="True" / AutoGenerateEditButton="True". I like to
change these links to an Image.

Thanks
Philjo


Ken Cox said:
Hi Philjo,

When you say "programmatically" do you mean adding the code based on an
event? Otherwise, it is quite easy to include a dropdownlist that's bound
to a field. See the code below.

Likewise, what do you mean by "autogenerated"? You can uses images in
template columns.

Perhaps you could show us the code you have so far?

Anyway, I've inserted some code below in case it helps.

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="hd" runat="server">
<title>Dropdownlist in GridView - Ken Cox [MVP]</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView
id="gvProducts"
DataSourceID="sqldsProducts"
DataKeyNames="ProductId"
AutoGenerateColumns="False"
AutoGenerateEditButton="False"
Runat="server">
<Columns>
<asp:templatefield showheader="False">
<edititemtemplate>
<asp:linkbutton id="LinkButton1" runat="server"
causesvalidation="True" commandname="Update"
text="Update"></asp:linkbutton>
<asp:linkbutton id="LinkButton2" runat="server"
causesvalidation="False" commandname="Cancel"
text="Cancel"></asp:linkbutton>
</edititemtemplate>
<itemtemplate>
<asp:imagebutton id="ImageEdit" runat="server"
causesvalidation="False" commandname="Edit"
imageurl="http://eus.amrinteractive.com.au/EUS/config/Edit.JPG" />
</itemtemplate>
</asp:templatefield>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<%#Eval("ProductName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox id="txtProductName"
runat="server"
text='<%# Bind("ProductName")%>'>
</asp:textbox>
<asp:requiredfieldvalidator
id="rqdProductName"
runat="server"
controltovalidate="txtProductName"
text="Please provide a product name.">
</asp:requiredfieldvalidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<%#Eval("CategoryName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
id="ddlCategory"
DataSourceID="sqldsCategories"
DataTextField="CategoryName"
DataValueField="CategoryId"
SelectedValue='<%#Bind("CategoryId")%>'
Runat="server" />
</EditItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

<asp:sqldatasource id="sqldsProducts"
runat="server"
connectionstring=
'<%$ ConnectionStrings:NorthwindConnectionString %>'
selectcommand="SELECT Products.ProductId,
Products.ProductName, Products.CategoryId,
Categories.CategoryName
FROM Products JOIN Categories
ON Categories.CategoryId = Products.CategoryId"
updatecommand="UPDATE Products SET ProductName=@ProductName,
CategoryId=@CategoryId
WHERE ProductId=@ProductId">
</asp:sqldatasource>

<asp:sqldatasource id="sqldsCategories"
runat="server"
connectionstring=
'<%$ ConnectionStrings:NorthwindConnectionString %>'
selectcommand="SELECT CategoryId,
CategoryName FROM Categories">
</asp:sqldatasource>
</div>
</form>
</body>
</html>


Philjo said:
Folks,



I am looking for some good samples which describe how we can
programmatically assign the following stuffs to a GridView control.



1. Association of a dropdownList with its data source to a grid column
using EditItemTemplate

2. Can I change the auto generated Edit, Update or Delete buttons in the
grid to an Image?



Thanks in advance

Philjo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top