Programmatically assign dropdownList to a GridView control

Discussion in 'ASP .Net Datagrid Control' started by Philjo, Sep 20, 2006.

  1. Philjo

    Philjo Guest

    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, Sep 20, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    >
    Ken Cox [Microsoft MVP], Sep 20, 2006
    #2
    1. Advertising

  3. Philjo

    Philjo Guest

    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]" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    >> 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, Sep 20, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    821
  2. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,846
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,281
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  4. Guest
    Replies:
    1
    Views:
    917
    =?Utf-8?B?dGlta2xpbmc=?=
    Jun 5, 2006
  5. Guest
    Replies:
    1
    Views:
    581
    Walter Wang [MSFT]
    Jun 6, 2006
Loading...

Share This Page