2012-07-14 53 views
0

我有使用List of Object以编程方式填充的GridView。我无法访问GridView中的列。我喜欢把列programically的宽度是多少?下面是代码我如何填写在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列。 有人可以帮我吗?

回答

0

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

+0

它不工作此代码 – vikifor 2012-07-21 23:01:09

相关问题