Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > GridView column width

Reply
Thread Tools

GridView column width

 
 
=?Utf-8?B?TWlrZSBQYXJyaXM=?=
Guest
Posts: n/a
 
      03-20-2007
I am trying to set the width of one of the columns of an asp.net 2.0 Gridview
control.

I am using a line -
MyGridview.Columns[2].ItemStyle.Width = Unit.Percentage(40);

The problem is that there are no columns in the gridview so this line "blows
up".

I am binding the gridview to data in code in the Page_load event. The
problem seems to be that I am trying to set the width before the creation of
the control is complete. All the examples I have seen, bind the control
directly to a database table/query and I presume that the control is then
populated earlier.

I have triedall the events in the page and the control, but without success.

Any ideas on what I am doing wrong, or how to achieve this?

Mike parris
 
Reply With Quote
 
 
 
 
David Wier
Guest
Posts: n/a
 
      03-20-2007
What about surrounding your statement with an if/then - so that if the
column count is larger than 0, set the width

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com



"Mike Parris" <> wrote in message
news:A52309C4-530C-42B4-86C2-...
> I am trying to set the width of one of the columns of an asp.net 2.0

Gridview
> control.
>
> I am using a line -
> MyGridview.Columns[2].ItemStyle.Width = Unit.Percentage(40);
>
> The problem is that there are no columns in the gridview so this line

"blows
> up".
>
> I am binding the gridview to data in code in the Page_load event. The
> problem seems to be that I am trying to set the width before the creation

of
> the control is complete. All the examples I have seen, bind the control
> directly to a database table/query and I presume that the control is then
> populated earlier.
>
> I have triedall the events in the page and the control, but without

success.
>
> Any ideas on what I am doing wrong, or how to achieve this?
>
> Mike parris



 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlrZSBQYXJyaXM=?=
Guest
Posts: n/a
 
      03-21-2007
Yes,

I could do this, it would stop the blowup, but I would never be able to set
the width of column 2.

When it renders there are 3 columns, but there does not seem to be an event
that I can use where the control has been filled and it contains the columns.

In ther mean time I have used a normal table to render the data. Code is no
more complex and it works!

Mike Parris

"David Wier" wrote:

> What about surrounding your statement with an if/then - so that if the
> column count is larger than 0, set the width
>
> --
> David Wier
> MVP/ASPInsider
> http://aspnet101.com
> http://aspexpress.com
>
>
>
> "Mike Parris" <> wrote in message
> news:A52309C4-530C-42B4-86C2-...
> > I am trying to set the width of one of the columns of an asp.net 2.0

> Gridview
> > control.
> >
> > I am using a line -
> > MyGridview.Columns[2].ItemStyle.Width = Unit.Percentage(40);
> >
> > The problem is that there are no columns in the gridview so this line

> "blows
> > up".
> >
> > I am binding the gridview to data in code in the Page_load event. The
> > problem seems to be that I am trying to set the width before the creation

> of
> > the control is complete. All the examples I have seen, bind the control
> > directly to a database table/query and I presume that the control is then
> > populated earlier.
> >
> > I have triedall the events in the page and the control, but without

> success.
> >
> > Any ideas on what I am doing wrong, or how to achieve this?
> >
> > Mike parris

>
>
>

 
Reply With Quote
 
David Wier
Guest
Posts: n/a
 
      03-21-2007
What event are you using for the code?

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com


"Mike Parris" <> wrote in message
news:7A91CADD-54DF-4DFC-BBE9-...
> Yes,
>
> I could do this, it would stop the blowup, but I would never be able to

set
> the width of column 2.
>
> When it renders there are 3 columns, but there does not seem to be an

event
> that I can use where the control has been filled and it contains the

columns.
>
> In ther mean time I have used a normal table to render the data. Code is

