GridView column width

Discussion in 'ASP .Net' started by =?Utf-8?B?TWlrZSBQYXJyaXM=?=, Mar 20, 2007.

  1. 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
     
    =?Utf-8?B?TWlrZSBQYXJyaXM=?=, Mar 20, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?TWlrZSBQYXJyaXM=?=

    David Wier Guest

    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:...
    > 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
     
    David Wier, Mar 20, 2007
    #2
    1. Advertising

  3. 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:...
    > > 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

    >
    >
    >
     
    =?Utf-8?B?TWlrZSBQYXJyaXM=?=, Mar 21, 2007
    #3
  4. =?Utf-8?B?TWlrZSBQYXJyaXM=?=

    David Wier Guest

    What event are you using for the code?

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


    "Mike Parris" <> wrote in message
    news:...
    > 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:...
    > > > 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

    > >
    > >
    > >
     
    David Wier, Mar 21, 2007
    #4
  5. =?Utf-8?B?TWlrZSBQYXJyaXM=?=

    vMike Guest

    "Mike Parris" <> wrote in message
    news:...
    > 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
     
    vMike, Mar 21, 2007
    #5
  6. 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:...
    > > 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:...
    > > > > 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
    > > >
    > > >
    > > >

    >
    >
    >
     
    =?Utf-8?B?TWlrZSBQYXJyaXM=?=, Mar 22, 2007
    #6
  7. 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:...
    > > 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
    >
    >
    >
    >
    >
     
    =?Utf-8?B?TWlrZSBQYXJyaXM=?=, Mar 22, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndrewF
    Replies:
    1
    Views:
    807
    Bruce Barker
    Oct 10, 2005
  2. Keith G Hicks
    Replies:
    3
    Views:
    779
    Keith G Hicks
    Feb 18, 2008
  3. Replies:
    0
    Views:
    943
  4. Carl Howarth

    DG Column Width vs Table Column Width

    Carl Howarth, Oct 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    275
    Eliyahu Goldin
    Oct 24, 2004
  5. Replies:
    1
    Views:
    1,557
Loading...

Share This Page