Solved

datagridview column width

Posted on 2010-08-16
4
701 Views
Last Modified: 2013-11-27
Hello:
I am trying to setup the width of my id column in a datagrid view.  I keep getting errors like "index was out of range" eventhough my grid has 10 columns and I want to hide the last column setting up the width to 0.  What am i doing wrong?  Please see code below.  Thank you.

Private Sub frmShowAllProspects_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sqlConnection As SqlConnection
        Dim sqlCommand As SqlCommand
        Dim dt As New DataTable


        sqlConnection = New SqlConnection("Connection String")
        sqlCommand = New SqlCommand("ssmc_te_getprospecthistorybyduetocontact", sqlConnection)
        sqlCommand.CommandType = CommandType.StoredProcedure

        Dim da As SqlDataAdapter
        da = New SqlDataAdapter(sqlCommand)
        Me.mydgCustomer.Columns(9).Width = 0

       
        da.Fill(dt)

        mydgCustomer.DataSource = dt


    End Sub

Open in new window

0
Comment
Question by:gloriagalvez
  • 2
4 Comments
 
LVL 3

Expert Comment

by:yanoch
ID: 33447411
Me.mydgCustomer.Columns(9) does not exist ... you may validate if the lenght of Me.mydgCustomer.Columns is > 9 before setting the width

you should also fill and bind the datagrid and then after set the width.

Like that :

Private Sub frmShowAllProspects_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sqlConnection As SqlConnection
        Dim sqlCommand As SqlCommand
        Dim dt As New DataTable


        sqlConnection = New SqlConnection("Connection String")
        sqlCommand = New SqlCommand("ssmc_te_getprospecthistorybyduetocontact", sqlConnection)
        sqlCommand.CommandType = CommandType.StoredProcedure

        Dim da As SqlDataAdapter
        da = New SqlDataAdapter(sqlCommand)
        da.Fill(dt)

        mydgCustomer.DataSource = dt

        Me.mydgCustomer.Columns(9).Width = 0


    End Sub
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 250 total points
ID: 33447441
Put this line:

Me.mydgCustomer.Columns(9).Width = 0

after this line:

mydgCustomer.DataSource = dt

You are setting the width of your datagridview before assigning it columns,thus the grid has no column 9 yet!
0
 

Author Closing Comment

by:gloriagalvez
ID: 33447450
I just realized what I was trying to do.  I filled and binded after setting the width.  sorry .. thank you so much.
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33447506
I just proposed the same exact solution 3 min before...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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