我正在尝试使用以下代码遍历ASP.net Web应用程序中的gridview
列,但代码不会进入foreach
循环。
// Code snippet to hide columns from a gridview named 'gvEmployees'
gvEmployees.DataSource = dvItems.ToTable();
gvEmployees.DataBind();
string name = "First Name"; //Column name supposed to hide
int index=-1;
foreach (DataColumn col in gvEmployees.Columns)
{
if (col.ColumnName.ToLower().Trim() == name.ToLower().Trim())
{
// Getting the column index if find a match
index= gvEmployees.Columns.IndexOf(col);
// Using the above index, hiding the column from the grid view.
gvEmployees.Columns[index].Visible = false;
}
}
我正在尝试隐藏gridview
中的某些列。
答案 0 :(得分:1)
将这些代码移至其他事件,例如rowInitialize或PreRender或GridInitialize
string name = "First Name";// Column name supposed to hide
int index=-1;
foreach (DataColumn col in gvEmployees.Columns)
{
if (col.ColumnName.ToLower().Trim() == name.ToLower().Trim())
{
col.Visible = false;
}
}
答案 1 :(得分:1)
我想我可能会发现出了什么问题。您不应该使用DataColumn作为枚举变量的数据类型。试试这个:
string name = "First Name";// Column name supposed to hide
for (int i = 0; i < gvEmployees.Columns.Count; i++)
{
if (gvEmployees.Columns[i].HeaderText.ToLower().Trim() == name.ToLower().Trim())
{
gvEmployees.Columns[i].Visible = false;
}
}
答案 2 :(得分:1)
我认为您正在尝试根据列标题隐藏列,因此请尝试使用以下代码
// Code snippet to hide columns from a gridview named 'gvEmployees'
gvEmployees.DataSource = dvItems.ToTable();
gvEmployees.DataBind();
string name = "First Name";// Column name supposed to hide
foreach (var col in gvEmployees.Columns)
{
if (col.Text.ToLower().Trim() == name.ToLower().Trim())
{
// hiding the column from the grid view.
col.Visible = false;
}
}
您还可以编写以下代码:
string name = "First Name";// Column name supposed to hide
for (int i = 0; i < gvEmployees.Columns.Count; i++)
{
if (gvEmployees.Columns[i].Text.ToLower().Trim() == name.ToLower().Trim())
{
gvEmployees.Columns[i].Visible = false;
}
}
您需要使用rowdatabound事件并在绑定行时隐藏单元格(列)。
int index = 0;
bool hidden = false;
protected void gvEmployees_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if(hidden)
{
e.Row.Cells[index].Visible = false;
return;
}
string name = "First Name";// Column name supposed to hide
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.ToLower().Trim() == name.ToLower().Trim())
{
e.Row.Cells[i].Visible = false;
hidden = true;
index = i;
break;
}
}
}
使用rowdatabound事件并在绑定行时隐藏单元格(列)。
List<int> indexes = new List<int>();
bool hidden = false;
List<string> names = new List<string>();
protected void gvEmployees_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if(hidden)
{
foreach(int index in indexes)
{
e.Row.Cells[index].Visible = false;
}
}
// start - Column names supposed to hide
// Building the list of column names to be hidden.
names.Add("First Name");
names.Add("Last Name");
names.Add("Address");
names.Add("ID");
// end - Column names supposed to hide
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (names.Contains(e.Row.Cells[i].Text.ToLower().Trim())
{
e.Row.Cells[i].Visible = false;
hidden = true;
indexes.Add(i);
}
}
}
答案 3 :(得分:1)
您的问题是列是自动生成的(gvEmployees.AutoGenerateColumns == true)。 Columns集合中自动生成的列不。
如果您希望对列进行这种控制,我认为您需要在设计时生成它们。你可以在表单上使用任何类型的DataSource对象,然后将它连接到你的GridView吗?这将在您的设计器文件中生成列以匹配您的数据,而不是依赖AutoGenerateColumns在DataBind()上创建列。然后列将位于Columns集合中。