在Gridview中隐藏自动生成的列

时间:2009-09-15 15:05:57

标签: asp.net gridview

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

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

答案 2 :(得分: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);
    }
}

答案 3 :(得分:0)

我检查列是否大于零,如果是,那么我会使用列集合可以引用列集合以及整数来将标识列设置为隐藏。

答案 4 :(得分:0)

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

如果您需要并知道列位置:

gridView.Columns[KnownColumnIndex].Visible = false;

答案 5 :(得分:0)

这将隐藏自动生成的列标题和单元格,而不会像数据绑定那样看起来乱糟糟。这是从here

中得出的正确答案

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