0

我有使用对象列表以编程方式填充的 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 列。有人可以帮我吗?

4

1 回答 1

0

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

于 2012-07-14T22:31:38.127 回答