datagrid column widths - remember after postback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wh1974

    datagrid column widths - remember after postback

    I'm not sure if i'm heading in the right direction so would appreciate
    any comments on what I'm trying to do.

    Basically I have a DataGrid on a page which has columns whose width's I
    set in the DataGrid's ItemCreated event and which need to be remembered
    after a postback.

    I bind to the DataGrid in the Page_Load event when PostBack is false.

    The DataGrid displays the data horizontally so I set the RepeatColumns
    property as so:

    MyDataGrid.Repe atColumns = m_MyDataTable.R ows.Count;
    MyDataGrid.Data Source = m_MyDataTable;
    MyDataGrid.Data Bind();

    The DataTable that I am binding to the grid contains a field that I
    need to use to set the width of each column in the DataGrid:

    private void ColumnList_Item Created(...)
    {
    // ...
    DataRowView rv = (DataRowView) e.Item.DataItem ;
    int colWidth = Convert.ToInt32 (rv["ColWidth"].ToString());
    e.Item.Width = Unit.Pixel(colW idth);
    }

    This works when the page is first loaded.

    However, upon postback i'm not binding to the DataGrid again so the
    code in the ItemCreated event doesn't work as there is no DataItem
    attached.

    The reason I am unable to bind to the DataGrid upon postback is because
    the grid contains further usercontrols whose viewstate needs to be
    remembered. By binding to the grid every time the page is loaded, the
    viewstate of individual usercontrols appears to be lost.
    Any help would be appreciated,
    Thanks,
    Wayne.

  • Fernando Hunth

    #2
    RE: datagrid column widths - remember after postback

    Do it on the ItemDataBound Event and be sure to work the ViewState on your
    control.




    Fernando Hunth
    Senior Developer
    Huddle Group S.A.
    fernando@huddle .com.ar



    Huddle Group S.A. | Enterprise Technology Services
    Microsoft Certified Partner

    Ciudad de la Paz 2719

    · Piso 6D (C1428CPU)

    · Ciudad de Buenos Aires · Argentina









    "wh1974" wrote:
    [color=blue]
    > I'm not sure if i'm heading in the right direction so would appreciate
    > any comments on what I'm trying to do.
    >
    > Basically I have a DataGrid on a page which has columns whose width's I
    > set in the DataGrid's ItemCreated event and which need to be remembered
    > after a postback.
    >
    > I bind to the DataGrid in the Page_Load event when PostBack is false.
    >
    > The DataGrid displays the data horizontally so I set the RepeatColumns
    > property as so:
    >
    > MyDataGrid.Repe atColumns = m_MyDataTable.R ows.Count;
    > MyDataGrid.Data Source = m_MyDataTable;
    > MyDataGrid.Data Bind();
    >
    > The DataTable that I am binding to the grid contains a field that I
    > need to use to set the width of each column in the DataGrid:
    >
    > private void ColumnList_Item Created(...)
    > {
    > // ...
    > DataRowView rv = (DataRowView) e.Item.DataItem ;
    > int colWidth = Convert.ToInt32 (rv["ColWidth"].ToString());
    > e.Item.Width = Unit.Pixel(colW idth);
    > }
    >
    > This works when the page is first loaded.
    >
    > However, upon postback i'm not binding to the DataGrid again so the
    > code in the ItemCreated event doesn't work as there is no DataItem
    > attached.
    >
    > The reason I am unable to bind to the DataGrid upon postback is because
    > the grid contains further usercontrols whose viewstate needs to be
    > remembered. By binding to the grid every time the page is loaded, the
    > viewstate of individual usercontrols appears to be lost.
    > Any help would be appreciated,
    > Thanks,
    > Wayne.
    >
    >[/color]

    Comment

    Working...