Solved

datagridview column width

Posted on 2010-08-16
4
698 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now