Here I am Explaining How To filter Grid View By using Drop Down List and Performing Edit and Update In Grid View.
1.Design The .aspx page like below with two drop dowm lists and grid view and sql data source and and configure the sql data source like below.
1.Design The .aspx page like below with two drop dowm lists and grid view and sql data source and and configure the sql data source like below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td align="left" style="padding-left: 5px; padding-bottom: 10px;">
<asp:DropDownList ID="ddlcarsyear" AutoPostBack="true" runat="server" CssClass="chzn-select input-deal-status"
Width="180px">
<asp:ListItem>2001</asp:ListItem>
<asp:ListItem>2002</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlcarsmodel" runat="server" AutoPostBack="true" CssClass="chzn-select input-deal-status"
Width="180px">
<asp:ListItem>Benz</asp:ListItem>
<asp:ListItem>Skoda</asp:ListItem>
<asp:ListItem>TataNano</asp:ListItem>
<asp:ListItem>Safari</asp:ListItem>
</asp:DropDownList>
<div class="clear">
</div>
</td>
</tr>
<tr>
<td align="center">
<asp:GridView ID="gridmsrp" Width="100%" runat="server" GridLines="None" HeaderStyle-BorderColor="#454545"
CssClass="Gridview" BorderStyle="None"
BorderWidth="0px"
DataSourceID="sqldatasourcecars"
AutoGenerateColumns="False"
DataKeyNames="cid"
EmptyDataText="There
Is No Data To Show.">
<Columns>
<asp:CommandField ShowEditButton="True"
ButtonType="Image"
EditImageUrl="~/img/btn-edit.png"
UpdateImageUrl="~/img/update_admin.png"
CancelImageUrl="~/img/Cancel_admin.png"
/>
<asp:BoundField DataField="cid"
HeaderText="CID"
ReadOnly="True"
Visible="False"
HeaderStyle-HorizontalAlign="Left"
HeaderStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Left"
ItemStyle-VerticalAlign="Middle"
/>
<asp:BoundField DataField="year"
HeaderText="YEAR"
ReadOnly="True"
HeaderStyle-HorizontalAlign="Left"
HeaderStyle-VerticalAlign="Middle"
ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField DataField="model"
HeaderText="Model"
ReadOnly="True"
HeaderStyle-HorizontalAlign="Left"
HeaderStyle-VerticalAlign="Middle"
ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle" />
<asp:TemplateField HeaderText="Price">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Bind("price") %>' Width="60"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidatormsrp1"
runat="server"
ControlToValidate="TextBox1"
Display="Dynamic"
ErrorMessage="This
Field Not Empty!" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatormsrp2"
runat="server"
ControlToValidate="TextBox1"
Display="Dynamic"
ErrorMessage="This
Field Allows Only Numbers!"
ForeColor="Red"
ValidationExpression="(^([0-9.]*\d*\d{1}?\d*)$)"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("price") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
<ItemStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
</asp:TemplateField>
<asp:TemplateField HeaderText="DESTINATION">
<EditItemTemplate>
<asp:TextBox ID="TextBox2"
runat="server"
Text='<%# Bind("destination") %>'></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidatormsrp7"
runat="server"
ControlToValidate="TextBox2"
Display="Dynamic"
ErrorMessage="This
Field Not Empty!" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatormsrp8"
runat="server"
ControlToValidate="TextBox2"
Display="Dynamic"
ErrorMessage="This
Field Allows Only Numbers!"
ForeColor="Red"
ValidationExpression="(^([0-9.]*\d*\d{1}?\d*)$)"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("destination") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
<ItemStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tax">
<EditItemTemplate>
<asp:TextBox ID="TextBox3"
runat="server"
Text='<%# Bind("tax") %>'></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidatormsrp9"
runat="server"
ControlToValidate="TextBox3"
Display="Dynamic"
ErrorMessage="This
Field Not Empty!" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatormsrp10"
runat="server"
ControlToValidate="TextBox3"
Display="Dynamic"
ErrorMessage="This
Field Allows Only Numbers!"
ForeColor="Red"
ValidationExpression="(^([0-9.]*\d*\d{1}?\d*)$)"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%# Bind("tax") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
<ItemStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
</asp:TemplateField>
<asp:TemplateField HeaderText="TOTAL">
<EditItemTemplate>
<asp:TextBox ID="TextBox4"
runat="server"
Text='<%# Bind("total") %>'></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidatormsrp5"
runat="server"
ControlToValidate="TextBox4"
Display="Dynamic"
ErrorMessage="This
Field Not Empty!" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatormsrp6"
runat="server"
ControlToValidate="TextBox4"
Display="Dynamic"
ErrorMessage="This
Field Allows Only Numbers!"
ForeColor="Red"
ValidationExpression="(^([0-9.]*\d*\d{1}?\d*)$)"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server"
Text='<%# Bind("total") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
<ItemStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
</asp:TemplateField>
</Columns>
<EmptyDataRowStyle BorderWidth="0"
BorderStyle="None"
HorizontalAlign="Center"
VerticalAlign="Middle"
Height="50" />
<HeaderStyle BorderColor="#454545"
CssClass="gridleaseheader"
Height="50px"
HorizontalAlign="Left"
VerticalAlign="Middle"
/>
<RowStyle CssClass="gridtd"
Height="50px"
HorizontalAlign="Left"
VerticalAlign="Middle"
/>
</asp:GridView>
<asp:SqlDataSource ID="sqldatasourcecars" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:sampledbConnectionString %>" SelectCommand="SELECT [cid], [year], [model], [price],
[destination], [tax], [total] FROM [cars] WHERE (([year] = @year) AND ([class]
= @class))"
UpdateCommand="UPDATE [cars] SET
[price] = @price, [destination] = @destination, [tax] = @tax, [total] = @total
WHERE [cid] = @original_cid"
OldValuesParameterFormatString="original_{0}">
<SelectParameters>
<asp:ControlParameter ControlID="ddlcarsyear" Name="year" PropertyName="SelectedValue"
Type="String"
/>
<asp:ControlParameter ControlID="ddlcarsmodel"
Name="Model"
PropertyName="SelectedValue"
Type="String"
/>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="price"
Type="Double"
/>
<asp:Parameter Name="destination"
Type="Double"
/>
<asp:Parameter Name="tax" Type="Double"
/>
<asp:Parameter Name="total"
Type="Double"
/>
<asp:Parameter Name="original_cid"
Type="Int32"
/>
</UpdateParameters>
</asp:SqlDataSource>
<div class="clear">
</div>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Then run the page........k any doubts comment here bye c u happy coding...
0 comments :