Solved

Data Grid Column Width

Posted on 2004-10-11
4
716 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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