在Gridview中隐藏自动生成的列

8

我有一个使用自动生成列的网格视图,因为用户可以选择在查询中返回哪些列。我想要隐藏标识列。我该如何隐藏自动生成列?即使在数据绑定事件中,列计数也为零。

我想隐藏自动生成列,可以通过在GridView的AutoGeneratingColumn事件中检查每个列是否是标识列(identity),如果是,则将其IsVisible属性设置为false。

6个回答

14

我发现了如何做到这一点。您需要使用rowdatabound事件,并在绑定行时隐藏单元格。

Protected Sub ResultGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ResultGrid.RowDataBound
        e.Row.Cells(1).Visible = False
End Sub

4
唯一需要注意的是,你需要根据适当的行类型进行更改。例如,如果 (e.Row.RowType != DataControlRowType.Pager) { e.Row.Cells[1].Visible = false; } - Martin Clarke

1
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[1].Visible = false;
}

1
我已经通过以下方式解决了这个问题。我编写了辅助函数来给我正确的列索引,然后隐藏所需的列。一旦辅助函数就位,您只需从gridview_databound函数中调用这个一行代码即可。
protected void grd_DataBound(object sender, EventArgs e)
{
    try
    {
        HideAutoGeneratedGridViewColumn(grd, "nContractID");           
    }
    catch (Exception ex)
    {

    }
}

    public int getColumnIndex(GridView grd, string sColumnName)
{
    return getColumnIndex(grd, sColumnName, false);
}
/// <summary>
/// Returns the columns index of the specified column based on the header text.
/// </summary>
/// <param name="grd"></param>
/// <param name="sColumnName"></param>
/// <returns></returns>
public int getColumnIndex(GridView grd, string sColumnName, bool bAutoGeneratedColumn)
{
    int ReturnVal = -1;
    try
    {
        if (grd != null)
        {
            if (!bAutoGeneratedColumn)
            {
                #region Static Columns
                if (grd.Columns.Count > 0)
                {
                    for (int x = 0; x < grd.Columns.Count; x++)
                    {
                        if (grd.Columns[x] != null)
                        {
                            if (grd.Columns[x].HeaderText.ToLower() == sColumnName.ToLower())
                            {
                                ReturnVal = x;
                                break;
                            }
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region AutoGenerated Columns
                if (grd.HeaderRow != null)
                {
                    for (int x = 0; x < grd.HeaderRow.Cells.Count; x++)
                    {
                        if (grd.HeaderRow.Cells[x] != null)
                        {
                            if (grd.HeaderRow.Cells[x].Text.ToLower() == sColumnName.ToLower())
                            {
                                ReturnVal = x;
                                break;
                            }
                        }
                    }
                }
                #endregion
            }
        }
    }
    catch (Exception ex)
    {
        ReturnVal = - 1;
        LogMessage("getColumnIndex(GridView grd, string sColumnName, bool bAutoGeneratedColumn) Error", ex.Message);
    }
    return ReturnVal;
}   
/// <summary>
/// Returns the columns index of the specified column based on the header text.
/// </summary>
/// <param name="sColumnName"></param>
/// <param name="r"></param>
/// <returns></returns>
public int getColumnIndex(string sColumnName, GridViewRow r)
{
    int ReturnVal = -1;
    try
    {
        if (r != null)
        {
            if (r.Cells.Count > 0)
            {
                for (int x = 0; x < r.Cells.Count; x++)
                {
                    if (r.Cells[x] != null)
                    {
                        if (((System.Web.UI.WebControls.DataControlFieldCell)(r.Cells[x])).ContainingField.HeaderText == sColumnName)
                        {
                            ReturnVal = x;
                            break;
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        ReturnVal = -1;
    }
    return ReturnVal;
}
public void HideAutoGeneratedGridViewColumn(GridView grd, string sColumnName)
{
    HideAutoGeneratedGridViewColumn(grd, getColumnIndex(grd, sColumnName, true));
}
public void HideAutoGeneratedGridViewColumn(GridView grd, int nColumnIndex)
{
    try
    {
        grd.HeaderRow.Cells[nColumnIndex].Visible = false;
        for (int x = 0; x < grd.Rows.Count; x++)
        {
            grd.Rows[x].Cells[nColumnIndex].Visible = false;
        }
    }
    catch (Exception ex)
    {
        LogMessage("HideAutoGeneratedGridViewColumn(GridView grd, int nColumnIndex) Error", ex.Message);
    }
}

0

你需要它吗?最简单的方法是不在选择查询中包含它。

如果你需要它并且知道列的位置:

gridView.Columns[KnownColumnIndex].Visible = false;

我需要将索引作为数据键包含在选择行中。 - SchwartzE
这对我没有用,gridView.Columns.Count对于自动生成的列为零。 - Somebody

0

这将隐藏自动生成的列标题和单元格,而不会像数据绑定那样看起来混乱。这是从这里获取的正确答案。

Protected Sub Gdvisitor_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gdvisitor.RowCreated
    If (e.Row.Cells.Count > 1) Then
        e.Row.Cells(1).Visible = False
    End If
End Sub


0

我会检查列是否大于零,如果是,则使用列名称和整数引用列集合的事实将标识列设置为隐藏。


2
自动生成的列未包含在列集合中。 - SchwartzE
在DataBound事件中执行,此时我相当确定自动生成的列将会在列集合中。 - Lazarus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接