Link to home
Start Free TrialLog in
Avatar of BigCap
BigCapFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Gridview - column widths

I have populated a gridview as follows

 Protected Sub fillGrid()
        Me.dsRequestDetail.ConnectionString = Me.strConnectionString
        Me.dsRequestDetail.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
        Me.dsRequestDetail.SelectCommand = "gw_GetRequestDetailByRequestId"
        Me.dsRequestDetail.SelectParameters.Clear()
        Me.dsRequestDetail.SelectParameters.Add("idValue", requestNo)

        Me.gvRequestDetail.DataSource = Me.dsRequestDetail

        'Me.gvRequestDetail.AutoGenerateEditButton = True
        'Me.gvRequestDetail.AutoGenerateDeleteButton = True

        Me.gvRequestDetail.DataBind()

        Me.gvRequestDetail.Columns(0).ItemStyle.Width = 100
        Me.gvRequestDetail.Columns(1).ItemStyle.Width = 200

    End Sub

It works, with the exception of the last 2 width statements. the 2nd line gives an error and says the column I am using is greater than number of columns.

Help!!!

Also, how do you make a column be right justified rather than left, and can the column titles be omitted?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of vbturbo
vbturbo
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And to set column's aligment

Me.gvRequestDetail.Columns("Myfieldname").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
Me.gvRequestDetail.Columns("Myfieldname").Name = DataGridViewContentAlignment.BottomLeft.ToString()

to get the correct amount of column's count

Dim x As Integer = Me.gvRequestDetail.Columns.Count()

vbturbo
Avatar of BigCap

ASKER

I actually did it a different way. I used AutoGenerateColumns="False" on the gridview and then populated each column separately.

<asp:BoundField DataField="Job Ref" ItemStyle-Width="120" HeaderText="" />                  
Hi

Glad you got your solution.
btw. i first now recognised it was asp.net -:) by seeing this line: Me.gvRequestDetail.DataBind()
though when asking questions, you should preliminary state which ver of visual studio 2003,2005 and if it is vb.net or asp.net ect...

vbturbo

And here is everything you need to know about datagridview in asp.net 2.0

http://msdn2.microsoft.com/en-us/library/aa479339.aspx