473,770 Members | 3,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal sender As Object, _
ByVal e As System.EventArg s)
Try
Dim colWidth As Integer
colWidth = CInt(Server.Htm lEncode(TextBox 1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Colum ns.Count - 1
GridView1.Colum ns(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 10285
If your GridView is creating the columns using the AutoGenerateCol umns=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(B yVal sender As Object, _
ByVal e As System.EventArg s)
Try
Dim colWidth As Integer
colWidth = CInt(Server.Htm lEncode(TextBox 1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Colum ns.Count - 1
GridView1.Colum ns(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******@newsg roups.nospam> wrote in message
news:EE******** *************** ***********@mic rosoft.com...
If your GridView is creating the columns using the
AutoGenerateCol umns=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(B yVal sender As Object, _
ByVal e As System.EventArg s)
Try
Dim colWidth As Integer
colWidth = CInt(Server.Htm lEncode(TextBox 1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Colum ns.Count - 1
GridView1.Colum ns(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 autogeneratecol umns 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******@newsg roups.nospam> wrote in message
news:EE******** *************** ***********@mic rosoft.com...
If your GridView is creating the columns using the
AutoGenerateCol umns=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(B yVal sender As Object, _
ByVal e As System.EventArg s)
Try
Dim colWidth As Integer
colWidth = CInt(Server.Htm lEncode(TextBox 1.Text))
If colWidth > 0 Then
For i As Integer = 0 To GridView1.Colum ns.Count - 1
GridView1.Colum ns(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.Column s collection), e.g.

Sub GridView1_RowCr eated(ByVal sender As Object, ByVal e As
GridViewRowEven tArgs)

If e.Row.RowType = DataControlRowT ype.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 autogeneratecol umns 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******@newsg roups.nospam> wrote in message
news:EE******** *************** ***********@mic rosoft.com...
If your GridView is creating the columns using the
AutoGenerateCol umns=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(B yVal sender As Object, _
> ByVal e As System.EventArg s)
> Try
> Dim colWidth As Integer
> colWidth = CInt(Server.Htm lEncode(TextBox 1.Text))
> If colWidth > 0 Then
> For i As Integer = 0 To GridView1.Colum ns.Count - 1
> GridView1.Colum ns(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 grdViewEnteredD ata.Columns.Cou nt - 1
grdViewEnteredD ata.Columns(r). ItemStyle.Width = 100
grdViewEnteredD ata.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.Column s collection), e.g.

Sub GridView1_RowCr eated(ByVal sender As Object, ByVal e As
GridViewRowEven tArgs)

If e.Row.RowType = DataControlRowT ype.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 autogeneratecol umns 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******@newsg roups.nospam> wrote in message
news:EE******** *************** ***********@mic rosoft.com...
> If your GridView is creating the columns using the
> AutoGenerateCol umns=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(B yVal sender As Object, _
>> ByVal e As System.EventArg s)
>> Try
>> Dim colWidth As Integer
>> colWidth = CInt(Server.Htm lEncode(TextBox 1.Text))
>> If colWidth > 0 Then
>> For i As Integer = 0 To GridView1.Colum ns.Count - 1
>> GridView1.Colum ns(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
10715
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 displays correctly in standards compiance mode in firefox, but not msie, where the padding is added on to the column widths. the table size is always correct, but the column widths are not what I expect them to be. If I change the column widths to...
3
3821
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 results. temStyle-BorderWidth="200" with my column turning black. ControlStyle-Width="200" with no apparent results. Any help would be appreciated.
4
4194
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 column widths to narrow them. I want to record these widths so I can reapply them when I bind the datagrid to the table with 15 columns. I have searched around for articles on this and saw some that talked about...
4
3745
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... http://msdn2.microsoft.com/en-US/library/system.web.ui.webcontrols.gridview.pagesize.aspx Public Overridable Property PageSize As Integer Dim instance As GridView Dim value As Integer
3
17531
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 m_dsTable = New Data.DataTable m_dsTable.Columns.Add("Name", System.Type.GetType("System.String"), "") m_dsTable.Columns.Add("URL", System.Type.GetType("System.String"), "") 'Add two new command fields to select and delete
0
1095
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 set to the correct values. However although the columns obey the Visibility property correctly they ignore the width property. How can I fix this? Guy
0
1334
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 the debugger I can see the widths and visibility set to the correct values. However although the columns obey the Visibility property correctly they ignore the width property. i have done this by changing the widths on each control and also by...
5
54721
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 the width and nothing works. 1. <asp:BoundField HeaderText="fieldName" ItemStyle-Width="300px" DataField="fieldName" /> 2. <asp:BoundField HeaderText="fieldName" HeaderStyle-Width="300px" DataField="fieldName" /> 3. This has been tried in both...
3
6756
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 do is scan the width of each column for the first couple hundred or so rows and find the largest. (But how does one convert character counts to field widths in pixels?) More generally, however, is there a better approach to accomplish this?
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.