By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,492 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,492 IT Pros & Developers. It's quick & easy.

datagrid column widths - remember after postback

P: n/a
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.

Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Do it on the ItemDataBound Event and be sure to work the ViewState on your
control.


Fernando Hunth
Senior Developer
Huddle Group S.A.
fe******@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
www.huddle.com.ar


"wh1974" wrote:
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.

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.