Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Programmatically assign dropdownList to a GridView control

Reply
Thread Tools

Programmatically assign dropdownList to a GridView control

 
 
Philjo
Guest
Posts: n/a
 
      09-20-2006
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


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      09-20-2006
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>
<aspropDownList
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
Philjo
Guest
Posts: n/a
 
      09-20-2006
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 [Microsoft MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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>
> <aspropDownList
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: how to programmatically assign text to a label control within a da ASP .Net 1 06-06-2006 09:40 AM
how to programmatically assign text to a label control within a datalist template? ASP .Net 1 06-05-2006 07:37 PM
GridView: Filter DropDownList for another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-29-2005 07:44 AM
GridView: Filtr DropDownList from another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-23-2005 01:31 PM
User Control Dropdownlist: can't assign value to JJ_377@hotmail.com ASP .Net 0 07-22-2005 12:17 AM



Advertisments