Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

datagridview column width

Posted on 2010-08-16
4
Medium Priority
?
705 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 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