如何从数据集中查找列值 [英] How to find the coloumn value from a dataset

查看:97
本文介绍了如何从数据集中查找列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个名为dsetGMS的数据集,它包含3个表.

我需要从该数据集中访问table1的列值并获取总数.

如何从此列中获取总计?

Hi,

I have a dataset called dsetGMS and it contains 3 tables.

I need to access the column value of table1 from this data set and obtain the total.

How can I get the total from this column?

推荐答案

这将只需要一行代码:
This will take just one line of code:
Dim sumTotal As Double = dsetGMS.Tables(0).Compute("SUM(value)", Nothing)


这没什么意思.

哪个列值,总计多少?

您想要获取一列的值,然后只写
It doesnt mean anything.

Which column value, what total?

It you want to get the value of a column then just write
dsetGMS.Tables(0).Rows(Rowid).Item(ColumnsName)



如果您想要总计一个特定的列,则循环遍历数据表并对其进行总计.



If you want a total of a particular column then Loop therough the data table and total on it.


这看起来像这样(对不起,我现在必须在不进行测试的情况下编写它,所以更像是伪代码):


That would look something like this (sorry, I must write it without testing right now, so take it more like pseudocode):


DataTable Table;
int Sum;

Table = FilledDataSet.Tables["NameOfTable"];
if(Table != null)
{
    Sum = 0;
    foreach (DataRow Row in Table.Rows)
    {
        Sum += (int)Row["NameOfColumn"];
    }
}



如果该列不存在或值为空,则此代码将遇到问题,因此需要进行一些进一步的错误检查.另外,我假定要累加的值是一个整数,但是对于其他数据类型,可以很容易地更改它.



This code will run into problems if the column does not exist or the value is null, so some more error checking is in order. Also, I assumed the value to be summed up to be an integer, but this can easily be changed for other data types.


这篇关于如何从数据集中查找列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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