Gridview column width.

Hi,
I am trying to increase the size of column in my gridview.

I have tried to use

 Referral_Gridview.Columns[1].ItemStyle.Width = Unit.Pixel(100);

but i get the following error:

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

For reasons too long to explain I am not binding my data using asp, but rather C#

Can anybody help.
Mr_ShawAsked:
Who is Participating?
 
srikanthreddyn143Commented:
Where ever you are binding data to gridview, check the data.

Or debug the code at the place you are assingng width and look whether you have column or not from watch window or immediate window


That error means that you dont column with index 1
0
 
Andy1988Commented:
Use (i) in place of [1]
0
 
Mr_ShawAuthor Commented:
I only want to effect the width of column 1
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Andy1988Commented:
try this gridProducts.Columns[1].HeaderStyle.Width = 100;
0
 
Mr_ShawAuthor Commented:
Nope..same error
0
 
Andy1988Commented:
You can try with putting BreakPoint on that statement and open the
Immediate Windows and type e.Item.ItemIndex in that and hit the ENTER. You will get the current value for the INdex and see whether it is less than 0 or greater than the total number of rows in the grid.

To open Immediate Windows Menu - Debud>Windows>Immediate


0
 
srikanthreddyn143Commented:
See whether your getting data to gridview?
0
 
HarryNSCommented:
In which event you trying to increase the width of the column???

Check this,

If (Referral_Gridview.Columns != null && Referral_Gridview.Columns.Count >0)
{
Referral_Gridview.Columns[1].ItemStyle.Width = Unit.Pixel(100);
}

If you replace this line Referral_Gridview.Columns[1].ItemStyle.Width = Unit.Pixel(100);  to above if condition and if NO error throws up then it is problem in you binding or event you are increasing the width.

In this case just move this line after Referral_Gridview.DataBind(); statement
0
 
dev_osamaCommented:
you should check first if there is a column with this index or not, as in the comment of HarryNS, after that you can use this line of code to adjust column width :

dataGridView1.Columns[1].Width = 100;;
0
 
Mr_ShawAuthor Commented:
How do I check if there is an index in the column.

0
 
Mr_ShawAuthor Commented:
I am not sure what the error means

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
0
 
Mr_ShawAuthor Commented:
I am not really sure how to use the watch window.

Here is a screen shot of my watch window. Is it possible for you to mark on it where I have to look.
watch-window-screen-shot.bmp
0
 
srikanthreddyn143Commented:
Look at the Columns. Are there any Columns in the gridview?
0
 
Mr_ShawAuthor Commented:
yes. my gridview is populated.... i just need to expand one of the columns.
0
 
srikanthreddyn143Commented:
Check whether the gridview has column collection by clicking '+' sign before Columns in watch window. If you then after binding keep the piece of code specified by Harry/dev
0
 
Mr_ShawAuthor Commented:
hmm not sure... here is a screen shot for you to have a look.

Shall I put the piece of code specified by Harry/dev in the page load or on RowDataBound
watch-window-screen-shot-collect.bmp
0
 
srikanthreddyn143Commented:
Use Column index as 0 not 1
0
 
Mr_ShawAuthor Commented:
Ok.. now error..

But how do i specify which column I want to change the width..
0
 
srikanthreddyn143Commented:
You can specify the column name or column index. your snapshot shows only one column. So index will be 0

dataGridView1.Columns[0].Width = 100;

or

If (Referral_Gridview.Columns != null && Referral_Gridview.Columns.Count >0)
{
Referral_Gridview.Columns[0].ItemStyle.Width = Unit.Pixel(100);
}

as Harry/Dev said
0
 
Mr_ShawAuthor Commented:
I have ten columns.

I am binding my data dynamically

example
gridview1.datasource = dataset;
gridveiw.databind();
0
 
srikanthreddyn143Commented:
Specify Columnm name instead of index

If (Referral_Gridview.Columns != null && Referral_Gridview.Columns.Count >0)
{
Referral_Gridview.Columns["Col1"].ItemStyle.Width = Unit.Pixel(100);
}
0
 
Mr_ShawAuthor Commented:
it does not like me putting a string in place of an int
0
 
srikanthreddyn143Commented:
can you post your code?
0
 
Mr_ShawAuthor Commented:
Referral_Gridview.Columns["Providers"].ItemStyle.Width = Unit.Pixel(100);

The Columns overload is an int not a string.
0
 
srikanthreddyn143Commented:
Can you post the part of code where you are binding data assigning col width
0
 
Mr_ShawAuthor Commented:
       Referral_Gridview.DataSource = DataSet1;
        Referral_Gridview.DataBind();

        Referral_Gridview.Columns["Providers"].ItemStyle.Width = Unit.Pixel(100);
0
 
srikanthreddyn143Commented:
-->  Referral_Gridview.DataSource = DataSet1;
        Referral_Gridview.DataBind();

        Referral_Gridview.Columns["Providers"].ItemStyle.Width = Unit.Pixel(100);

At the arrowed point pls keep breakpoint and check the data in dataset
0
 
Mr_ShawAuthor Commented:
the syntax which you are suggesting will not work because Column[ ] requires a int overload not a string.
0
 
srikanthreddyn143Commented:
You can have index. thats fine. Look at the dataset and check whether do u have any data or not?
0
 
Mr_ShawAuthor Commented:
the code which worked was

 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[4].Width = 500;
        }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.