Solved

datagridview column width

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 98
Error building VS2105 solution from repository 1 62
PowerShell: ForEach-Object Export to CSV 4 93
SQL Server Serialization error 8 48
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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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