当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to make gridview searchable?

问题描述:

I've got a gridview that draws from multiple tables, I'd like to make it searchable in three fields, one from each table. I plan on using a dropdown to select which field to search for, and a textbox of course for search input, but what then? Here's my gridview code in C# ASP.NET:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"

EmptyDataText="There are no data records to display." AllowPaging="True"

BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"

CellPadding="3" GridLines="Vertical" HorizontalAlign="Center"

DataKeyNames="SubId" AllowSorting="True">

<AlternatingRowStyle BackColor="#DCDCDC" />

<Columns>

<asp:BoundField DataField="SubId" HeaderText="Submission ID"

SortExpression="SubId" >

<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />

</asp:BoundField>

<asp:BoundField DataField="CustName" HeaderText="Customer"

SortExpression="CustName" />

<asp:BoundField DataField="CustCity" HeaderText="Customer City"

SortExpression="CustCity" />

<asp:BoundField DataField="CustState" HeaderText="Customer State"

SortExpression="CustState" />

<asp:BoundField DataField="BroName" HeaderText="Broker "

SortExpression="BroName" />

<asp:BoundField DataField="BroState" HeaderText="Broker State"

SortExpression="BroState" />

<asp:BoundField DataField="EntityType" HeaderText="EntityType"

SortExpression="EntityType" />

<asp:BoundField DataField="Coverage" DataFormatString="{0:c}"

HeaderText="Coverage" SortExpression="Coverage" />

<asp:BoundField DataField="Status" HeaderText="Status"

SortExpression="Status" />

<asp:HyperLinkField DataNavigateUrlFields="SubId"

DataNavigateUrlFormatString="View.aspx?SubId={0}" Text="View" />

<asp:HyperLinkField DataNavigateUrlFields="SubId"

DataNavigateUrlFormatString="ViewEdit.aspx?SubId={0}" Text="Edit" />

</Columns>

<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

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

<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

<RowStyle BackColor="#EEEEEE" ForeColor="Black" />

<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />

<SortedAscendingCellStyle BackColor="#F1F1F1" />

<SortedAscendingHeaderStyle BackColor="#0000A9" />

<SortedDescendingCellStyle BackColor="#CAC9C9" />

<SortedDescendingHeaderStyle BackColor="#000065" />

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"

ProviderName="<%$ ConnectionStrings:ProductInstanceString.ProviderName %>"

SelectCommand="SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Submission.Status FROM Submission INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId">

</asp:SqlDataSource>

网友答案:

Use dropdown on the page and on the codebehind use DataView object to filter rows of grid, see below syntax:

DataView dvData = new DataView(dtData);
dvData.RowFilter = "[ColumnName]=[Condition, this will be your dropdown value]";
gv.DataSource = dvData;
gv.DataBind();
分享给朋友:
您可能感兴趣的文章:
随机阅读: