Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want display some datas in gridview based on dropdown selected value
ASP.NET
 <asp:DropDownList ID="ddlStatus" runat="server"  AutoPostBack="true" 
        onselectedindexchanged="ddlStatus_SelectedIndexChanged">
      <asp:ListItem>NewOrder</asp:ListItem>
    <asp:ListItem>Ready</asp:ListItem>
   <asp:ListItem>Canceled</asp:ListItem>
   <asp:ListItem>Shipped</asp:ListItem>
   <asp:ListItem>Delivered</asp:ListItem>
    </asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" GridLines="None" 
        DataKeyNames="OrderId" ForeColor="#333333" Style="position: static"    
        AutoGenerateColumns="False"  CssClass="gridview" 
        onrowdeleting="GridView1_RowDeleting" AllowPaging="True" PageSize="5" 
        onpageindexchanging="GridView1_PageIndexChanging" onrowediting="EditCustomer" 
        onrowupdating="UpdateCustomer" 
        onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowdatabound="GridView1_RowDataBound">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="Lime" />
        <AlternatingRowStyle BackColor="White" />
        <Columns>
        <asp:TemplateField HeaderText="Order No">
        <ItemTemplate>
         <asp:Label ID="lblno" runat="server" Text='<%#Eval("OrderId") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
      <asp:TemplateField HeaderText="Customer Name">
        <ItemTemplate>
         <asp:Label ID="Custname" runat="server" Text='<%#Eval("UserName") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Image">
        <ItemTemplate>
        <asp:Image ID="image" runat="server" ImageUrl='<%# "GridviewImage.ashx?ImID=" + Eval("productid")%>' Width="100" Height="100" />
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="OrderDate">
        <ItemTemplate>
        <asp:Label ID="bname" runat="server" Text='<%#Eval("OrderDate") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="Price">
        <ItemTemplate>
        <asp:Label ID="cost" runat="server" Text='<%#Eval("OrderAmount") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
    <asp:TemplateField HeaderText = "Order Status">
    <ItemTemplate>
        <asp:Label ID="lblstatus" runat="server" Text='<%# Eval("StatusDescription")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
            <asp:Label ID="lblstatus" runat="server" Text='<%# Eval("StatusDescription")%>' Visible = "false"></asp:Label>
    <asp:DropDownList ID = "ddlstatus" runat = "server">
     <asp:ListItem>New Order</asp:ListItem>
    <asp:ListItem>Ready</asp:ListItem>
   <asp:ListItem>Canceled</asp:ListItem>
   <asp:ListItem>Shipped</asp:ListItem>
   <asp:ListItem>Delivered</asp:ListItem>
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
</Columns>
 <EmptyDataTemplate>
<strong>There are no Orders.</strong>
</EmptyDataTemplate>
 </asp:GridView>

My cs code as
C#
private void BindDatastatus()
    {
        try
        {
            SqlCommand com = new SqlCommand("rsa_products_sp_GetadminordersortbyStatus", con);
            com.CommandType = CommandType.StoredProcedure;
            string stat = ddlStatus.SelectedValue.ToString();
            com.Parameters.AddWithValue("@status", stat);
            da = new SqlDataAdapter(com);
            ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.Attributes.Add("bordercolor", "black");
        }
        catch (Exception ex)
        {
        }
    }

C#
protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
   {
       try
       {
           BindDatastatus();
       }
       catch (Exception ex)
       {
       }
   }

My stored Procedure
SQL
CREATE proc [dbo].[rsa_products_sp_GetadminordersortbyStatus] (@status varchar)
as
select OrderId,
OrderDate,OrderAmount,StatusDescription,productid,
isnull((Select [UserName] from rsa_Users WHERE [UserId]=rsa_Order.UserId),'NA')'UserName',
isnull(NetAmount,0)'NetAmount'
from rsa_Order
where active=1 and StatusDescription=@status

GO



Its not showing anything in the grid what i'm doing wrong please help me to solve this.Thanks
Posted
Updated 10-Jun-15 22:06pm
v3
Comments
Herman<T>.Instance 11-Jun-15 3:26am    
put BindDatastatus(); in an
if (!Page.IsPostback)
BindDatastatus();
kwelpooh 11-Jun-15 4:05am    
no its not working
Animesh Datta 11-Jun-15 4:41am    
check whether there is any data exists or not .

1 solution

If you dont have tons of records including all statuses, have all records retrieved once instead of hitting database again and again.

You can then write linq queries to filter data as per your need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900