one1day
one1day

Reputation: 139

How to hide column and access it programatically in Gridview?

I have a gridview which is bonded totally programatically . I want to hide some columns and also rich them to show them in the label.

Here is my gridview :

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

I want to hide EmpID and UnitID . and want to show them in a label on the front-end side

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

I am trying to use this code , but it is not working even gives me error

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

Any Help appreciate

Thanks

Upvotes: 0

Views: 14847

Answers (5)

hussains8
hussains8

Reputation: 731

Write below code within GridView OnRowDataBound method to hide a specific column

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

Upvotes: 0

Prateek Gupta
Prateek Gupta

Reputation: 908

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

Upvotes: 0

clamchoda
clamchoda

Reputation: 5001

Yes there is actually a easy way to do that.

You are probably receiving an error because when you autogenerate columns the count is always 0.

After you Databind, iterate through your columns and hide them by index. I too will assume you datasource is a DataTable for this example.

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

You'll need to attach a RowDataBound event to your grid view.

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

In here, you can access your hidden values.

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

As for enriching the datagrid with labels, Win has provided you an elegant solution :P

Upvotes: 0

Justjyde
Justjyde

Reputation: 322

I will assume that you are binding to a DataTable. You need to

  1. customize your GridView to display only the columns you wish to be visible.
  2. Set AutoGenerateColumn to false in the GridView Property window.
  3. Access the EmpID and UnitID information from the DataTable you are bound to not from the GridView.

Hope this helps

Upvotes: 0

Win
Win

Reputation: 62301

If you want to hide a column base on a logic, you want to use RowDataBound.

It is a bit easy to maintain in the future.

Here is the sample. You can hide or show whatever columns you like.

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

Upvotes: 1

Related Questions