从数据表中选择两列之间的值

时间:2016-04-20 11:40:49

标签: c# asp.net

我有一个数据表。我需要根据用户输入获取某个列值。例如,假设数据表有三列intpkdata,intFrom,intTo 这是我的一些代码,

                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["intpkdata"] = new Random().Next(0, 99999).ToString();
                drCurrentRow["intFrom"] = txtFrom.Text;
                drCurrentRow["intTo"] = txtTo.Text;

                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["Pcidata"] = dtCurrentTable;
                gdvpciData.DataSource = dtCurrentTable;
                gdvpciData.DataBind();

要求:

如果intFrom / intTo数据已存在于datatable中,那么msg应该来了.Pleas看到图像的记录

enter image description here

3 个答案:

答案 0 :(得分:2)

试试这个

string fromValue = "some value";
string toValue = "some value";

if(dtCurrentTable.AsEnumerable().Any(row => fromValue == row.Field<string>("intFrom") && toValue == row.Field<string>("intTo")))
{
 //exists 
}

看到这个问题。 Check if value exists in dataTable?

Check if String / Record exists in DataTable

答案 1 :(得分:0)

txtFromtxtTo成为两个值,必须分别在intFrom行和intTo列中进行检查。(假设值为10且30)。所以过滤条件如下,如果计数> 0表示表中存在这样的行,那么可以打印消息。

string txtFrom = "10";
string txtTo = "30";
if (dtCurrentTable.AsEnumerable().Where(row => row.Field<string>("intFrom") == txtFrom && row.Field<string>("intTo") == txtTo).Count() > 0)
{
    // Display message here that it already exist
}

答案 2 :(得分:0)

检查此代码....

{

   String strName = "name";

    bool contains = dtCurrentTable.AsEnumerable().Any(row => strName == row.Field<String>("intFrom"));

    String strName2 = "name";

    bool contains2 = dtCurrentTable.AsEnumerable().Any(row => strName2 == row.Field<String>("intTo"));

    if (contains && contains2)
    {
        // Do your code
    }
}