How to change Column Width of GridView programically

I have GridView that is filled programically using List of Object. I can't access to Columns in GridView. I like to put Width of Columns programically?Here is the code how I fill the 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();

I use this event handler to change header

 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 = "Калории";

}

In this way the width isn't changed and I can't access to gridView columns. Could somebody help me?

Answers


You can change column widths right after GridView DataBind() method call. For example: gvTekovenKorisnik.Columns[0].Width = %yourvalue%;


Need Your Help

Code written using visual studio C# 2005 does not work in visual studio c# 2010

c# visual-studio-2010 visual-studio

I am currently working on the code which was written by people using visual studio C# 2005. After I run the code in visual studio C# 2010, this window pops up which says:

Starting google-chrome via Selenium on headless debian system

google-chrome selenium-rc headless xvfb robotframework

I'm trying to start the google-chrome browser in disabled web security mode. The selenium log says: