Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-04-30
5
Medium Priority
?
645 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:skaleem1
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 24276029
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
0
 
LVL 1

Author Comment

by:skaleem1
ID: 24276091
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...
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24292684
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
0
 
LVL 1

Accepted Solution

by:
skaleem1 earned 0 total points
ID: 24295304
M3mph15,
 Thanks for your response however I found out a work around and it is working for me. I am addding the columns in the code behind and I have changed the way I am adding them. I was orginally adding the columns as:

this.GridView1.Columns.Add(new BoundField() { DataField = "Description", HeaderText = "Description" });

Now I am adding those columns as template field:

TemplateField txtTemplateFieldDesc = new TemplateField() { HeaderText = "Description" };
 txtTemplateFieldDesc.ItemTemplate = new GridviewTextItemTemplate() { txtIdColumnName = "Description", txtIdSrcTableName = "Released" };
this.GridView1.Columns.Add(txtTemplateFieldDesc);
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24311380
No worries, Good luck
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question