no
> more complex and it works!
>
> Mike Parris
>
> "David Wier" wrote:
>
> > What about surrounding your statement with an if/then - so that if the
> > column count is larger than 0, set the width
> >
> > --
> > David Wier
> > MVP/ASPInsider
> > http://aspnet101.com
> > http://aspexpress.com
> >
> >
> >
> > "Mike Parris" <> wrote in message
> > news:A52309C4-530C-42B4-86C2-...
> > > I am trying to set the width of one of the columns of an asp.net 2.0

> > Gridview
> > > control.
> > >
> > > I am using a line -
> > > MyGridview.Columns[2].ItemStyle.Width = Unit.Percentage(40);
> > >
> > > The problem is that there are no columns in the gridview so this line

> > "blows
> > > up".
> > >
> > > I am binding the gridview to data in code in the Page_load event. The
> > > problem seems to be that I am trying to set the width before the

creation
> > of
> > > the control is complete. All the examples I have seen, bind the

control
> > > directly to a database table/query and I presume that the control is

then
> > > populated earlier.
> > >
> > > I have triedall the events in the page and the control, but without

> > success.
> > >
> > > Any ideas on what I am doing wrong, or how to achieve this?
> > >
> > > Mike parris

> >
> >
> >



 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      03-21-2007

"Mike Parris" <> wrote in message
news:A52309C4-530C-42B4-86C2-...
> I am trying to set the width of one of the columns of an asp.net 2.0

Gridview
> control.
>
> I am using a line -
> MyGridview.Columns[2].ItemStyle.Width = Unit.Percentage(40);
>
> The problem is that there are no columns in the gridview so this line

"blows
> up".
>
> I am binding the gridview to data in code in the Page_load event. The
> problem seems to be that I am trying to set the width before the creation

of
> the control is complete. All the examples I have seen, bind the control
> directly to a database table/query and I presume that the control is then
> populated earlier.
>
> I have triedall the events in the page and the control, but without

success.
>
> Any ideas on what I am doing wrong, or how to achieve this?
>
> Mike parris

You would want to handle this the RowDataBound event (or may be the
DataBound event would work too) as follows

Here is an example. obviously the cell(2) will change. Or if you are
adjusting a text box you may have to use findcontrol but you should be able
to get the idea.

Sub YourGrid_RowDataBound(sender as object, e as GridViewRowEventArgs)
if e.row.rowtype= DataControlRowType.DataRow then
dim c as tablecell = e.row.cells(2)
c.width = unit.pixel(300)
dim strText as string = c.text
dim strNew as string =
ctype(strText,datetime).tostring("M/d/yyyy")
c.text = strNew
dim ctl as imagebutton =
ctype(e.row.findcontrol("Unlock1"),imagebutton)
if not ctl is nothing then
Dim intRow As int32 = e.Row.DataItemIndex
ctl.commandargument = intRow.tostring()
end if
end if
End Sub

Mike




 
Reply With Quote
 
=?Utf-8?B?TWlrZSBQYXJyaXM=?=
Guest
Posts: n/a
 
      03-22-2007
I tried Databound and Rowdatabound.

Mike Parris

"David Wier" wrote:

> What event are you using for the code?
>
> --
> David Wier
> MVP/ASPInsider
> http://aspnet101.com
> http://aspexpress.com
>
>
> "Mike Parris" <> wrote in message
> news:7A91CADD-54DF-4DFC-BBE9-...
> > Yes,
> >
> > I could do this, it would stop the blowup, but I would never be able to

> set
> > the width of column 2.
> >
> > When it renders there are 3 columns, but there does not seem to be an

> event
> > that I can use where the control has been filled and it contains the

> columns.
> >
> > In ther mean time I have used a normal table to render the data. Code is

