DataGrid and Index error

Hi Folks, I have a datagrid which displays two columns. I wish to set each column width.
Below is the snippet of coding.
 I receive the following error "Index out of range - must be non-negative and less than the size of the collection"
I understand what the error message means, but I do not understand why I received.
Can someone be as so kind as to explain what I did wrong??
 

Dim vsqltxt As String = ""
   Dim vsep As String = "'"
   Dim dgColStyle As DataGridTableStyle
   
   dgColStyle = New DataGridTableStyle
       
   DsViewCodes1.Clear()
   vsqltxt = "SELECT  RecId,Code, Description FROM Codes where CodeType=" & vsep & xcode & vsep & " and SortOrder <>'A'  ORDER BY SortOrder, Description"
   daBrowseCodes.SelectCommand.CommandText = vsqltxt
   daBrowseCodes.Fill(DsViewCodes1)
   DsViewCodes1.Tables("Codes").Columns("RecId").ColumnMapping = MappingType.Hidden
   dgColStyle.MappingName = "DsViewCodes1"
   dbGridCodes.TableStyles.Add(dgColStyle)
   dgColStyle.GridColumnStyles(1).Width = 75
   dgColStyle.GridColumnStyles(2).Width = 150
   dbGridCodes.AlternatingBackColor = Color.Yellow
   dbGridCodes.DataSource = DsViewCodes1.Tables("Codes")

Any help would be appericated.....
bearpawsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SanclerCommented:
The GridColumnStyles Collection is zero based.  So the first column width should be set by (0) and the second by (1).  Trying to set anything by (2) goes beyond the number of styles you have added.

Roger
bearpawsAuthor Commented:
That's what I thought too, so I had changed them to first column(0) and second column(1)  but I would still receive the same error...
DabasCommented:
Hi bearpaws:
What line is causing the error?
Can you post your amended code?

Dabas
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

SanclerCommented:
You say you have two columns but, reading your code, you only show code relating to one

>>
   DsViewCodes1.Tables("Codes").Columns("RecId").ColumnMapping = MappingType.Hidden
   dgColStyle.MappingName = "DsViewCodes1"
   dbGridCodes.TableStyles.Add(dgColStyle)
<<

I had assumed that you had done something in the designer with regard to the other one.  Is that not the case?  If so, it will be the setting the width for (1) which causes the index out of range exception.

Roger

bearpawsAuthor Commented:
I am using a sql (see vsqltxt) statement to populate the record set. It pulls 3 columns: RecId,Code,Description.
The column "RecId" is hidden by the mappingtype.hidden statement.

Are you saying I have to map the other two columns??

I am really feeling stupid here...:((

SanclerCommented:
Sorry, I should have read your code more thoroughly before.

When you bind a DataGrid to a datasource - in this case, your "Codes" Table - it manages without any express DataGridTableStyle or DataGridColumnStyles.  It just works out the display by looking at the columns in the datasource.  In your case, one of the columns is hidden even before it gets to the datagrid by the MappingType=Hidden, so it just displays the two columns.  If you are happy with what it produces automatically, there is no need to do anything else.  But if you want to alter how the DataGrid displays if it is left to the automatic process, you can only do so by a DataGridColumnStyle which, in turn, means that you have to create and add a DataGridTableStyle to contain those.  You can do this totally yourself - either at the design stage if you already bind the DataGrid to the datasource at that stage, or programmatically.  But by far the easiest way is to let the DataGrid do it for you.  If you add a DataGridTableStyle without any DataGridColumnStyles it will automatically put relevant columns in it by looking at the columns in the datasource.  But it can only do that once it knows what the datasource is and what it contains.  And your code doesn't tell it that until

   dbGridCodes.DataSource = DsViewCodes1.Tables("Codes")

which is after you have tried to access the columns

   dgColStyle.GridColumnStyles(1).Width = 75
   dgColStyle.GridColumnStyles(2).Width = 150

So, no, you don't have to "map the other two columns".  But if you are going to let that happen automatically what you do need to do is leave changing the column widths until after the DataGrid, via the binding, has found out how many columns there are.  

All you need to do is move those two lines of code to the end, and change the indexes to a zero-base, and I reckon (although I haven't had a chance to test) that it should be OK.

Roger
bearpawsAuthor Commented:
Thank you for responding,it is appericated
And sometimes I have to read things a million times before I understand....:-)
but it was no go...
I even tried commenting out the hidden column.
SanclerCommented:
This is working for me (albeit with slightly different data)

        Dim ds As New DataSet
        Dim vsqltxt As String = ""
        vsqltxt = "SELECT OrderID, Freight, OrderDate, ShippedDate FROM Orders"
        OleDbDataAdapter1.SelectCommand.CommandText = vsqltxt
        OleDbDataAdapter1.Fill(ds)
        ds.Tables("Orders").Columns("OrderID").ColumnMapping = MappingType.Hidden
        Dim ts As New DataGridTableStyle
        ts.MappingName = "Orders"
        DataGrid1.TableStyles.Add(ts)
        DataGrid1.DataSource = ds.Tables("Orders")
        DataGrid1.TableStyles(0).AlternatingBackColor = Color.Yellow
        DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 75
        DataGrid1.TableStyles(0).GridColumnStyles(1).Width = 100

This version

        Dim ds As New DataSet
        Dim vsqltxt As String = ""
        vsqltxt = "SELECT OrderID, Freight, OrderDate, ShippedDate FROM Orders"
        OleDbDataAdapter1.SelectCommand.CommandText = vsqltxt
        OleDbDataAdapter1.Fill(ds)
        ds.Tables("Orders").Columns("OrderID").ColumnMapping = MappingType.Hidden
        Dim ts As New DataGridTableStyle
        ts.MappingName = "Orders"
        DataGrid1.TableStyles.Add(ts)
        DataGrid1.TableStyles(0).AlternatingBackColor = Color.Yellow
        DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 75 '<<<< error
        DataGrid1.TableStyles(0).GridColumnStyles(1).Width = 100
        DataGrid1.DataSource = ds.Tables("Orders")

produces the index out of range error where marked.

Roger

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.