473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Setting GridVIew column widths at runtime

Hi,

I would like to set the column widths in the ASP.NET GridView control at
runtime

I have tried the following:
http://msdn2.microsoft.com/en-us/library/ms178296.aspx
Which uses the following code on a button:

Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Try
Dim colWidth As Integer
colWidth = CInt(Server.HtmlEncode(TextBox1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = colWidth
Next
End If
Catch
' Report error.
End Try
End Sub

But the count on the columns collection returns 0.

I have tried putting the code immediately before and after the databind but
that doesn't work either.

Could someone please tell me what I am doing wrong?

Regards

Steve
Mar 6 '06 #1
5 10266
If your GridView is creating the columns using the AutoGenerateColumns=true
then the Columns collection would not have them.

http://msdn2.microsoft.com/en-us/lib...w.columns.aspx
"Explicitly declared column fields can be used in combination with
automatically generated column fields. When both are used, explicitly
declared column fields are rendered first, followed by the automatically
generated column fields. Automatically generated column fields are not added
to the Columns collection."
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve Bugden" wrote:
Hi,

I would like to set the column widths in the ASP.NET GridView control at
runtime

I have tried the following:
http://msdn2.microsoft.com/en-us/library/ms178296.aspx
Which uses the following code on a button:

Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Try
Dim colWidth As Integer
colWidth = CInt(Server.HtmlEncode(TextBox1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = colWidth
Next
End If
Catch
' Report error.
End Try
End Sub

But the count on the columns collection returns 0.

I have tried putting the code immediately before and after the databind but
that doesn't work either.

Could someone please tell me what I am doing wrong?

Regards

Steve

Mar 6 '06 #2
They should be capable to set in autogenerating scenario also if one handles
RowCreated event and sets cell width explicitly in code.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Phillip Williams" <WE******@newsgroups.nospam> wrote in message
news:EE**********************************@microsof t.com...
If your GridView is creating the columns using the
AutoGenerateColumns=true
then the Columns collection would not have them.

http://msdn2.microsoft.com/en-us/lib...w.columns.aspx
"Explicitly declared column fields can be used in combination with
automatically generated column fields. When both are used, explicitly
declared column fields are rendered first, followed by the automatically
generated column fields. Automatically generated column fields are not
added
to the Columns collection."
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve Bugden" wrote:
Hi,

I would like to set the column widths in the ASP.NET GridView control at
runtime

I have tried the following:
http://msdn2.microsoft.com/en-us/library/ms178296.aspx
Which uses the following code on a button:

Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Try
Dim colWidth As Integer
colWidth = CInt(Server.HtmlEncode(TextBox1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = colWidth
Next
End If
Catch
' Report error.
End Try
End Sub

But the count on the columns collection returns 0.

I have tried putting the code immediately before and after the databind
but
that doesn't work either.

Could someone please tell me what I am doing wrong?

Regards

Steve

Mar 6 '06 #3
Hello Phillip and Teemu,

THankyou for your repllies.

I tried putting the code in the rowcreated event but that didn't work either.

I am binding to a datatable, whose structure is determined at runtime.
Therefore the autogeneratecolumns has to be set true.

Does anyone have any other ideas as to how I can set the widths?

Steve

"Teemu Keiski" wrote:
They should be capable to set in autogenerating scenario also if one handles
RowCreated event and sets cell width explicitly in code.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Phillip Williams" <WE******@newsgroups.nospam> wrote in message
news:EE**********************************@microsof t.com...
If your GridView is creating the columns using the
AutoGenerateColumns=true
then the Columns collection would not have them.

http://msdn2.microsoft.com/en-us/lib...w.columns.aspx
"Explicitly declared column fields can be used in combination with
automatically generated column fields. When both are used, explicitly
declared column fields are rendered first, followed by the automatically
generated column fields. Automatically generated column fields are not
added
to the Columns collection."
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve Bugden" wrote:
Hi,

I would like to set the column widths in the ASP.NET GridView control at
runtime

I have tried the following:
http://msdn2.microsoft.com/en-us/library/ms178296.aspx
Which uses the following code on a button:

Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Try
Dim colWidth As Integer
colWidth = CInt(Server.HtmlEncode(TextBox1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = colWidth
Next
End If
Catch
' Report error.
End Try
End Sub

But the count on the columns collection returns 0.

I have tried putting the code immediately before and after the databind
but
that doesn't work either.

Could someone please tell me what I am doing wrong?

Regards

Steve


Mar 6 '06 #4
During the RowCreated event you can loop through the Row.Cells collection (to
get the equivalent of the GridView.Columns collection), e.g.

Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
dim td as TableCell
For each td in e.Row.Cells
'set the td style to the desired width
Next For
End If
End Sub

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve Bugden" wrote:
Hello Phillip and Teemu,

THankyou for your repllies.

I tried putting the code in the rowcreated event but that didn't work either.

I am binding to a datatable, whose structure is determined at runtime.
Therefore the autogeneratecolumns has to be set true.

Does anyone have any other ideas as to how I can set the widths?

Steve

"Teemu Keiski" wrote:
They should be capable to set in autogenerating scenario also if one handles
RowCreated event and sets cell width explicitly in code.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Phillip Williams" <WE******@newsgroups.nospam> wrote in message
news:EE**********************************@microsof t.com...
If your GridView is creating the columns using the
AutoGenerateColumns=true
then the Columns collection would not have them.

http://msdn2.microsoft.com/en-us/lib...w.columns.aspx
"Explicitly declared column fields can be used in combination with
automatically generated column fields. When both are used, explicitly
declared column fields are rendered first, followed by the automatically
generated column fields. Automatically generated column fields are not
added
to the Columns collection."
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve Bugden" wrote:

> Hi,
>
> I would like to set the column widths in the ASP.NET GridView control at
> runtime
>
> I have tried the following:
> http://msdn2.microsoft.com/en-us/library/ms178296.aspx
> Which uses the following code on a button:
>
> Protected Sub Button1_Click(ByVal sender As Object, _
> ByVal e As System.EventArgs)
> Try
> Dim colWidth As Integer
> colWidth = CInt(Server.HtmlEncode(TextBox1.Text))
> If colWidth > 0 Then
> For i As Integer = 0 To GridView1.Columns.Count - 1
> GridView1.Columns(i).ItemStyle.Width = colWidth
> Next
> End If
> Catch
> ' Report error.
> End Try
> End Sub
>
> But the count on the columns collection returns 0.
>
> I have tried putting the code immediately before and after the databind
> but
> that doesn't work either.
>
> Could someone please tell me what I am doing wrong?
>
> Regards
>
> Steve


Mar 6 '06 #5
Thankyou Phillip. That works perfectly.

I was using the wrong collection ie
For r As Integer = 0 To grdViewEnteredData.Columns.Count - 1
grdViewEnteredData.Columns(r).ItemStyle.Width = 100
grdViewEnteredData.Columns(r).ItemStyle.Wrap = True
Next

"Phillip Williams" wrote:
During the RowCreated event you can loop through the Row.Cells collection (to
get the equivalent of the GridView.Columns collection), e.g.

Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
dim td as TableCell
For each td in e.Row.Cells
'set the td style to the desired width
Next For
End If
End Sub

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve Bugden" wrote:
Hello Phillip and Teemu,

THankyou for your repllies.

I tried putting the code in the rowcreated event but that didn't work either.

I am binding to a datatable, whose structure is determined at runtime.
Therefore the autogeneratecolumns has to be set true.

Does anyone have any other ideas as to how I can set the widths?

Steve

"Teemu Keiski" wrote:
They should be capable to set in autogenerating scenario also if one handles
RowCreated event and sets cell width explicitly in code.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Phillip Williams" <WE******@newsgroups.nospam> wrote in message
news:EE**********************************@microsof t.com...
> If your GridView is creating the columns using the
> AutoGenerateColumns=true
> then the Columns collection would not have them.
>
> http://msdn2.microsoft.com/en-us/lib...w.columns.aspx
> "Explicitly declared column fields can be used in combination with
> automatically generated column fields. When both are used, explicitly
> declared column fields are rendered first, followed by the automatically
> generated column fields. Automatically generated column fields are not
> added
> to the Columns collection."
>
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Steve Bugden" wrote:
>
>> Hi,
>>
>> I would like to set the column widths in the ASP.NET GridView control at
>> runtime
>>
>> I have tried the following:
>> http://msdn2.microsoft.com/en-us/library/ms178296.aspx
>> Which uses the following code on a button:
>>
>> Protected Sub Button1_Click(ByVal sender As Object, _
>> ByVal e As System.EventArgs)
>> Try
>> Dim colWidth As Integer
>> colWidth = CInt(Server.HtmlEncode(TextBox1.Text))
>> If colWidth > 0 Then
>> For i As Integer = 0 To GridView1.Columns.Count - 1
>> GridView1.Columns(i).ItemStyle.Width = colWidth
>> Next
>> End If
>> Catch
>> ' Report error.
>> End Try
>> End Sub
>>
>> But the count on the columns collection returns 0.
>>
>> I have tried putting the code immediately before and after the databind
>> but
>> that doesn't work either.
>>
>> Could someone please tell me what I am doing wrong?
>>
>> Regards
>>
>> Steve

Mar 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Sabljak | last post by:
I seem to having a little trouble getting a table to display correctly in both msie and firefox. I want to set the table and column widths in pixels, and have some cell padding too. The table...
3
by: sck10 | last post by:
Hello, I am using a datagrid and am trying to control the column widths for each column. Is this possible? I have tried using the following: ControlStyle-BorderWidth="1000" with no apparent...
4
by: Rich | last post by:
Hello, I have a one datagrid that will be based on different datatables. One datatable may have 7 columns, another 15... With the tables that have more columns, I have been manually dragging...
4
by: Adam Sandler | last post by:
Hello, Using VWD 2005 here and I want to set the GridView PageSize at runtime. This URL has an example of what I exactly want to do......
3
by: =?Utf-8?B?SmVu?= | last post by:
I would like to set the width on 2 columns of a asp.net gridview control, but I'm having trouble making this work. Here is the code I use to create the gridview: 'Create a new data table...
0
by: =?Utf-8?B?Z3V5?= | last post by:
I hav a GridView with autogenerated columns and I programatically set the width and visibility of each column. The code executes correctly as with the debugger I can see the widths and visibility...
0
by: =?Utf-8?B?Z3V5?= | last post by:
Repost as no reply from dotnet.Framework.ASPNet I have a GridView with autogenerated columns and I programatically set the width and visibility of each column. The code executes correctly as with...
5
by: mohaaron | last post by:
OK, I have now gone through lots of posts, and there are lots, looking for a anwer as to why I can't set the width of the columns in my gridview. So far I have tried the following methods to set...
3
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I am wondering what is considered a best practice for setting column widths to fit the data for a DataGridView (.Net 2.0) that was created on-the-fly from an arbitrary query. What I would probably...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.