Connecting Tech Pros Worldwide Help | Site Map

Setting GridVIew column widths at runtime

Steve Bugden
Guest
 
Posts: n/a
#1: Mar 6 '06
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
Phillip Williams
Guest
 
Posts: n/a
#2: Mar 6 '06

re: Setting GridVIew column widths at runtime


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:
[color=blue]
> 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[/color]
Teemu Keiski
Guest
 
Posts: n/a
#3: Mar 6 '06

re: Setting GridVIew column widths at runtime


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" <WEBSWAPP@newsgroups.nospam> wrote in message
news:EE2F7087-A864-435B-B85B-0251CE5F7B7E@microsoft.com...[color=blue]
> 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:
>[color=green]
>> 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[/color][/color]


Steve Bugden
Guest
 
Posts: n/a
#4: Mar 6 '06

re: Setting GridVIew column widths at runtime


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:
[color=blue]
> 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" <WEBSWAPP@newsgroups.nospam> wrote in message
> news:EE2F7087-A864-435B-B85B-0251CE5F7B7E@microsoft.com...[color=green]
> > 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:
> >[color=darkred]
> >> 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[/color][/color]
>
>
>[/color]
Phillip Williams
Guest
 
Posts: n/a
#5: Mar 6 '06

re: Setting GridVIew column widths at runtime


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:
[color=blue]
> 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:
>[color=green]
> > 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" <WEBSWAPP@newsgroups.nospam> wrote in message
> > news:EE2F7087-A864-435B-B85B-0251CE5F7B7E@microsoft.com...[color=darkred]
> > > 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[/color]
> >
> >
> >[/color][/color]
Steve Bugden
Guest
 
Posts: n/a
#6: Mar 7 '06

re: Setting GridVIew column widths at runtime


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:
[color=blue]
> 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:
>[color=green]
> > 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:
> >[color=darkred]
> > > 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" <WEBSWAPP@newsgroups.nospam> wrote in message
> > > news:EE2F7087-A864-435B-B85B-0251CE5F7B7E@microsoft.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
> > >
> > >
> > >[/color][/color][/color]
Closed Thread