Solved

Data Grid Column Width

Posted on 2004-10-11
4
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

726 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