datagrid column widths - remember after postback

W

wh1974

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.RepeatColumns = m_MyDataTable.Rows.Count;
MyDataGrid.DataSource = m_MyDataTable;
MyDataGrid.DataBind();

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_ItemCreated(...)
{
// ...
DataRowView rv = (DataRowView) e.Item.DataItem;
int colWidth = Convert.ToInt32(rv["ColWidth"].ToString());
e.Item.Width = Unit.Pixel(colWidth);
}

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.
 
G

Guest

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




Fernando Hunth
Senior Developer
Huddle Group S.A.
(e-mail address removed)



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

Ciudad de la Paz 2719

· Piso 6D (C1428CPU)

· Ciudad de Buenos Aires · Argentina
www.huddle.com.ar
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top