如何以编程方式更改GridView的列宽度

时间:2012-07-14 21:22:33

标签: asp.net gridview width

我有使用List of Object以编程方式填充的GridView。我无法访问GridView中的列。我喜欢以编程方式放置列的宽度?这是我填充GridView的代码

DataClassesDataContext dContext = new DataClassesDataContext();
            var userId = (Guid)(Membership.GetUser(Membership.GetUser().UserName, false).ProviderUserKey);
            var tekovenKorisnikQuery = from d in dContext.pregledIshranas
                              where d.UserId == userId
                              select new {d.datum, d.kilogrami, d.visina, d.BMI, d.kalorii};

            List<PregledIshranaFormatirano> listaFormatirana = new List<PregledIshranaFormatirano>();
            foreach (var d in tekovenKorisnikQuery)
            {
                listaFormatirana.Add(new PregledIshranaFormatirano(string.Format("{0:dd-MM-yyyy}", d.datum), d.kilogrami.ToString(), d.visina.ToString(), string.Format("{0:N2}", d.BMI), d.kalorii.ToString()));
            }

            gvTekovenKorisnik.DataSource = listaFormatirana;
            gvTekovenKorisnik.DataBind();

我使用此事件处理程序来更改标题

 protected void gvTekovenKorisnik_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Width = new Unit("200px"); 
            e.Row.Cells[0].Text = "Датум";
            e.Row.Cells[1].Width = new Unit("200px");
            e.Row.Cells[1].Text = "Килограми";
            e.Row.Cells[2].Width = new Unit("200px");
            e.Row.Cells[2].Text = "Висина";
            e.Row.Cells[3].Width = new Unit("200px");
            e.Row.Cells[3].Text = "BMI индекс";
            e.Row.Cells[4].Width = new Unit("200px");
            e.Row.Cells[4].Text = "Калории";

}

这样宽度不会改变,我无法访问gridView列。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以在GridView DataBind()方法调用后立即更改列宽。 例如:gvTekovenKorisnik.Columns [0] .Width =%yourvalue%;