如何隐藏列并在Gridview中以编程方式访问它?

时间:2013-05-21 22:27:08

标签: c# asp.net

我有一个gridogram,它完全以编程方式绑定。我想隐藏一些列,并且还要丰富它们以在标签中显示它们。

这是我的gridview:

<Gridview ID=Gridview1 runat="server" ></Gridview>

我想要隐藏EmpID和UnitID。并希望在前端标签上显示它们

EmpID  EmpName UnitID  UnitName    
--------------------------------
 1      jack     4      MyUnit

我正在尝试使用此代码,但它无法正常工作甚至错误

 if (GridForUnits.Columns.Count > 1)
        {
           GridForUnits.Columns[1].Visible = false;
           //GridForUnits.Columns[1].Visible = false;
        }

任何帮助表示

由于

5 个答案:

答案 0 :(得分:1)

如果要隐藏逻辑列的基础,则需要使用RowDataBound

将来维护起来有点容易。

以下是样本。您可以隐藏或显示您喜欢的任何列。

enter image description here

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" 
    AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField HeaderText="EmpID" DataField="EmpID" />
        <asp:TemplateField HeaderText="EmpName" >
            <ItemTemplate>
                <asp:Label runat="server" ID="EmpNameLabel" 
                    Text='<%# Eval("EmpName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="UnitID" DataField="UnitID" />
        <asp:TemplateField HeaderText="UnitName" >
            <ItemTemplate>
                <asp:Label runat="server" ID="UnitNameLabel" 
                    Text='<%# Eval("UnitName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


public class Employee
{
    public int EmpID { get; set; }  
    public string EmpName  { get; set; }  
    public int UnitID  { get; set; }
    public string UnitName { get; set; }  
}

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = new List<Employee>
    {
    new Employee { EmpID = 1, EmpName = "One", UnitID = 100, UnitName = "One hundred"},
    new Employee { EmpID = 2, EmpName = "Two", UnitID = 200, UnitName = "Two hundred"},
    new Employee { EmpID = 3, EmpName = "Three", UnitID = 300, UnitName = "Three hundred"},
    new Employee { EmpID = 4, EmpName = "Four", UnitID = 400, UnitName = "Four hundred"}
    };
    GridView1.DataBind();
}


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var employee = e.Row.DataItem as Employee;
        var empNameLabel = e.Row.FindControl("EmpNameLabel") as Label;
        var unitNameLabel = e.Row.FindControl("UnitNameLabel") as Label;

        if (employee.UnitID == 200)
        {
            empNameLabel.Visible = false;
            unitNameLabel.Visible = false;
        }
    }
}

答案 1 :(得分:0)

我将假设您绑定到DataTable。你需要

  1. 自定义GridView以仅显示您希望显示的列。
  2. 在GridView属性窗口中将AutoGenerateColumn设置为false。
  3. 从GridView中访问您绑定的DataTable中的EmpID和UnitID信息。
  4. 希望这有帮助

答案 2 :(得分:0)

是的,实际上有一种简单的方法可以做到这一点。

您可能收到错误,因为当您autogenerate列时,计数始终为0。

Databind之后,遍历列并按索引隐藏它们。我也会假设你的数据源是这个例子的DataTable

        DataTable dt = new DataTable();
        dt.Columns.Add("EmpID");
        dt.Columns.Add("EmpName");
        dt.Columns.Add("UnitID");
        dt.Columns.Add("UnitName");
        DataRow dr = dt.NewRow();
        dr["EmpID"] = 1;
        dr["EmpName"] = "Jack";
        dr["UnitID"] = 4;
        dr["UnitName"] = "MyUnit";
        dt.Rows.Add(dr);

        GridView1.DataSource = dt;
        GridView1.DataBind();

        //GridView1.Columns[0].Visible = false; // will error on autogenerate columns
        // So after your data is bound, loop though

        int columnIndexEmpID = 0;
        int columnIndexUnitID = 2;

        GridView1.HeaderRow.Cells[columnIndexEmpID].Visible = false;
        GridView1.HeaderRow.Cells[columnIndexUnitID].Visible = false;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            gvr.Cells[columnIndexEmpID].Visible = false;
            gvr.Cells[columnIndexUnitID].Visible = false;
        }

您需要将RowDataBound事件附加到网格视图。

<asp:GridView ID="GridView1" runat="server" 
    onrowdatabound="GridView1_RowDataBound">
</asp:GridView>

在这里,您可以访问隐藏的值。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRowView drv = (DataRowView)e.Row.DataItem;
    if (drv != null)
    {
        string cellValue = Convert.ToString(drv["EmpID"]);// By column name
    }
}

至于使用标签丰富数据网格,Win为您提供了一个优雅的解决方案:P

答案 3 :(得分:0)

GridView1.DataBind();
if (GridView1.Columns.Count > 0)
    GridView1.Columns[0].Visible = false;
else
{
    GridView1.HeaderRow.Cells[0].Visible = false;
    foreach (GridViewRow grow in GridView1.Rows)
    {
        grow.Cells[0].Visible = false;
    }
}

答案 4 :(得分:0)

在 GridView OnRowDataBound 方法中编写以下代码以隐藏特定列

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[colIndex].Visible = false;
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[colIndex].Visible = false;
    }
}