隐藏自动生成的gridview列

时间:2014-05-20 12:21:22

标签: c# asp.net gridview

我需要隐藏在网格视图中自动生成的列。 任何人都可以建议我怎么做..

**我的代码**

public page_load
{
private void BindGrid()
    {
        dt.Columns.Add("ID", typeof(Int16));// here it produce error as Field name ID already exists 
        dt.Columns.Add("FirstName", typeof(string));
        dt.Columns.Add("LastName", typeof(string));
        dt.Columns.Add("DateOfBirth", typeof(string));
        dt.Columns.Add("Occupation", typeof(string));
        dt.Columns.Add("Address", typeof(string));
        dt.Columns.Add("State", typeof(string));
        dt.Columns.Add("PostCode", typeof(string));
        dt.Columns.Add("Country", typeof(string));
        dt.Columns.Add("Email", typeof(string));
        dt.Columns.Add("Mobile", typeof(string));
        dt.Columns.Add("CustomerType", typeof(string));
        dt.Columns.Add("BusinessName", typeof(string));        
        CexCustomer.DataBind();        
    }
}

protected void btn_ShowCust_Click(object sender, EventArgs e)
{
    dt.Columns.Add(new DataColumn("ID", typeof(Int16)));
                      dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
                      dt.Columns.Add(new DataColumn("LastName", typeof(string)));
                      dt.Columns.Add(new DataColumn("DateOfBirth", typeof(string)));
                      dt.Columns.Add(new DataColumn("Occupation", typeof(string)));
                      dt.Columns.Add(new DataColumn("Address", typeof(string)));
                      dt.Columns.Add(new DataColumn("State", typeof(string)));
                      dt.Columns.Add(new DataColumn("PostCode", typeof(string)));
                      dt.Columns.Add(new DataColumn("Country", typeof(string)));
                      dt.Columns.Add(new DataColumn("Email", typeof(string)));
                      dt.Columns.Add(new DataColumn("Mobile", typeof(string)));
                      dt.Columns.Add(new DataColumn("CustomerType", typeof(string)));
                      dt.Columns.Add(new DataColumn("BusinessName", typeof(string)));                         


                      DataRow dr = dt.NewRow();
                      int countvalue = myojb.Count;

                      for (int i = 0; i < countvalue; i++)
                      {
                          dt.Rows.Add(
                                     myojb[i].id,
                                     myojb[i].firstName,
                                     myojb[i].lastName,
                                     myojb[i].dob.ToUniversalTime().ToString("dd/MM/yyy"),
                                     myojb[i].occupation,
                                     myojb[i].address,
                                     myojb[i].state,
                                     myojb[i].postCode,
                                     myojb[i].country.ToUpper(),
                                     myojb[i].email,
                                     myojb[i].mobileNumber,
                                     myojb[i].customertype,
                                     myojb[i].businessName
                                     );

                      }
                      CexCustomer.Visible = true;                         
                      CexCustomer.DataSource = dt;
                      CexCustomer.Columns[12].Visible = false;//hide business name..
                      CexCustomer.DataBind();

}

虽然运行时我需要隐藏Businessdetails可以任何人建议如何解决这个问题..

2 个答案:

答案 0 :(得分:0)

在您的行数据绑定事件中......

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

答案 1 :(得分:0)

谢谢你的回答.. 我只是将自动生成的列设置为false,然后按照我的编码过程...它运行良好..