Link to home
Start Free TrialLog in
Avatar of skaleem1
skaleem1Flag for Canada

asked on

How to change the column width of a gridview programatically through code behind

How to change the column width of a gridview programatically through code behind. I have a gridview control on a aspx page and am adding all columns including the update/delete buttons in the code behind. I have also added the code for changing the column width in the GridView1_RowDataBound event but it is not working as desired. The text columns are working but the other columns are contracted. Attached is the code below, please help what is not correct here?
protected int widestData=0;
 
private void AddGridViewColumns()
        {
            this.GridView1.Columns.Clear();
            this.GridView1.AutoGenerateColumns = false;
            switch (_pageMode)
            {
 
                case "ReleasedParts":
                    this.GridView1.AllowPaging = false;
                    this.GridView1.PageSize = 5;
                    this.GridView1.DataKeyNames = new string[] { "PartID" };
                    this.GridView1.Columns.Add(new BoundField() { DataField = "PartID", HeaderText = "PartID", Visible = false });
                    AddEditAndDeleteButtons();
                    this.GridView1.Columns.Add(new BoundField() { DataField = "Part Number", HeaderText = "Part Number"});
                    this.GridView1.Columns.Add(new BoundField() { DataField = "Hardware PN", HeaderText = "Hardware PN" });
                    this.GridView1.Columns.Add(new BoundField() { DataField = "Hardware PN2", HeaderText = "Hardware PN2" });
                    this.GridView1.Columns.Add(new BoundField() { DataField = "Hardware PN3", HeaderText = "Hardware PN3" });
                    this.GridView1.Columns.Add(new BoundField() { DataField = "Hardware PN4", HeaderText = "Hardware PN4" });
                    this.GridView1.Columns.Add(new BoundField() { DataField = "Docs", HeaderText = "Docs" });
                    this.GridView1.Columns.Add(new BoundField() { DataField = "ACC Description", HeaderText = "ACC Description" });
                    this.GridView1.Columns.Add(new BoundField() { DataField = "ECN ", HeaderText = "ECN" });
                    this.GridView1.Columns.Add(new BoundField() { DataField = "Release Date", HeaderText = "Release Date" });
                    DeleteBtnIndex = GridView1.Columns.Count - 1;
 
                    break;
                              
            }
        }
 
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            widestData = 0;
            System.Data.DataRowView drv;
            drv = (System.Data.DataRowView)e.Row.DataItem;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (drv != null)
                {
                    switch (_pageMode)
                    {
 
                        case "ReleasedParts":
                            String catName = drv[1].ToString();
 
                            int catNameLen = catName.Length;
                            if (catNameLen > widestData)
                            {
                                widestData = catNameLen;
                                GridView1.Columns[1].ItemStyle.Width = widestData * 1;
                                GridView1.Columns[1].ItemStyle.Wrap = false;
                                GridView1.Columns[2].ItemStyle.Width = widestData * 1;
                                GridView1.Columns[2].ItemStyle.Wrap = false;
                                GridView1.Columns[3].ItemStyle.Width = widestData * 8;
                                GridView1.Columns[3].ItemStyle.Wrap = false;
                                GridView1.Columns[4].ItemStyle.Width = widestData * 8;
                                GridView1.Columns[5].ItemStyle.Wrap = false;
                                GridView1.Columns[6].ItemStyle.Width = widestData * 8;
                                GridView1.Columns[7].ItemStyle.Wrap = false;
                            }
 
                            break;
                    }
 
 
 
                }
                
            }
 
private void AddEditAndDeleteButtons()
        {
 
            this.GridView1.Columns.Add(new CommandField() { HeaderText = "", ShowEditButton = true, EditText = "Edit", UpdateText = "Save", CancelText = "Cancel" });
            this.GridView1.Columns.Add(new CommandField() { HeaderText = "", ShowDeleteButton = true });
            DeleteBtnIndex = GridView1.Columns.Count - 1;
        }

Open in new window

Avatar of M3mph15
M3mph15
Flag of Australia image

HI,

Sorry im not a a computer where i can test this out but ther emight be something along the lines of GridView1.ColumnWidth or GridView1.Columns([index]).width
Avatar of skaleem1

ASKER

M3mph15,

thanks for your reply however I have put a breakpoint on those lines and it correctly sets the width to the desired number. There is someting preventing it from rendering the changed width, may be stylesheet, i need to check that...
Hi skaleem1,

Let us know if you find anything so i can try to help you out. Or if you like post your code and i'll have a read through to see if i can spot anything.

-M3mph15
ASKER CERTIFIED SOLUTION
Avatar of skaleem1
skaleem1
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No worries, Good luck