Imran Azam
Imran Azam

Reputation: 89

Select Value between two column from datatable

I have a datatable. I need to fetch a certain column value based on the user input. For example, lets say the datatable has three column intpkdata,intFrom,intTo Here is my some code,

                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();

Requirement :

if intFrom/intTo data is already exist in datatable then msg should come.Pleas see the image for records

enter image description here

Upvotes: 3

Views: 1005

Answers (3)

Ajay
Ajay

Reputation: 6590

try this

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

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

see this ques. Check if value exists in dataTable?

Check if String / Record exists in DataTable

Upvotes: 2

Dipan
Dipan

Reputation: 1

Check this code....

{

   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
    }
}

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29026

Let txtFrom and txtTo be the two values, that have to be checked in the intFrom row and intTo columns respectively.(assuming that are having values 10 and 30). So the filter condition will be as follows, if the count > 0 means there are such rows existing in the table so the message can be printed.

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
}

Upvotes: 0

Related Questions