Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting columns width of DataGrid 1

Status
Not open for further replies.

nastoski

Technical User
Oct 21, 2003
21
Dear All,
How can I change the width of columns in DataGrid. I'm using following code for DataGrid:


DataSet ds;
ds = managerService.ReturnDataSet("select * from
transactions");

tranDataGrid.DataSource = ds;
tranDataGrid.DataMember = "transactions";

transactionDataGrid.DataBind();

everything works fine (Datagrid is shown on the page) but when I add code to set the column width

transactionDataGrid.Columns[0].ItemStyle.Width = Unit.Pixel
(35);

error appear:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Why is the collection of columns 0 and how can I change the width of the each column..

Thanks in advance!

Igor

 
I tested your code and I got the same error. What I did was to open the property builder for the grid and deselect the "Create columns automatically at run time" checkbox. Then I added each column individually. This worked. I am not exactly why it doesn't work the other way, but must be in the way the grid is renderd. Give it a try and let me know.

Jim
 
Thanks,
I will check this out and let you know..

Igor!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top