GridView column width

G

Guest

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
 
D

David Wier

What about surrounding your statement with an if/then - so that if the
column count is larger than 0, set the width
 
G

Guest

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 said:
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 said:
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
 
D

David Wier

What event are you using for the code?

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


Mike Parris said:
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 said:
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 said:
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
 
V

vMike

Mike Parris said:
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
 
G

Guest

I tried Databound and Rowdatabound.

Mike Parris

David Wier said:
What event are you using for the code?

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


Mike Parris said:
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 said:
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



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
 
G

Guest

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 said:
Mike Parris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top