Link to home
Create AccountLog in
Avatar of Mr_Shaw
Mr_Shaw

asked on

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.
Avatar of Andy1988
Andy1988

Use (i) in place of [1]
Avatar of Mr_Shaw

ASKER

I only want to effect the width of column 1
try this gridProducts.Columns[1].HeaderStyle.Width = 100;
Avatar of Mr_Shaw

ASKER

Nope..same error
SOLUTION
Avatar of Andy1988
Andy1988

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of srikanthreddyn143
See whether your getting data to gridview?
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Mr_Shaw

ASKER

How do I check if there is an index in the column.

Avatar of Mr_Shaw

ASKER

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"
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Mr_Shaw

ASKER

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
Look at the Columns. Are there any Columns in the gridview?
Avatar of Mr_Shaw

ASKER

yes. my gridview is populated.... i just need to expand one of the columns.
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
Avatar of Mr_Shaw

ASKER

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
Use Column index as 0 not 1
Avatar of Mr_Shaw

ASKER

Ok.. now error..

But how do i specify which column I want to change the width..
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
Avatar of Mr_Shaw

ASKER

I have ten columns.

I am binding my data dynamically

example
gridview1.datasource = dataset;
gridveiw.databind();
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);
}
Avatar of Mr_Shaw

ASKER

it does not like me putting a string in place of an int
can you post your code?
Avatar of Mr_Shaw

ASKER

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

The Columns overload is an int not a string.
Can you post the part of code where you are binding data assigning col width
Avatar of Mr_Shaw

ASKER

       Referral_Gridview.DataSource = DataSet1;
        Referral_Gridview.DataBind();

        Referral_Gridview.Columns["Providers"].ItemStyle.Width = Unit.Pixel(100);
-->  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
Avatar of Mr_Shaw

ASKER

the syntax which you are suggesting will not work because Column[ ] requires a int overload not a string.
You can have index. thats fine. Look at the dataset and check whether do u have any data or not?
Avatar of Mr_Shaw

ASKER

the code which worked was

 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[4].Width = 500;
        }