基于CheckBoxList条件显示GridView列:双GridView独立控制需求
Solution for Independent Column Visibility Control of Two GridViews
Let's fix this step by step. The core issue here is that your two GridViews have different column structures, so you need separate visibility rules for each instead of reusing the same index logic across both. Let's break down the requirements and adjust the code accordingly:
Clarified Requirements
gridviewHeader: Only column index 0 should always be visible; columns 1 and beyond are controlled by the CheckBoxList selections.gridviewData: Columns index 0 (template checkbox) and 1 (Header) should always be visible; columns 2 and beyond are controlled by the CheckBoxList selections.
Adjusted Code Implementation
First, let's update the click handler to apply separate rules for each GridView. We'll also add safeguards to avoid invalid index errors:
protected void ShowClickedOnly_Click(object sender, EventArgs e) { gridviewData.Visible = true; gridViewHeader.Visible = true; foreach (System.Web.UI.WebControls.ListItem item in cbxSelected.Items) { string columnName = item.Text; // Handle header GridView: keep column 0 always visible int headerColIndex = GetColumnIndex(gridViewHeader, columnName); if (headerColIndex != -1 && headerColIndex > 0) { gridViewHeader.Columns[headerColIndex].Visible = item.Selected; } // Handle data GridView: keep columns 0 and 1 always visible int dataColIndex = GetColumnIndex(gridviewData, columnName); if (dataColIndex != -1 && dataColIndex > 1) { gridviewData.Columns[dataColIndex].Visible = item.Selected; } } } // Keep your column index helper method, with improved case-insensitive matching private int GetColumnIndex(GridView grid, string colName) { foreach (DataControlField col in grid.Columns) { if (col.HeaderText.Equals(colName, StringComparison.OrdinalIgnoreCase)) { return grid.Columns.IndexOf(col); } } return -1; }
Key Fixes & Explanations
- Independent Index Checks: We now fetch the column index for each GridView separately, then apply their unique visibility rules:
- For the header grid, we only modify columns where
headerColIndex > 0(ensuring column 0 stays visible at all times) - For the data grid, we only modify columns where
dataColIndex > 1(protecting the first two columns from visibility changes)
- For the header grid, we only modify columns where
- Valid Index Guard: We check if the returned index is not
-1to avoid runtime errors if a CheckBoxList item doesn't match any column header text. - Removed Redundant Logic: The nested
ifblocks from your previous attempt are gone—this eliminates conflicting visibility settings that caused the empty column issue.
Optional: Initialize Default Visibility
Add this to your Page_Load method to ensure columns start in a consistent state (all visible) on first load:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Set all header grid columns to visible by default foreach (DataControlField col in gridViewHeader.Columns) { col.Visible = true; } // Set all data grid columns to visible by default foreach (DataControlField col in gridviewData.Columns) { col.Visible = true; } // Optional: Pre-select all CheckBoxList items to match default visibility foreach (ListItem item in cbxSelected.Items) { item.Selected = true; } } }
内容的提问来源于stack exchange,提问作者Smith Stanley
相关产品推荐
相关产品推荐

