该行已经属于此表vb.net [英] This row already belongs to this table vb.net

查看:119
本文介绍了该行已经属于此表vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想避免数据表中出现重复数据.
错误指向

I just want to avoid duplicate data in my datatable.
The error points at

dt.Rows.Add(row)



我的代码有什么问题?



What is wrong in my code?

Dim dtSH As DataTable = wfms.formflow.getshopheadbycostcentreid(CostCtr)
 
For i As Integer = 0 To dtSH.Rows.Count - 1

   Dim row As DataRow = dt.NewRow()
   row = dt.NewRow()

    If dt.Rows.Count <> 0 Then

'loop each dt.rows'
      For countrow As Integer = 0 To dt.Rows.Count - 1 

'to determine whether new row equals data in dt'
        If dt.Rows(countrow)("EMPID_T").ToString <> row("EMPID_T").ToString Then

               row("EMPID_T") = dtSH.Rows(i)("EMPID_T")
               row("NAME_T") = dtSH.Rows(i)("NAME_T")
               dt.Rows.Add(row)

         End If

       Next

Else
                  row = dt.NewRow()
                  row("EMPID_T") = dtSH.Rows(i)("EMPID_T")
                  row("NAME_T") = dtSH.Rows(i)("NAME_T")
                  dt.Rows.Add(row)
End If


Next



///更新:

我要做的实际上是检索不同的数据并绑定到dt中.是否有任何方法可以在不使用SQL的情况下使用VB代码将不同的数据选择到数据表中?



/// update:

All I want to do is actually retrieve the distinct data and bind into dt. Is there any method to select distinct data into datatable using vb code without using SQL?

推荐答案

我一定在做某事...

您的代码要像我想的那样糟糕
I must have been on somthing...

Your code want as bad as I thought
Dim dtNewRow As DataRow   
dtNewRow = resultsDataTable.NewRow()   
dtNewRow.Item("id") = 0   
dtNewRow.Item("name") = "Joe Blogs"  
resultsDataTable.Rows.Add(dtNewRow) 



但是,为什么必须要遍历行的For循环,这还不够吗?无法复制完整的副本,如下所示:




But Why do you have to For loops that goes through the row, isnt one enough? Cant the complete copy look like this:


Dim dtSH As DataTable = wfms.formflow.getshopheadbycostcentreid(CostCtr)
 
            For i As Integer = 0 To dtSH.Rows.Count - 1
                  Dim row As DataRow = dt.NewRow()
                  row("EMPID_T") = dtSH.Rows(i)("EMPID_T")
                  row("NAME_T") = dtSH.Rows(i)("NAME_T")
                  dt.Rows.Add(row)
            next 





And shouldnt that be all?


在vb.code中使用循环会降低系统性能.
我想到的一种方法是用不同的数据过滤数据表.

示例:
dt数据表中的数据:

//添加新行
ID 1
名字约翰

//添加新行
ID 2
姓名玛丽

//添加新行
ID 1
名字约翰

如果将dt绑定到Gridview1,它将变为:

1约翰
2玛丽
1约翰

实现此方法以区分数据表:

Using looping in vb.code will decrease the system performance.
One way that comes to my mind is by filtering the datatable with distinct data.

Example:
Data in dt datatable:

//Add new row
ID 1
Name John

//Add new row
ID 2
Name Mary

//Add new row
ID 1
Name John

if bind dt to Gridview1 it becomes:

1 John
2 Mary
1 John

Implement this method to distinct the datatable:

Dim dttbl As DataTable = dt.DefaultView.ToTable(True)

    GridView1.DataSource = dttbl
    GridView1.DataBind()



结果变成:

1约翰
2玛丽



SO the result becomes:

1 John
2 Mary


这篇关于该行已经属于此表vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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