Groups | Blog | Home
all groups > asp.net datagrid control > september 2006 >

asp.net datagrid control : Programmatically assign dropdownList to a GridView control


Ken Cox [Microsoft MVP]
9/20/2006 10:58:43 AM
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>


[quoted text, click to view]

Philjo
9/20/2006 5:19:29 PM
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

Philjo
9/20/2006 9:28:26 PM
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


[quoted text, click to view]

AddThis Social Bookmark Button