Solved

Data Grid Column Width

Posted on 2004-10-11
4
713 Views
Last Modified: 2011-04-14
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?
0
Comment
Question by:panareto
  • 2
4 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 12278441
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
0
 
LVL 20

Expert Comment

by:ihenry
ID: 12284799
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");
0
 

Author Comment

by:panareto
ID: 12284961
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
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 500 total points
ID: 12286248
panareto,

you'll have to set the width in ItemDataBound event as this is AutoGenerated Columns.

-tushar
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now