【问题标题】:I need to know how to avoid the System.Data.DataRowview in vb 2010我需要知道如何避免 vb 2010 中的 System.Data.DataRowview
【发布时间】:2013-02-27 20:19:06
【问题描述】:

我的代码有问题;并希望能得到一些帮助,让她做我想做的事,而不会出现烦人的错误。读取数据绑定列表框字符串,找到用户选择的字符串并将所述字符串写入文件。

    Private Sub btnContinue_Click(sender As System.Object, e
    As System.EventArgs) Handles btnContinue.Click
    '  1st file and mySQL Update to testing table
    If txtLocation.Text.ToString <> "" And txtUnitTested.Text.ToString <> "" Then 
        Dim filewriter As New System.IO.StreamWriter(
               "C:\Users\OER\Documents\Visual Studio 2010\Projects\frmTest1_0.txt")
        Dim curItem = lstCustomer.SelectedItem
        Dim now As DateTime = DateTime.Now
        With filewriter
            .WriteLine(vbCrLf + (now.ToString("F"))) ' Write the DateTime to the file
        End With

        For Each objDataRowView As DataRowView In lstCustomer.SelectedItems
            Dim item As String
            For Each item As DataRowView In Me.lstCustomer.Items.Item("customer")
                item = String.Parse(lstCustomer.SelectedValue)
                WriteLine(curItem(item))
            Next item
        Next '  THIS IS THE FOR LOOP I AM HAVING ISSUES WITH!!!!!!!

        With filewriter
            .WriteLine(vbCrLf + txtLocation.Text + vbCrLf + txtUnitTested.Text)
        End With
        filewriter.Close()
        frmTest1.Show()
    Else
        MsgBox("Please Type the Location and Unit Tested!!!", vbCritical)
        txtLocation.Clear()
        txtUnitTested.Clear()
        txtLocation.Focus()
    End If

【问题讨论】:

  • 你得到了什么“恼人的错误”?我怀疑它是HavingIssueException
  • 不,先生,更多的是逻辑错误(程序将 System.Data.DataView 添加到我的数据库中)
  • 就好像列表框中的数据需要单独声明或者与数据绑定列表一起声明,可以这么说
  • 是的,上面有逻辑问题。但是,你能显示加载 ListBox 的代码吗?
  • 错误是

标签: vb.net vb.net-2010 filewriter databound-controls


【解决方案1】:

这些循环:

For Each objDataRowView As DataRowView In lstCustomer.SelectedItems
     Dim item As String
     For Each item As DataRowView In Me.lstCustomer.Items.Item("customer")
         item = String.Parse(lstCustomer.SelectedValue)
             WriteLine(curItem(item))
     Next item
Next 

会给你带来一些问题。您已经声明了一个与另一个变量同名的循环变量。然后你给它赋值。

另外,我不太清楚这两个循环的目的是什么。我对您在这部分代码中要完成的工作感到有些困惑。我将从重命名这一行中的变量开始:

Dim item as String

我将以此为例:

Dim result as string

做别的东西,然后你不能在循环中改变 item 的值(因为那是循环变量)。这会导致错误。

将循环内的代码更改为:

result = String.Parse(lstCustomer.SelectedValue)

很可能这仍然无法将您带到您想要去的地方,但这是一个好的开始。除非您的 lstCustomer 中的每个项目都包含多个要迭代的项目,否则我认为您可能只需要第一个外部循环。

如果你的循环是这样开始的:

For Each objDataRowView As DataRowView In lstCustomer.SelectedItems

那么我想你会想要:

objDataRowView.Item("customer")

作为输出。我认为这将是您表中客户字段的值。

你基本上是说这一行有一个名为客户的列......我想要那个值。

【讨论】:

  • +1 表示尝试回答,尽管根据问题很难理解发生了什么。
  • 谢谢@D;非常感谢您的回答
  • 一旦你整理出循环,我想你只需要引用数据行中的正确项目而不是数据行本身作为输出。我需要查看您将列表绑定到的数据表的结构才能确定。
  • id primary auto_increment time (varchar, 40) customer (varchar, 40) MyIsam database, utf8 unicode_ci collat​​ion
猜你喜欢
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 2014-05-26
  • 2017-07-25
  • 2016-02-10
相关资源
最近更新 更多