关闭表单时出错 [英] Error when closing form

查看:81
本文介绍了关闭表单时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的程序

    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



只是说,其中一种称为此过程的窗体,当该窗体关闭时,它将具有"ArgumentOutOfRangeException"异常.但是,如果我删除了可疑部分,它不会出现任何错误...

请我需要您的帮助,谢谢...

另一个问题是,当更改 DataGridView.AllowUserToAddRows 时,datagridview是否将某些内容传递给该列,以告知该列 DataGridView.AllowUserToAddRows 是假还是假? ="h2_lin">解决方案

之所以给出此异常,是因为该列中的参数(或简称为value)超出范围.

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

祝你好运!


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?

解决方案

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!


这篇关于关闭表单时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