【问题标题】:Getting the row number of the dataset by using datagridview's currentrow property使用datagridview的currentrow属性获取数据集的行号
【发布时间】:2012-02-07 15:00:19
【问题描述】:

请帮我解决这个问题。我有一个绑定到数据集的 datagridview,当我选择要删除的 datagridview 项目时,我得到了正确的项目。但是当我将 datagridview 排序到某个字段时,它返回一个不同的项目。这是我的代码:

DSet.Tables("sec_company_address").Rows(sec_company_address.CurrentRow.Index).Delete()

编辑:我尝试了这一行,但它带来了一个错误。

Dim xID As String = sec_role_module.CurrentRow.Cells("nID").Value.ToString
sDataSet.Tables(sec_role_module.Name).Rows.Find(xID).Delete()

【问题讨论】:

    标签: vb.net ado.net datagridview dataset


    【解决方案1】:

    首先,您必须在sec_company_address 中设置Primary 列,这样DELETE 才能起作用。试试这个:

    ' change columnName to your column which is the Unique Identifier
    dim xID as string = dataGridView1.CurrentRow.Cells("columnName").Value.Tostring 
    DSet.Tables("sec_company_address").Rows.Find(xID).Delete()
    DSet.Tables("sec_company_address").AcceptChanges()
    

    更新

    这个怎么样?

        For Each xRow As DataRow In sec_role_module.SelectedRows
            sDataset.Tables(sec_role_module.Name).Rows.Remove(xRow)
            sDataset.AcceptChanges()
        Next
    

    【讨论】:

    • 感谢您的回答..我尝试了您的建议,但它返回错误...表没有主键..但我的表有它...我想知道为什么。
    • 你能贴一张你的数据集和你的datagridview的截图吗?
    【解决方案2】:

    通过更改 datagridview 的排序,您可以引用数据集中的另一个项目,因为您的数据集仍然与初始加载相同。

    你怎么能解决这个问题? 您可以在第一列中使用唯一标识符,您可以参考它来删除数据集中的行。

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2012-03-24
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多