Click here to Skip to main content
15,918,258 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a procedure that looks like this
    Public Shared Sub FillMultiColumn(ByVal srcDataGridView As DataGridView, ByVal strColumnWidths As String, _
                                      ByVal intColumnNumber As Integer, ByVal strCommand As String, ByVal columnName As String, _
                                      ByVal strDisplayMember As String, ByVal strValueMember As String, _
                                      Optional ByVal srcCommandType As CommandType = CommandType.Text)
        clsConnections.SetSQL()
        Try
            Dim myCmd As SqlCommand = New SqlCommand(strCommand, clsConnections.connSQL)
            myCmd.CommandType = srcCommandType
            Dim da As SqlDataAdapter = New SqlDataAdapter(myCmd)
            Dim ds As New DataSet
            da.Fill(ds, "temp")
            Dim dt As DataTable = ds.Tables("temp")
            Dim position = srcDataGridView.Columns(columnName).Index
            Dim newColumn As New DataGridViewMultiColumnComboColumnDemo.DataGridViewMultiColumnComboColumn
            newColumn.AutoSizeMode = srcDataGridView.Columns(columnName).AutoSizeMode
            newColumn.HeaderText = srcDataGridView.Columns(columnName).HeaderText
            newColumn.Width = srcDataGridView.Columns(columnName).Width
            newColumn.Name = columnName
            newColumn.CellTemplate = New DataGridViewMultiColumnComboColumnDemo.DataGridViewMultiColumnComboCell
            newColumn.DataSource = dt
            newColumn.DisplayMember = strDisplayMember
            newColumn.ValueMember = strValueMember
            If srcDataGridView.ColumnCount > 0 Then
                srcDataGridView.Columns.Remove(columnName)
            End If
''' ----> the suspicious part
            srcDataGridView.Columns.Insert(position, newColumn)
''' ----> end of the suspicious part
            newColumn = Nothing
            ds.Dispose()
            myCmd.Dispose()
            myCmd.Dispose()
        Catch ex As Exception
            MsgBox("Error : FillMultiColumn in clsProcedures." & vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "Error")
        Finally
            clsConnections.connSQL.Close()
            clsConnections.connSQL.Dispose()
        End Try
    End Sub


Let's just say, one of the forms called this procedure and when that form closes, it will have "ArgumentOutOfRangeException" exception. But if i remove the suspicious part, it won't give any error...

Please i need your help, thank you...

Additional question, when the DataGridView.AllowUserToAddRows is changed, does the datagridview pass something to the column that tells the column that the DataGridView.AllowUserToAddRows is false or true?
Posted
Updated 20-Apr-11 0:07am
v2
Comments
candice_Blav 19-Apr-11 21:22pm    
Thanks for the solution, I tried to test this procedure to another project, but it went well. By the way, I found out something. It goes something like this :

I have a tabControl with 2 tab pages. Each tab has a datagridview. The datagridview in tab 2 is the one using the above procedure.

Situation 1 : When the form is loaded, tab 1 is being shown and no error was displayed but when you close the form after opening the form, the "ArgumentOutOfRangeException" shows.

Situation 2 : After the form is loaded, when you go to tab 2, and then close the form, no error shows.

Is it possible it's because the datagridview in tab 2 hasn't repainted?... I'm not sure of my theory but hope you can help me..

Hope you can help me.
candice_Blav 20-Apr-11 5:46am    
again, i think i found the suspect. If the DataGridView.AllowUserToAddRows is changed to false, that triggers the error. But when i removed the DataGridView.AllowUserToAddRows part, it went ok.

1 solution

This exception is given because the argument (or simply value) in that column is out of range.

Have a look at DataGridViewColumn:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.displayindex.aspx[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900