I have a GridView where the datasource is set from a Datatable filled from a Stored Procedure. There are two columns in the datatable that I am interested in filtering/showing & hiding based on their content.
The data is for lessons for different courses.
DataTable/Datasource
Identifier | Lesson Title | BaseIdentifier |
---|---|---|
CBT-0001 | How to Build a Cabinet | CBT-0001 |
CBT-0002 | Dangers of Cutting Lumber | CBT-0002 |
TBL-0001 | How to Build a Table | TBL-0001 |
TBL-0002 | Dangers of Cutting Lumber | CBT-0002 |
In the above table the Lesson TBL-0002 is just a alias/ pointer to CBT-0002.
Based on a checkbox I need to show everything in the Gridview and I need to just show the parent lessons the ones where the identifier = baseIdentifier. So after filtering / or hiding rows my gridview would look like this:
[ ] Show Alias Lessons
Identifier | Lesson Title | BaseIdentifier |
---|---|---|
CBT-0001 | How to Build a Cabinet | CBT-0001 |
CBT-0002 | Dangers of Cutting Lumber | CBT-0002 |
TBL-0001 | How to Build a Table | TBL-0001 |
[X] Show Alias Lessons
Identifier | Lesson Title | BaseIdentifier |
---|---|---|
CBT-0001 | How to Build a Cabinet | CBT-0001 |
CBT-0002 | Dangers of Cutting Lumber | CBT-0002 |
TBL-0001 | How to Build a Table | TBL-0001 |
TBL-0002 | Dangers of Cutting Lumber | CBT-0002 |
My goal is to only query once rather than having to requery every time the checkbox is Checked/unchecked.
The only time an Identifier will equal a BaseIdentifier is when it is the parent lesson (Parents are Aliases of themselves).
My Code:
PathBO pathBO = new PathBO(); LessonBO lessonBO = new LessonBO(); pathBO.GetPath(); DataTable dt = new DataTable(); dt = lessonBO.GetAllAliasLessons(); gvLessons.DataSource = dt; gvLessons.DataBind();
My Gridview:
<asp:GridView ID="gvLessons" runat="server" cssClass="Grid" GridLines="None" AutoGenerateColumns="False" AllowSorting="true"> <AlternatingRowStyle cssClass="GridAltRow" /> <Columns> <asp:BoundField DataField="Identifier" HeaderText="Identifier" SortExpression="Identifier" /> <asp:BoundField DataField="SectionTitle" HeaderText="SectionTitle" /> <asp:BoundField DataField="VersionNum" HeaderText="VersionNumber" /> <asp:BoundField DataField="AliasList" HeaderText="AliasList" /> <asp:BoundField DataField="BaseIdentifier" HeaderText="BaseIdentifier" Visible="False" /> </Columns> <EditRowStyle cssClass="GridEditRow" /> <FooterStyle cssClass="GridFooter" /> <HeaderStyle cssClass="GridHeader" /> <PagerStyle cssClass="GridPager" /> <RowStyle cssClass="GridRow" /> <SelectedRowStyle cssClass="GridSelectedRow" /> <SortedAscendingCellStyle cssClass="GridSortAscCell" /> <SortedAscendingHeaderStyle cssClass="GridSortAscHeader" /> <SortedDescendingCellStyle cssClass="GridSortDescCell" /> <SortedDescendingHeaderStyle cssClass="GridSortDescHeader" /> </asp:GridView>
Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
First up? Folks – this is how you ask a question on SO!
Don’t post 200 lines of code, but DO post the EXACT lines of code we need!
Ok, reading this close? Well, our filter actually becomes:
[x] Show Alias Lessons
If "Identifer" <> "BaseIdentifer" then Show all lessons Else "Identifier" = "BaseIdentifer"
So, your code becomes this:
DataView dtMyFilterView As new DataView dtMyFilterView = dt If ShowAliasLessons.Checked = True Then dtMyFilterView.RowFilter = "" Else dtMyFilterView.RowFilter = "Identifier <> BaseIdentifier" End If gvLessons.DataSource = dtMyFilterView gvLessons.DataBind();
Edit: depending on the filter – it not clear if you ONLY want non aliased.
But you can of course do this:
If ShowAliasLessons.Checked = True Then dtMyFilterView.RowFilter = ""Identifier <> BaseIdentifier"" Else dtMyFilterView.RowFilter = "Identifier = BaseIdentifier" End If
So depending on all lessons no matter what, or only non aliased vs aliased. (but, you get the idea and the choice is yours).
Now I munged the code between vb and c#. But RIGHT after you setup the “dt” (the data table), then we create a dataView based on that dt. The dataview is used, since it can be EASY sorted, and of course filtered. In fact, you could save/shove the “data view” into session and simply check the status of the [x] Show aliased lessons, and simple change the filter and re-bind back into the grid. (you thus would NOT need to re-call the stored procedure that fills the table, but ONLY flip the filter on the “data view”. This of course assumes that you saved the dv in viewstate or session. But really, zero issues if that if you re-call the stored procedure, get the data table and THEN set the “dv” filter on that table.
So just add a “new” middle step. use a “data view” between the data table and the gridview. data views are tailor made for this question. You can filter, or sort or do whatever and then data bind that “data view” in place of the table!
So shove the data view into the grid, and right before you do, just set the filter you need.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0