Link to home
Start Free TrialLog in
Avatar of panareto
panareto

asked on

Data Grid Column Width

I have a Data Grid that is populated Dynamically via a DataTable. The AutoGenerateColumns attribute is set to true because the i retreive the data from a database table. The DataGrid Produces 4 columns and i want the 2nd one to have a different width than the others. I tried to set it via Datagrid.Columns(1).ItemStyle.Width = ...
but it doesnt work as i thing the column collection is not populated at all...

DataGrid.Columns.Count results 0....

Can anybody help please?
Avatar of tusharashah
tusharashah

Set the Width of Column in ItemDataBound event like following:
-----------------------------------------------------------------------------------------------------------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      switch ( e.Item.ItemType )
      {
            case ListItemType.Header:
                  e.Item.Cells[0].Width = 70;     // This will set 1st Column to width 70
            break;
      }
}
-----------------------------------------------------------------------------------------------------------------------

-tushar
Change column width after binding,

DataGrid1.DataSource = GetRecords();
DataGrid1.DataBind();

// change 2th column width to 60 pixel
DataGrid1.Columns[1].ItemStyle.Width = new Unit("60 px");
Avatar of panareto

ASKER

Pro_DataGrid2.ShowHeader="True"
              Pro_DataGrid2.DataSource = Pro_Table2
              Pro_DataGrid2.DataBind()
              Pro_DataGrid2.Columns(1).ItemStyle.Width = Unit.Pixel(300)


Pro_Table2.Columns.count //results 4
Pro_DataGrid2.Columns.count //results 0

and the Pro_DataGrid2.Columns(1).ItemStyle.Width = Unit.Pixel(300)

results error...System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial