C# 使用列名从数据视图中查找值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17230858/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:53:32  来源:igfitidea点击:

Find value from the dataview using column name

c#asp.netasp.net-mvcado.net

提问by Muhammad Rizwan Shahid

I am trying to find value from dataview using column name. My dataset shows value but my if condition returns false. I am working in asp.net using c#.

我正在尝试使用列名从数据视图中查找值。我的数据集显示值,但我的 if 条件返回 false。我正在使用 c# 在 asp.net 中工作。

I have tried with different code. I am trying to get value like this

我尝试过不同的代码。我试图获得这样的价值

dv[0].DataView.Table.Columns[0].ToString().Contains("52")
   //RETURN FALSE

OR

dv[0].Equals("52")
   // //RETURN FALSE

OR

或者

dv[0].Find("52")
   // //RETURN FALSE

Following is my dataset enter image description here

以下是我的数据集 在此处输入图片说明

回答by Steven Jennings

int rowIndex = dv.Find("52");

if (rowIndex == -1) {
    Console.WriteLine("No match found.");
} 
else {
    Console.WriteLine("{0}, {1}", 
    dv(rowIndex)("GBA_Nbr_GBAccount").ToString(), 
    dv(rowIndex)("GBA_Nam_GBAccount").ToString());
}

I think that may be the solution to your problem, or at the very least point you in the right direction.

我认为这可能是您问题的解决方案,或者至少为您指明了正确的方向。

回答by BhargavaKatta

If "GBA_Nbr_GBAccount" column is a string type, it may contains spaces. You should trim text before comparing. Try this

如果“GBA_Nbr_GBAccount”列是字符串类型,则可能包含空格。您应该在比较之前修剪文本。尝试这个

dv[0]["GBA_Nbr_GBAccount"].ToString().Trim().Equals("52");

回答by Daniel J.G.

You could use Linqto query the datatable or the dataview. For example, assuming your column is of type string:

您可以Linq用来查询数据表或数据视图。例如,假设您的列是字符串类型:

var condition = yourDataTable.AsEnumerable()
                             .Any(r => r.Field<string>("GBA_Nbr_GBAccount") == "52");

var condition = yourDataView.Cast<DataRowView>()
                            .Any(rv => rv.Row.Field<string>("GBA_Nbr_GBAccount") == "52");

If the column was an integer, just change the Field<string>to Field<int>and compare against an integer, not a string

如果该列是整数,只需更改Field<string>toField<int>并与整数进行比较,而不是字符串

var condition = yourDataTable.AsEnumerable()
                             .Any(r => r.Field<int>("GBA_Nbr_GBAccount") == 52);

var condition = yourDataView.Cast<DataRowView>()
                            .Any(rv => rv.Row.Field<int>("GBA_Nbr_GBAccount") == 52);

Example application using string column:

使用字符串列的示例应用程序:

static void Main(string[] args)
{
    DataSet dataset = new DataSet();
    dataset.Tables.Add(new DataTable("table1"));
    dataset.Tables[0].Columns.Add(new DataColumn("Value", typeof(string)));
    dataset.Tables[0].Rows.Add("10");
    dataset.Tables[0].Rows.Add("52");

    DataTable table = dataset.Tables[0];
    DataView view = table.DefaultView;

    var condition1 = table.AsEnumerable().Any(r => r.Field<string>("Value") == "52");

    var condition2 = view.Cast<DataRowView>().Any(rv => rv.Row.Field<string>("Value") == "52");

    Console.WriteLine(String.Format("Result querying datatable: '{0}'. Result using dataview:'{1}'", condition1, condition2));
     Console.ReadLine();
}

If you are really using a string for the column, check for white spaces and apply a trim if needed.

如果您真的为列使用字符串,请检查空格并根据需要应用修剪。

回答by Ben

One way to check for the existence of a specific column in a dataview would be like the following:

检查数据视图中特定列是否存在的一种方法如下所示:

if (dv.Table.Columns["Name"] != null)
{
//Name column exists. Add Logic here
}

回答by Sam

Use the DataViewManager object instead it has a DataSet property associated with it and the dataset has all the normal dataset features. So, you can traverse the dataset.tables[index] object...

使用 DataViewManager 对象,而不是它具有与之关联的 DataSet 属性,并且数据集具有所有正常的数据集功能。所以,你可以遍历 dataset.tables[index] 对象...

Hope it helps

希望能帮助到你

Example: dv.DataViewManager.DataSet.Tables[0].Rows[0][1]

示例dv.DataViewManager.DataSet.Tables[0].Rows[0][1]

Thanks, Sam

谢谢,山姆