问题
I am working with a gridview that pulls data from a SQL database based on selections in dropdown lists. The source table has six columns for attributes of the selection, but depending on what is chosen, there could be anywhere from one to six of those that are empty (all null values). When the column is empty, I would like to have it hidden so the page is less clunky and confusing.
I've searched around for an answer for the past couple days, but what I have found so far is either related to hiding columns that you know are empty which I will not know or removing them in the SQL code which I think doesn't work if the column is called for in the gridview code and doesn't exist in the query.
I'm very new to ASP.NET, so I'm sorry if some of my terminology is off! Let me know if you have any questions about what I'm asking.
Thanks in advance for your help!
回答1:
Instead of hiding empty columns, why don't you add the columns you want in code behind?
When you retrieve the data to be displayed, you know which columns are present. You can add them and databind them in code behind.
To get you started with this, here is some code from a helpful article on how to do this:
BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name";
GridView1.Columns.Add(nameColumn);
回答2:
You could try:
- Set up the gridview will all columns hidden (
Visible="false") - In the gridview's RowDataBound event, check each of the column values and if it has data, set the column to show (
Visible="true")
The RowDataBound method could look something like this:
void YourGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// if this is a SqlDataSource you can use DataRowView,
// otherwise use whatever type is used in the data source
DataRowView rowView = (DataRowView)e.Row.DataItem;
if (!String.IsNullOrEmpty(rowView["ColumnA"]))
YourGridview.Columns[0].Visible = true;
// repeat for your other columns
}
}
回答3:
Best way to do that for web:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
namespace MyProject
{
public static class ExtensionGridView
{
public static GridView RemoveEmptyColumns(this GridView gv)
{
// Make sure there are at least header row
if (gv.HeaderRow != null)
{
int columnIndex = 0;
// For each column
foreach (DataControlFieldCell clm in gv.HeaderRow.Cells)
{
bool notAvailable = true;
// For each row
foreach (GridViewRow row in gv.Rows)
{
string columnData = row.Cells[columnIndex].Text;
if (!(string.IsNullOrEmpty(columnData) || columnData ==" "))
{
notAvailable = false;
break;
}
}
if (notAvailable)
{
// Hide the target header cell
gv.HeaderRow.Cells[columnIndex].Visible = false;
// Hide the target cell of each row
foreach (GridViewRow row in gv.Rows)
row.Cells[columnIndex].Visible = false;
}
columnIndex++;
}
}
return gv;
}
}
}
回答4:
Instead of this code:
if (!String.IsNullOrEmpty(rowView["ColumnA"]))
YourGridview.Columns[0].Visible = true;
Use this one:
if (!String.IsNullOrEmpty(Convert.Tostring(rowView["ColumnA"])) )
YourGridview.Columns[0].Visible = true;
回答5:
on the onItemBound event of the grid, you can check the value of the variable to be set in the column cell and set its visibility accordingly
来源:https://stackoverflow.com/questions/5195964/how-can-i-hide-empty-columns-in-a-gridview-without-knowing-which-will-be-empty