首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

DataGridView编辑有关问题

2012-03-15 
DataGridView编辑问题小弟在学习.net winform,做了一个客户管理来练手。却不想实测的时候出现了意想不到,又

DataGridView编辑问题
小弟在学习.net winform,做了一个客户管理来练手。却不想实测的时候出现了意想不到,又无从下手的错误。

我本人测试的时候根本就没测试出来,呵呵,还是朋友厉害。

1、界面如图


2、会报错的操作方式


3、不会报错的操作方式


4、错误提示:

在 System.ArgumentOutOfRangeException 中第一次偶然出现的“mscorlib.dll”类型的异常
LoadMember错误:[索引超出范围。必须为非负值并小于集合大小。
参数名: index]

5、代码
操作顺序,点击“新建公司”,在左边的列表中出现一个名为“新建公司”的列表项,点示该项,在右边修改公司名称等,即时输入,即时保存。

点击公司后,右边会出现联系人列表,直接点击输入,即输即存。

VB.NET code
Public Class frmCustomer    Private curCorpID As String    Private curCorpNodeIndex As Integer    '载入企业数据    Private Sub LoadData()        Dim sql As String        Dim dt As DataTable        Dim Conn As New DAL.SQLTools        Dim iCount As Long, i As Long        Dim parentNode As Windows.Forms.TreeNode        Dim CorpName As String, CorpID As String        '关键字        Dim strKeyword As String        strKeyword = txtKeyword.Text        sql = "select * From 客户_公司 where 1=1"        If strKeyword <> "" Then            sql = sql & " and 公司名称 like '%" & strKeyword & "%'"        End If        sql = sql & " Order by 公司名称 ASC"        dt = Conn.pExecute(sql).Tables(0)        iCount = dt.Rows.Count        If iCount <= 0 Then Exit Sub        lstCorp.Nodes.Clear()        parentNode = lstCorp.Nodes.Add("我的企业客户")        For i = 0 To iCount - 1            With parentNode                CorpID = dt.Rows(i)("ID")                CorpName = dt.Rows(i)("公司名称")                Dim node As New Windows.Forms.TreeNode                node.Text = CorpName                node.ToolTipText = CorpID                .Nodes.Add(node)            End With        Next        lstCorp.ExpandAll()    End Sub    '载入单个企业数据    Private Sub LoadCorpInfo(ByVal CorpID As String)        Dim sql As String        Dim Conn As New DAL.SQLTools        Dim dt As New DataTable        Dim CorpName As String, Tel As String, Fax As String, Address As String        sql = "select * From 客户_公司 where ID=" & CorpID        dt = Conn.pExecute(sql).Tables(0)        If dt.Rows.Count > 0 Then            curCorpID = CorpID            CorpName = dt.Rows(0)("公司名称").ToString            Address = dt.Rows(0)("地址").ToString            Tel = dt.Rows(0)("电话").ToString            Fax = dt.Rows(0)("传真").ToString            txtCorpName.Text = CorpName            txtAddress.Text = Address            txtTel.Text = Tel            txtFax.Text = Fax        End If    End Sub    '载入联系人列表    Private Sub LoadMember()        Try            Dim sql As String            Dim dt As DataTable            Dim Conn As New DAL.SQLTools            sql = "select ID,姓名,性别,职位,手机,QQ,邮箱,座机,生日 From 客户_联系人 where 公司ID=" & curCorpID & " Order BY ID ASC"            dt = Conn.pExecute(sql).Tables(0)            lstMember.DataSource = dt        Catch ex As Exception            Debug.Print("LoadMember错误:[" & ex.Message & "]")        End Try    End Sub    Private Sub frmCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        LoadData()    End Sub    '新建公司客户    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click        Dim sql As String        Dim Conn As New DAL.SQLTools        Dim intTemp As Long        sql = "Insert Into 客户_公司 (公司名称) Values ('新建公司')"        intTemp = Conn.iExecute(sql)        LoadData()    End Sub    Private Sub frmCustomer_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize        TabControl1.Width = Me.Width - 260        TabControl1.Height = Me.Height - 194        lstCorp.Height = Me.Height - 133    End Sub    Private Sub lstCorp_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles lstCorp.AfterSelect        If e.Node.Index < 0 Then            '重新读取列表            LoadData()            Exit Sub        End If        '读取公司信息,显示在右边        curCorpNodeIndex = e.Node.Index        curCorpID = e.Node.ToolTipText        If curCorpID = "" Then Exit Sub        LoadCorpInfo(curCorpID)        '读取该公司的联系人列表        lstMember.Visible = True        LoadMember()    End Sub    Private Sub lstMember_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles lstMember.CellClick        If e.ColumnIndex > 0 And e.RowIndex > 0 Then            'lstMember.CurrentCell = lstMember.Rows(e.RowIndex).Cells(e.ColumnIndex)            'lstMember.BeginEdit(True)        End If    End Sub    Private Sub txtCorpName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCorpName.TextChanged        Dim intTemp As Long, strName As String        If curCorpID & "" = "" Then            MsgBox("请在左边选择一个公司!")            Exit Sub        End If        strName = txtCorpName.Text        intTemp = SaveOneFieldVal("客户_公司", "公司名称", strName, " and ID=" & curCorpID, "Edit")        '修改公司名称的时候,更新左边的列表对应项文本        lstCorp.Nodes(0).Nodes(curCorpNodeIndex).Text = strName    End Sub    Private Sub txtAddress_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAddress.TextChanged        Dim intTemp As Long, strName As String        If curCorpID & "" = "" Then Exit Sub        strName = txtAddress.Text        intTemp = SaveOneFieldVal("客户_公司", "地址", strName, " and ID=" & curCorpID, "Edit")    End Sub    Private Sub txtTel_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTel.TextChanged        Dim intTemp As Long, strName As String        If curCorpID & "" = "" Then Exit Sub        strName = txtTel.Text        intTemp = SaveOneFieldVal("客户_公司", "电话", strName, " and ID=" & curCorpID, "Edit")    End Sub    Private Sub txtFax_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFax.TextChanged        Dim intTemp As Long, strName As String        If curCorpID & "" = "" Then Exit Sub        strName = txtFax.Text        intTemp = SaveOneFieldVal("客户_公司", "传真", strName, " and ID=" & curCorpID, "Edit")    End Sub    Private Sub txtKeyword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKeyword.TextChanged        LoadData()    End Sub    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        LoadData()    End Sub    Private Sub lstMember_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles lstMember.CellValueChanged        If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Exit Sub        Try            '获取ID            Dim curPersonID As String, intTemp As Long, strValue As String, sql As String, strField As String            Dim Conn As New DAL.SQLTools            curPersonID = lstMember.Rows(e.RowIndex).Cells(0).Value.ToString            '保存当前单元格的值            strValue = lstMember.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString            strField = lstMember.Columns(e.ColumnIndex).HeaderText            '保存前检测参数            If curCorpID = "" Or curCorpID = "0" Then Exit Sub            If curPersonID <> "" Then                sql = " and 公司ID=" & curCorpID                sql = sql & " and ID=" & curPersonID                intTemp = SaveOneFieldVal("客户_联系人", strField, strValue, sql, "Edit")            Else                sql = "insert into 客户_联系人 (公司ID," & strField & ") Values ('" & curCorpID & "','" & strValue & "')"                intTemp = Conn.iExecute(sql)            End If            LoadMember()        Catch ex As Exception            Debug.Print("lstMember_CellValueChanged错误:[" & ex.Message & "]")        End Try    End Sub    Private Sub lstMember_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles lstMember.CellEnter        If e.ColumnIndex > 0 And e.RowIndex >= 0 Then            lstMember.CurrentCell = lstMember.Rows(e.RowIndex).Cells(e.ColumnIndex)            lstMember.BeginEdit(True)        End If    End SubEnd Class


[解决办法]
群里帮顶.

VB.NET
[解决办法]
索引超出数组范围了

你看下面这句,是不是Conn.pExecute(sql).Tables.Count=0了?
dt = Conn.pExecute(sql).Tables(0)

加个判断
dim ds DataSet =Conn.pExecute(sql)
if(ds.Tables.Count>0) then
dt=ds.Tables(0)
end if

[解决办法]
没见过这样的错误...

不过最好在数据缓存过程进行确认。
dgv为datagridview
VB.NET code
    '脏数据的缓存确认    Private Sub dgv_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv.CurrentCellDirtyStateChanged        If dgv.IsCurrentCellDirty Then            dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)        End If    End Sub
[解决办法]

会报错的操作方式中,如操作图那样,点击DATAGIRDVIEW以外的时候 
你看是不是 进入了 点击左侧列表的事件

热点排行