如何查看数据表是否已有一行。 [英] How to see if a datatable has one row already.

查看:101
本文介绍了如何查看数据表是否已有一行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个按钮。该表单还包含8个文本框。文本框填写完毕后;选择该按钮将数据保存在名为SetUpTable的数据表中。

如果再次按下按钮以防止发生错误,我已经输入 If语句来检查并查看数据表是否包含行。 (最多只能有1行)但是我使用的代码读取0行,而不管数据表是新的没有行还是已经有1行。那么我做错了什么?



在调试模式下,当我第二次按下按钮但是表达式

 SetupTable.Rows.Count 

表示行数为0.



我尝试过:



 公共 SetupTable  As  DataTable =  DataTable()

私有 Sub Button1_Click(发件人作为 对象, e As EventArgs)句柄 Button1.Click
Dim 标题作为 字符串 = SetUp
Dim rowws 作为 整数
rowws = SetupTable.Rows.Count ' 用于衡量行数
如果 SetupTable.Rows.Count> 0 然后 ' 如果为true则更改第1行中的数据
SetupTable.Rows( 0 )。Item( 0 )= WFileDir.Text
SetupTable.Rows( 0 )。项目( 1 )= YBFileDir.Text
SetupTable.Rows( 0 )。项目( 2 )= MFileDir.Text
SetupTable.Rows( 0 )。项目( 3 )= Port.Text
SetupTable.Rows( 0 )。项目( 4 )= Cred.Text
SetupTable.Rows( 0 )。Item( 5 )= Host.Text
SetupTable.Rows( 0 )。Item( 6 )= EmailFrom.Text
SetupTable.Rows( 0 )。Item( 7 )= EmailTo.Text
其他 ' 如果为false则创建新的数据行
SetupTable.Columns.AddRange( New DataColumn(){
DataColumn(WFileDir.Text, GetType String )),
DataColumn(YBFileDir.Text, GetType String )),
DataColumn(MFileDir.Text, GetType String )),
DataColumn(Port.Text , GetType String )),
DataColumn(Cred.Text, GetType String )),
DataColumn(Host.Text, GetType String )),
DataColumn(EmailFrom.Text, GetType String )),
DataColumn(EmailTo.Text, GetType String ))})

结束 如果

解决方案

您实际上没有插入行 - 您创建了列。



在此处插入此行你的Else块的结尾(在创建列之后):

 SetupTable.Rows.Add(SetupTable.NewRow)


I have a button on a form. The form also contains 8 text boxes. Once the text boxes have been filled out; the button is selected to save the data in a datatable called SetUpTable.
If the button is pressed again to prevent a error occurring, I have put in an If statement to check and see if the datatable contains a row. (There should only be 1 row maximum) However the code I am using reads 0 rows irrespective of whether the datatable is new with no rows or already has 1 row. So what am I doing wrong?

In debug mode, I can see that the datatable contains one row when I press the button the second time but the expression

SetupTable.Rows.Count

states the row count to be 0.

What I have tried:

Public SetupTable As DataTable = New DataTable()

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Title As String = "SetUp"
        Dim rowws As Integer
        rowws = SetupTable.Rows.Count  'Used to measure the row count
        If SetupTable.Rows.Count > 0 Then 'if true then change the data in row 1
            SetupTable.Rows(0).Item(0) = WFileDir.Text
            SetupTable.Rows(0).Item(1) = YBFileDir.Text
            SetupTable.Rows(0).Item(2) = MFileDir.Text
            SetupTable.Rows(0).Item(3) = Port.Text
            SetupTable.Rows(0).Item(4) = Cred.Text
            SetupTable.Rows(0).Item(5) = Host.Text
            SetupTable.Rows(0).Item(6) = EmailFrom.Text
            SetupTable.Rows(0).Item(7) = EmailTo.Text
        Else     'if false then create a new row of data
            SetupTable.Columns.AddRange(New DataColumn() {
            New DataColumn(WFileDir.Text, GetType(String)),
            New DataColumn(YBFileDir.Text, GetType(String)),
            New DataColumn(MFileDir.Text, GetType(String)),
            New DataColumn(Port.Text, GetType(String)),
            New DataColumn(Cred.Text, GetType(String)),
            New DataColumn(Host.Text, GetType(String)),
            New DataColumn(EmailFrom.Text, GetType(String)),
            New DataColumn(EmailTo.Text, GetType(String))})

        End If

解决方案

You don't actually insert a row - you create columns.

Insert this line at the end of your Else-block (after creating the columns):

SetupTable.Rows.Add(SetupTable.NewRow)


这篇关于如何查看数据表是否已有一行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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