Solved

datagridview column width

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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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