21275

Filter the GridView Data based on Checkbox which is outside of GridView?

Question:

I have 5 check box on my page and a Grid view with Template Field, I am not using any bond field on page load I am binding the grid with all data of a table, I want to filter the data based on the check box check.

suppose: I have check box like A B C D. All check box are out side of Grid view. When user checked the check box A, then in Grid view, check box A related data should show, like wise for B C and D.

how do that?, Some one please give some example code and bit logic.

It would be great if I'll be able to filter the gridview without any postback.

Grid:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"> <Columns> <asp:TemplateField HeaderText="ID" SortExpression="ID"> <ItemTemplate> <asp:Label ID="lblId" runat="server" Text='<%#Eval("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Discription" SortExpression="Discription"> <ItemTemplate> <asp:Label ID="lblDiscription" runat="server" Text='<%#Eval("Discription") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address" SortExpression="Address"> <ItemTemplate> <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

Binding the Grid:

TestPageDao page1Dao = new TestPageDao (); if (!IsPostBack) { IList<TestDAO> TestDAO = page1Dao.GetAlldata(); GridView1.DataSource = TestDAO; GridView1.DataBind(); }

I tried <a href="https://stackoverflow.com/questions/2739778/filter-gridview" rel="nofollow">Filter gridview</a> or <a href="http://forums.asp.net/p/1034014/2904713.aspx" rel="nofollow">http://forums.asp.net/p/1034014/2904713.aspx</a>

Answer1:

If you want to avoid postback, uses jQuery like here: <a href="http://jquerybyexample.blogspot.com/2012/04/how-to-filter-gridview-records-using.html" rel="nofollow">http://jquerybyexample.blogspot.com/2012/04/how-to-filter-gridview-records-using.html</a>

An alternative is AJAX, but at the end of the story it is a postback-like approach.

Finally using postback you can do it in several ways by dynamically setting the GridView filter <strong>before</strong> the GridView loads. This can be achieved with the Page load event, the OnSelecting of the associated datasource (if any), or similar.

Here it is an extraction of the aspx:

<asp:CheckBox ID="CheckBox1" runat="server" Text="A" /> <asp:CheckBox ID="CheckBox2" runat="server" Text="B" /> <asp:CheckBox ID="CheckBox3" runat="server" Text="C" /> <asp:CheckBox ID="CheckBox4" runat="server" Text="D" /> <hr /> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="sqlDataSourceGridView" AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None" Width="650px" > <Columns> <asp:BoundField DataField="CompanyName" HeaderText="Company" ItemStyle-Width="200px" /> <asp:BoundField DataField="ContactName" HeaderText="Name" ItemStyle-Width="125px"/> <asp:BoundField DataField="City" HeaderText="city" ItemStyle-Width="125px" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="125px" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" ConnectionString="<%$ ConnectionStrings:northWindConnectionString %>" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [City], [Country] FROM [Customers]" OnSelecting="SqlDataSourceGridView_Selecting"> <FilterParameters> <asp:ControlParameter ControlID="checkbox1" Name="CompanyName" PropertyName="Checked" ConvertEmptyStringToNull="false" /> </FilterParameters> </asp:SqlDataSource>

Note the OnSelecting event and note that there isn't any filter preset.

Now in the code behind set dynamically the filter:

protected void SqlDataSourceGridView_Selecting(object sender, SqlDataSourceSelectingEventArgs e) { SqlDataSourceGridView.FilterExpression = string.Empty; if (CheckBox1.Checked) { SqlDataSourceGridView.FilterExpression += "(CompanyName=1)"; } if (CheckBox2.Checked) { if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR "; SqlDataSourceGridView.FilterExpression += "(B=1)"; } if (CheckBox3.Checked) { if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR "; SqlDataSourceGridView.FilterExpression += "(C=1)"; } if (CheckBox4.Checked) { if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR "; SqlDataSourceGridView.FilterExpression += "(D=1)"; } }

If you don't like the OnSelecting event, you can do the same in the PageLoad:

protected void Page_Load(object sender, EventArgs e) { // here same code of above // . . . }

I didn't test it so verify minor errors.

Recommend

  • The source of the report definition has not been specified
  • Magento Not Translating Wishlist Product Name and Description
  • Shorten code vb.net
  • FBLOG_TRACE() No logging to Logfile — FBLOG_INFO() logging OK — What is the DIFFERENCE
  • FBLOG_TRACE() No logging to Logfile — FBLOG_INFO() logging OK — What is the DIFFERENCE
  • not getting all images from gridview on item click listener
  • SQL query to exclude records that appear in other rows?
  • Add delivery info to query in SAP Crystal Reports
  • Kentico Repeater HTML Properties showing with selected transformation
  • How to change display text in item template based on eval in gridview?
  • CSRF Token Mismatch Laravel 4
  • Hibernate criteria query
  • Maven repository location is not updated in eclipse
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • Deserialize Dictionary
  • How can I have equal heights for inner elements of flexbox grid/boxes/cards without using jQuery?
  • Getting zero results in search using elastic4s
  • Excel 2007: Format of email address from Outlook 2007
  • Accessing local repository in offline mode
  • Organizing large javascript files [closed]
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Parenthesis() and SQL Query Performance
  • AWS-SES: Handling Bounces for Invalid ISPs
  • Dynamic ranges again - once more, with text strings
  • runtime error when linking ffmpeg libraries in qt creator
  • read values from form post in jquery or javascript
  • Delphi: Where is the shortcut that started the application? [duplicate]
  • can variables be set randomly when declaring them again?
  • Validate jQuery plugin, field not required
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Websockets service method fails during R startup
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • using HTMLImports.whenReady not working in chrome
  • How to stop GridView from loading again when I press back button?