jithesh
jithesh

Reputation: 253

How to get a column value from a temporary Datatable (.net) based on a condtion

ID  name   folderpath        files 

1    S1    D:\Official\1     1.jpg;2.jpg
2    S2    D:\Official\2    3.jpg;4.jpg

This is my Temp Datatable. From this I want to get the files(Eg : 1.jpg;2.jpg) by giving input as folderpath (Eg:D:\Official\1).

Please help.. Regards Jithesh

Upvotes: 0

Views: 118

Answers (3)

Rahul Singh
Rahul Singh

Reputation: 21825

If you know the index of column you want to fetch then you can do this:-

dt.Rows[rowIndex][columnName];

Otherwise, since DataTable is a collection of datarow elements you can iterate it using a foreach loop like this:-

string files = String.Empty;
foreach (DataRow row in dt.Rows)
{
    if(row["folderpath"].ToString() == "YourValue")
    {  
         files= row["files"].ToString();
         break;
    }
}

Edit:

If dataTable may contain duplicate rows then you can use, LINQ to query your table like this:-

var allfiles = dt.AsEnumerable().Where(x => x.Field<string>("folderPath") == searchVal)
                                .Select(x => x.Field<string>("files"));

Upvotes: 2

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

Try this

var filteredData = dt.Select("folderpath like '%Official\1%'");

This will return array of datarow if data is found.

Upvotes: 0

ttaaoossuu
ttaaoossuu

Reputation: 7894

Use DataTable.Select() method:

DataRow[] foundRows = myDataTable.Select("[folderpath] = 'D:\Official\1'");
for(int i = 0; i < foundRows.Length; i++)
{
    Console.WriteLine(foundRows[i]["files"]);
    // Or do something else with it
}

Upvotes: 1

Related Questions