Page view counter

Drop Down List in a GRIDVIEW

Last post 10-07-2008 5:30 AM by priyappated. 15 replies.

Sort Posts:

  • Drop Down List in a GRIDVIEW

    09-30-2008, 3:27 PM

    Hello All,

    i have a gridview with columns like ItemNo, Title, Price, Size. everything is coming from SQL DB. what i am trying to do is to display a DDL in the gridview so that both the price and Size are diplayed in a DDL format. to explain clearly right now for every itemno, i have different sizes namely 1,2,3,4,5 etc and a different price according to the sizes. for example

    itemno     title                  price        size

    A1001   Red Shoes         $15           2

    A1001  Red Shoes          $20           4

    A1001  Red Shoes          $25           7       and so on. 

    as you can see its displaying the title and itemno so many items. i was wondering if there's way to just display itemno and title ( just once ) and their respective prices and sizes in a DDL's. something

    like below.

    itemNo          title              price     size

    A1001           Red Shoes     $15      2

                                             $20       4

                                             $25       7

    here is the complete code i have so far.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GVsearch.aspx.cs" Inherits="GVsearch" %>

    <!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>SQL and GV seach page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:TextBox id="txtSearch" runat="server" />

    <asp:Button id="btnSearch" runat="server"

    Text = "Search"

    OnClick = "btnSearch_OnClick" />

    <br />

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LocalDBConnectionString %>"

    SelectCommand="SELECT [ItemNumber], [Title], [Price], [SizeName],[Description],[ShortTitle],[ShortDescription] FROM [SearchTable] WHERE (([Title] LIKE '%' + replace(@Title, ' ', '%') + '%') OR ([ShortDescription] LIKE '%' + replace(@ShortDescription, ' ', '%') + '%') OR ([ShortTitle] LIKE '%' + replace(@ShortTitle, ' ', '%') + '%') OR ([Description] LIKE '%' + replace(@Description, ' ', '%') + '%') OR ([ItemNumber] LIKE '%' + replace(@ItemNumber, ' ', '%') + '%') OR ([SizeName] LIKE '%' + replace(@SizeName, ' ', '%') + '%'))" OnSelecting="SqlDataSource1_Selecting" >

    <SelectParameters>

    <asp:Parameter Name="Title" Type="String" />

    <asp:Parameter Name="ShortDescription" Type="String" />

    <asp:Parameter Name="ShortTitle" Type="String" />

    <asp:Parameter Name="Description" Type="String" />

    <asp:Parameter Name="ItemNumber" Type="String" />

    <asp:Parameter Name="SizeName" Type="String" />

    </SelectParameters>

    </asp:SqlDataSource>

    <br />

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"

    CellPadding="4" DataKeyNames="ItemNumber" DataSourceID="SqlDataSource1" ForeColor="#333333"

    GridLines="None" AllowSorting="True">

    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

    <Columns>

    <asp:BoundField DataField="ItemNumber" HeaderText="Item" InsertVisible="False"

    SortExpression="ItemNumber" HtmlEncode="False" />

    <asp:BoundField DataField="Title" HeaderText="Product Name" SortExpression="Title" HtmlEncode="False" />

    <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" DataFormatString="{0:c}" HtmlEncode="False" />

    <asp:BoundField DataField="SizeName" HeaderText="Size" SortExpression="SizeName" HtmlEncode="False" />

    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" HtmlEncode="False" />

     

    </Columns>

    <RowStyle BackColor="#E3EAEB" />

    <EditRowStyle BackColor="#7C6F57" />

    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

    <AlternatingRowStyle BackColor="White" />

    </asp:GridView>

     

     

    </div>

     

    </form>

    </body>

    </html>

    --------------------------- CODE BEHIND------------

     

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class GVsearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }
    
    
    
        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@Title"].Value = txtSearch.Text;
            e.Command.Parameters["@ShortDescription"].Value = txtSearch.Text;
            e.Command.Parameters["@ShortTitle"].Value = txtSearch.Text;
            e.Command.Parameters["@Description"].Value = txtSearch.Text;
            e.Command.Parameters["@ItemNumber"].Value = txtSearch.Text;
            e.Command.Parameters["@SizeName"].Value = txtSearch.Text;
         
        }
    }
    
     

    any ideas or suggestions are highly appreciated.

    Thanks once again

  • Re: Drop Down List in a GRIDVIEW

    09-30-2008, 4:07 PM
    • Loading...
    • renitdaniel
    • Joined on 08-03-2008, 8:09 PM
    • Posts 75
    • Points 171

    Hey there

    Check this link out http://www.webswapp.com/codesamples/aspnet20/dropdownlist_gridview/default.aspx

    The coding is given in the links below the page. Im assuming it is similar to what you are trying to implement.

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 10:22 AM

     Hello there,

    i am still stuck, all i am trying to is for every itemNo in the gridview i want to display its (itemno)  sizes in dropdownlist. is there anyway to do this.

    i really appreciate it.

    Thanks

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 11:02 AM
    • Loading...
    • renitdaniel
    • Joined on 08-03-2008, 8:09 PM
    • Posts 75
    • Points 171

     

    Try something like this:

    <asp:GridView ID="GridView1" runat="server" DataKeyName="ItemNo" DataSource = "DataSource_Name">

    <asp:TemplateField>

    <ItemTemplate>

    <asp:Label ID="lblItemNo" runat="server" Text='<%# Bind("ItemNo") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

     

    <asp:TemplateField HeaderText="Sizes" >

    <ItemTemplate>

    <asp:DropDownList ID="ddSizes" runat="server" DataTextField="Size" DataValueField="ItemNo" AppendDataBoundItems="true"

    Text="<%#Bind('Size') %>">

    </asp:DropDownList>

    </ItemTemplate>

    </asp:TemplateField>

    </asp:GridView>

     

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 12:10 PM

     Hello Sir,

    i tried the way you have adviced, i am getting the following error

    'ddlSizes' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    any suggestions sir

    Thanks i appreciate it

     

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 12:30 PM

    newtoaspnet2008:

     Hello Sir,

    i tried the way you have adviced, i am getting the following error

    'ddlSizes' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    any suggestions sir

    Thanks i appreciate it

     

    u cant use "Text" property . there is no text property for dropdownlist . u have to use "SelectedValue"

    -

    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.
  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 12:35 PM
    • Loading...
    • renitdaniel
    • Joined on 08-03-2008, 8:09 PM
    • Posts 75
    • Points 171

    Yes he is right, try SelectedValue.. 'text' is not a property of DDL, my bad

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 12:46 PM

    Hello Sir,

    if i try like SelectedValue="<%# Bind('SizeName') %> i am getting the following error Crying

    'ddlSizes' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentOutOfRangeException: 'ddlSizes' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Source Error:

    Line 20:     {
    Line 21:         SearchPanel.Visible = true;
    Line 22:         GridView1.DataBind();
    Line 23: 
    Line 24:        

    Thanks I appreciate it

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 1:06 PM
    • Loading...
    • renitdaniel
    • Joined on 08-03-2008, 8:09 PM
    • Posts 75
    • Points 171

    Could you put a breakpoint in there and tell me what value ou are receiving?

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 1:33 PM
    • Loading...
    • engineerachu
    • Joined on 10-24-2007, 12:05 PM
    • Chennai, TamilNadu, India
    • Posts 587
    • Points 1,912

     Can you post the code?

    Cheers,
    Achutha Krishnan

    ~ No one can do everything, but everyone can do something ~
  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 1:48 PM
    • Loading...
    • nikhil.jha
    • Joined on 01-04-2008, 6:18 PM
    • Posts 22
    • Points 114

    Hi,

    You may add the the items to the drop down lists inside GridView1_RowDataBound(object sender, GridViewRowEventArgs e). Place a condition as -

    if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)

    {

    }

    Now within this if block, loop through the items and add Price as well as Size to the corresponding dropdownlist.

    Dont bind the dropdownlists in the gridview definition itself if you are going for this approach.

    Hope this helps. Smile  

  • Re: Drop Down List in a GRIDVIEW

    10-02-2008, 1:56 PM

    Hello All,

    here is the complete code: and regarding breakpoint this might be very silly and dumb but there is no option to insert a breakpoint in ASPX page. i know how to do it the code behind page(ASPX.CS).

    if i run it in debug mode it just show the same error what i posted earlier.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DDLinGV.aspx.cs" Inherits="DDLinGV" %>

    <!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:TextBox id="txtSearch" runat="server" />

    <asp:Button id="btnSearch" runat="server"

    Text = "Search"

    OnClick = "btnSearch_OnClick" />

    <br />

    <asp:Panel ID="SearchPanel" runat="server" Visible="false">

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WSLocalDBConnectionString %>"

    SelectCommand="SELECT [ItemNumber], [Title], [Price], [SizeName],[Description],[ShortTitle],[ShortDescription],[TitleImage],[ThumbnailImage] FROM [SearchTable] WHERE (([Title] LIKE '%' + replace(@Title, ' ', '%') + '%') OR ([ShortDescription] LIKE '%' + replace(@ShortDescription, ' ', '%') + '%') OR ([ShortTitle] LIKE '%' + replace(@ShortTitle, ' ', '%') + '%') OR ([Description] LIKE '%' + replace(@Description, ' ', '%') + '%') OR ([ItemNumber] LIKE '%' + replace(@ItemNumber, ' ', '%') + '%') OR ([SizeName] LIKE '%' + replace(@SizeName, ' ', '%') + '%'))" OnSelecting="SqlDataSource1_Selecting" >

    <SelectParameters>

    <asp:Parameter Name="Title" Type="String" />

    <asp:Parameter Name="ShortDescription" Type="String" />

    <asp:Parameter Name="ShortTitle" Type="String" />

    <asp:Parameter Name="Description" Type="String" />

    <asp:Parameter Name="ItemNumber" Type="String" />

    <asp:Parameter Name="SizeName" Type="String" />

    </SelectParameters>

    </asp:SqlDataSource>

    <br />

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"

    CellPadding="4" DataKeyNames="ItemNumber" DataSourceID="SqlDataSource1" ForeColor="#333333"

    GridLines="None" AllowSorting="True">

    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

    <Columns>

    <asp:BoundField DataField="ItemNumber" HeaderText="Item" InsertVisible="False"

    SortExpression="ItemNumber" HtmlEncode="False" />

    <asp:BoundField DataField="Title" HeaderText="Product Name" SortExpression="Title" HtmlEncode="False" />

    <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" DataFormatString="{0:c}" HtmlEncode="False" />

    <asp:BoundField DataField="SizeName" HeaderText="Sizes" SortExpression=SizeName HtmlEncode="False" />

    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" HtmlEncode="False" />

    <asp:TemplateField HeaderText="Available Sizes">

    <ItemTemplate>

    <asp:DropDownList ID="ddlSizes" runat="server" DataTextField="SizeName" AppendDataBoundItems="true"

    DataValueField="ItemNumber" SelectedValue="<%# Bind('SizeName') %>" />

     

    </ItemTemplate>

    </asp:TemplateField>

     

    </Columns>

    <RowStyle BackColor="#E3EAEB" />

    <EditRowStyle BackColor="#7C6F57" />

    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

    <AlternatingRowStyle BackColor="White" />

    </asp:GridView>

     

     

     

    </asp:Panel>

    </div>

    </form>

    </body>

    </html>

     

    ------------------ CODE BEHIND ------------

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class DDLinGV : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            SearchPanel.Visible = true;
            GridView1.DataBind();
    
           
        }
    
        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@Title"].Value = txtSearch.Text;
            e.Command.Parameters["@ShortDescription"].Value = txtSearch.Text;
            e.Command.Parameters["@ShortTitle"].Value = txtSearch.Text;
            e.Command.Parameters["@Description"].Value = txtSearch.Text;
            e.Command.Parameters["@ItemNumber"].Value = txtSearch.Text;
            e.Command.Parameters["@SizeName"].Value = txtSearch.Text;
    
    
                }
    }
    

    Thanks a lot, i really appreciate it.

  • Re: Drop Down List in a GRIDVIEW

    10-03-2008, 3:02 AM
    Answer

    Hi newtoaspnet2008,

    Have you initialized DropDownList bound with a data source? If not, you cannot set SelectedValue for it before bound it, because there is no value in it and how the selected one comes. So I think you need to give a data source to DropDownList by setting DataSourceID with data source control or setting manually in RowDataBound event of GridView.

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Drop Down List in a GRIDVIEW

    10-04-2008, 7:16 AM
    • Loading...
    • nikhil.jha
    • Joined on 01-04-2008, 6:18 PM
    • Posts 22
    • Points 114

    Hi,

    As mentioned in my previous reply, you can achieve the desired result programmatically. For this, do not bind the columns of the gridview statically but bind them dynamically inside GridView1_RowDataBound as explained earlier. Get the records in a dataset. Inside GridView1_RowDataBound, assign the values to all the labels (Title, Price etc.) and then loop through the records for adding the corresponding Sizes to ddlSizes. Repeat the same for remaining titles. 

  • Re: Drop Down List in a GRIDVIEW

    10-06-2008, 9:52 AM

     Hello Sir,

    is there any possibility that you can post a sample or example. I really appreciate it.

    Thanks 

Page 1 of 2 (16 items) 1 2 Next >