Solved

Data Grid Column Width

Posted on 2004-10-11
4
707 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
panareto,

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

-tushar
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

16 Experts available now in Live!

Get 1:1 Help Now