We help IT Professionals succeed at work.

gridview can't set column width of dynamically created gridview

Hi, I've seen tonnes about this on the internet so i've been through everything on google! But still I can't figure this out.

No matter what I do I can't seem to consistently set the column widths on my gridview. In the RowDataBound event I have added the following code

e.Row.Cells(6).Width = 200

And, that did seem to work... briefly. In one query I run, it works, but when I extract a different set of data into the same table it inexplicably stops working. There is no difference between the two queries, except that the second one is extracting a lot more data.

think i may need to bind the columns first before binding the data (sort of like in http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx) but I so far haven't been able to get that working...

Any help?

thanks, Aiden
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
Hi kovilpattiBalu, I have tried that before but didn't solve the issue.

I saw CodeCruisers solution before but didn't think it applied since I was dynamically creating the table. But in fact, I suppose the columns must still be automatically created so that might be the solution. Just need to get a few moments to look at that...

Author

Commented:
Hi actually I've spotted the problem. I was doing it already the same way that is specified in CodeCruisers link... but the problem is that in on of the columns which is not working (i.e. I can't set the width) the problem is actually that there is a very long line which has no spaces in it e.g. something like

shopijdsopaijd8*(&odhoiahfodi87()&()Yoihdoihaoidh07)*&)&*iodhaoidhoiau0870708

So, with a line like above, it expands the column and the data does not get wrapped (I tried adding grdMTReports.RowStyle.Wrap = True but it makes no difference)

So, it's not really feasable to check how long column data is and start inserting spaces. Is there some way to just get the single line to wrap properly?
Most Valuable Expert 2012
Top Expert 2014
Commented:

Author

Commented:
excellent, yes, this does it...

e.Row.Cells(1).Attributes.Add("style", "word-break:break-all;word-wrap:break-word")

Probably should have investigated that myself but thanks for that!