Monday, 9 April 2012

How to use DropDownLists in Gridview in Asp.net.

To implement this concept you need to follow the below steps : 

Step1 :First you need to design a table in Sql Database to retrieve the records from database. 

Step2:Create a new Asp.net website in Visual Studio and write the following html code in the design part of the Default.aspx page. 

Source code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
Table.Gridview{border:solid 1px #df5015;}
.GridviewTable{border:none}
.GridviewTable td{margin-top:0;padding: 0; vertical-align:middle }
.GridviewTable tr{color: White; background-color: #df5015; height: 30px; text-align:center}
.Gridview th{color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;padding:0.5em 0.5em 0.5em 0.5em;text-align:center}
.Gridview td{border-bottom-color:#f0f2da;border-right-color:#f0f2da;padding:0.5em 0.5em 0.5em 0.5em;}
.Gridview tr{color: Black; background-color: White; text-align:left}
:link,:visited { color: #DF4F13; text-decoration:none }
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="GridviewDiv">
<table style="width: 420px" border="0" cellpadding="0" cellspacing="1" class="GridviewTable">
<tr >
<td style="width: 40px;">
UserId
</td>
<td style="width: 120px;" >
LastName
</td>
<td style="width: 130px;">
UserName
</td>
<td style="width: 130px;">
Location
</td>
</tr>
<tr >
<td style="width: 40px;">
</td>
<td style="width: 120px;">
</td>
<td style="width: 130px;">
<asp:DropDownList ID="ddlUserName" runat="server" DataSourceID="SqlDataSource1"
        DataValueField="UserName" AutoPostBack="True" Width="120px" Font-Size="11px"
        AppendDataBoundItems="True" DataTextField="UserName">
<asp:ListItem Text="All" Value="%"/>
</asp:DropDownList>
</td>
<td style="width: 130px;">
<asp:DropDownList ID="ddlLocation" runat="server" DataSourceID="SqlDataSource2"
        DataValueField="Location" AutoPostBack="True" Width="120px" Font-Size="11px"
        AppendDataBoundItems="True" DataTextField="Location">
<asp:ListItem Text="All" Value="%"/>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="4">
    <asp:GridView runat="server" ID="gvdetails" ShowHeader="False"
        AllowPaging="True" DataSourceID="SqlDataSource3" AutoGenerateColumns="False"
        Width="420px"  CssClass="Gridview">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" ItemStyle-Width="40px"
        InsertVisible="False" ReadOnly="True" SortExpression="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" ItemStyle-Width="120px"
        SortExpression="UserName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" ItemStyle-Width="130px"
        SortExpression="LastName"/>
<asp:BoundField DataField="Location" HeaderText="Location" ItemStyle-Width="130px"
        SortExpression="Location"/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
           <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>"
        SelectCommand="SELECT DISTINCT [UserName] FROM [UserInfomation]"></asp:SqlDataSource>
         <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>"
        SelectCommand="SELECT DISTINCT [Location] FROM [UserInfomation]"></asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>"
        SelectCommand="SELECT * FROM [UserInfomation]" FilterExpression=" UserName Like '{0}%' and Location Like '{1}%'">
        <FilterParameters>
         <asp:ControlParameter Name="UserName" ControlID="ddlUserName" PropertyName="SelectedValue" />
         <asp:ControlParameter Name="Location" ControlID="ddlLocation" PropertyName="SelectedValue" />
         </FilterParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>

Step3:
Now build the Solution and Debug it for the output.

Output :




No comments:

Post a Comment

CRUD operations using AngularJS in Asp.Net MVC

In this article I will show you how to perform CRUD operations in Asp.Net MVC5 using AngularJS. Add AngularJS using the Manage Nuget Pack...