> no
> > more complex and it works!
> >
> > Mike Parris
> >
> > "David Wier" wrote:
> >
> > > What about surrounding your statement with an if/then - so that if the
> > > column count is larger than 0, set the width
> > >
> > > --
> > > David Wier
> > > MVP/ASPInsider
> > > http://aspnet101.com
> > > http://aspexpress.com
> > >
> > >
> > >
> > > "Mike Parris" <> wrote in message
> > > news:A52309C4-530C-42B4-86C2-...
> > > > I am trying to set the width of one of the columns of an asp.net 2.0
> > > Gridview
> > > > control.
> > > >
> > > > I am using a line -
> > > > MyGridview.Columns[2].ItemStyle.Width = Unit.Percentage(40);
> > > >
> > > > The problem is that there are no columns in the gridview so this line
> > > "blows
> > > > up".
> > > >
> > > > I am binding the gridview to data in code in the Page_load event. The
> > > > problem seems to be that I am trying to set the width before the

> creation
> > > of
> > > > the control is complete. All the examples I have seen, bind the

> control
> > > > directly to a database table/query and I presume that the control is

> then
> > > > populated earlier.
> > > >
> > > > I have triedall the events in the page and the control, but without
> > > success.
> > > >
> > > > Any ideas on what I am doing wrong, or how to achieve this?
> > > >
> > > > Mike parris
> > >
> > >
> > >

>
>
>

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBQYXJyaXM=?=
Guest
Posts: n/a
 
      03-22-2007
Thanks vMike,

I tried this. The code ran OK, but I couldn't get it to actually alter the
column widths.

Think I'll stick with the table.

Mike Parris

"vMike" wrote:

>
> "Mike Parris" <> wrote in message
> news:A52309C4-530C-42B4-86C2-...
> > I am trying to set the width of one of the columns of an asp.net 2.0

> Gridview
> > control.
> >
> > I am using a line -
> > MyGridview.Columns[2].ItemStyle.Width = Unit.Percentage(40);
> >
> > The problem is that there are no columns in the gridview so this line

> "blows
> > up".
> >
> > I am binding the gridview to data in code in the Page_load event. The
> > problem seems to be that I am trying to set the width before the creation

> of
> > the control is complete. All the examples I have seen, bind the control
> > directly to a database table/query and I presume that the control is then
> > populated earlier.
> >
> > I have triedall the events in the page and the control, but without

> success.
> >
> > Any ideas on what I am doing wrong, or how to achieve this?
> >
> > Mike parris

> You would want to handle this the RowDataBound event (or may be the
> DataBound event would work too) as follows
>
> Here is an example. obviously the cell(2) will change. Or if you are
> adjusting a text box you may have to use findcontrol but you should be able
> to get the idea.
>
> Sub YourGrid_RowDataBound(sender as object, e as GridViewRowEventArgs)
> if e.row.rowtype= DataControlRowType.DataRow then
> dim c as tablecell = e.row.cells(2)
> c.width = unit.pixel(300)
> dim strText as string = c.text
> dim strNew as string =
> ctype(strText,datetime).tostring("M/d/yyyy")
> c.text = strNew
> dim ctl as imagebutton =
> ctype(e.row.findcontrol("Unlock1"),imagebutton)
> if not ctl is nothing then
> Dim intRow As int32 = e.Row.DataItemIndex
> ctl.commandargument = intRow.tostring()
> end if
> end if
> End Sub
>
> Mike
>
>
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resize Gridview width (not column width) based on size of databrought back. Hartman_Ralph@Hotmail.com ASP .Net Web Controls 1 06-13-2009 10:21 AM
Dynamically Resize Gridview Width Property (NOT Column Width) Hartman_Ralph@Hotmail.com ASP .Net 0 06-02-2009 03:59 PM
gridview column values to another page via gridview hyperlink column Keith G Hicks ASP .Net 3 02-18-2008 06:17 AM
Textbox width scaling to width of data not width of page? AndrewF ASP .Net 1 10-10-2005 04:38 PM
DG Column Width vs Table Column Width Carl Howarth ASP .Net Datagrid Control 1 10-24-2004 08:48 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57