无法遍历 gridview 列(自动生成)?

发布于 2024-12-11 21:22:16 字数 720 浏览 1 评论 0原文

我正在尝试使用以下代码遍历 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隐藏一些列。

I am trying to traverse the gridview columns in ASP.net web application, using the following code, However the code is not going inside the foreach loop.

// 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;
   }

}

I am trying to hide some columns from gridview.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

司马昭之心 2024-12-18 21:22:16

我认为您正在尝试根据列标题隐藏列,因此请尝试以下代码,

// 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);
        }
    }
}

I think u are trying to hide column based on column title so please try following code,

// 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;
  }
}

Update

you can also write following code:

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;
    }
}

Update when using auto generated columns (for single-column version),

You need to use the rowdatabound event and hide the cell (column) when the row is bound.

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;
        }
    }
}

Update when using auto generated columns, (for multi-column version)

Using the rowdatabound event and hide the cell (column) when the row is bound.

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);
        }
    }
}
下壹個目標 2024-12-18 21:22:16

我想我可能会发现出了什么问题。不应使用 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;
            }

        }

I think I might find out what was going wrong. You should not use DataColumn as the data type of the enumeration variable. Try this:

        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;
            }

        }
终止放荡 2024-12-18 21:22:16

将这些代码移至其他一些事件,例如 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;
 }

}

Move these code to some other event like rowInitialize or PreRender or 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;
 }

}
浮华 2024-12-18 21:22:16

您的问题是列是自动生成的(gvEmployees.AutoGenerateColumns == true)。
自动生成的列不在列集合中。

如果您希望对列进行这种控制,我认为您需要在设计时生成它们。您可以在表单上使用任何类型的 DataSource 对象,然后将其连接到 GridView 吗?这将在设计器文件中生成列以匹配您的数据,而不是依赖 AutoGenerateColumns 在 DataBind() 上创建列。然后这些列将位于 Columns 集合中。

Your issue is that the columns are auto-generated (gvEmployees.AutoGenerateColumns == true).
Auto-generated columns are not in the Columns collection.

If you want this kind of control over your columns, I think you're going to need to generate them at design time. Can you use a DataSource object of any kind on your form, then hook it up to your GridView? That will generate columns in your designer file to match your data, rather than relying on AutoGenerateColumns to create the columns on DataBind(). Then the columns will be in the Columns collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文