vb.net 计算数据集中有多少行,然后在文本框中显示为文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7894954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count how many rows are in the dataset then display as text in a textbox?
提问by MDL
How can I count how many rows are returned by a dataset and then show the total number of rows as textbox.text and read-only so that the user can only see them but not change them?
如何计算数据集返回的行数,然后将总行数显示为 textbox.text 和只读,以便用户只能看到它们但不能更改它们?
so far I have this but it dosent return a number and says it cant find table 0:
到目前为止,我有这个,但它不返回一个数字并说它找不到表 0:
tbRecordsFound.Text = ds.Tables(0).Rows.Count
回答by James Johnson
Try something like this:
尝试这样的事情:
tbRecordsFound.Text = ds.Tables.Cast<DataTable>().Sum(x => x.Rows.Count).ToString()
You can also do it like this:
你也可以这样做:
Dim recordCount as Integer = 0;
For Each table as Datatable in ds.Tables
recordCount += table.Rows.Count
tbRecordsFound.Text = recordCount.ToString()
回答by JJC
tbRecordsFound.Text = ds.Tables(0).Rows.Count
The above code will work, however you need to give the table an identifier, such as:
上面的代码可以工作,但是你需要给表一个标识符,例如:
tbRecordsFound.Text = ds.Tables("TableName").Rows.Count
This can be done via creating a DataAdapter and using the "Fill" function to give the table a name. Here is an example where "da" represents the DataAdapter:
这可以通过创建 DataAdapter 并使用“Fill”函数为表命名来完成。下面是一个示例,其中“da”代表 DataAdapter:
da.Fill(ds, "TableName")
回答by royhette agapito
For i As Integer = 0 To yourdatagridviewName.Rows.Count() - 1 Step +1
i = +i
TextBox2.Text = i
Next