I am putting information from a folder into a datatable. I’m putting the info into the datatable using the following code line:
dtUpgradeFileInfo.Rows.Add(nums0,nums1,nums2,test1);
It appears to be working but I’m not as familiar with datatables in C# as I am in VB. How would i search the datatable where the first column has a certain value and the third column has the highest value in case there are multiple rows with the same value in the first column. I am also unsure of how to retrieve the info once I’ve found the row I need. The type for each column is int,int,int,string respectively.
,
If by VB you mean VB.NET and not something like VB6, then the code to work with DataTables (as opposed to legacy VB recordsets) will be the same in C#. Rather, the methods you would employ, obviously the syntax will be differently because it’s C#. There’s semi-colons and brackets where you might except parentheses. But they’re using the same objects, calling the same methods.
Anyway, you could do this (C# 3.0+)
DataRow matchingRow = (from DataRow row in dtUpgradeFileInfo.Rows
where (int)row"Column1" == yourValue
orderby (int)row"Column3" descending
select row).FirstOrDefault();
if (matchingRow != null)
{
// get to work
}
And for a non-LINQ answer (any version of C#)
string filter = "Column1 = " + yourValue.ToString();
string sortOrder = "Column3 desc";
dtUpgradeFileInfo.DefaultView.RowFilter = filter;
dtUpgradeFileInfo.DefaultView.Sort = sortOrder;
DataRow myRow = null;
DataTable filteredTable = dtUpgradeFileInfo.DefaultView.ToTable();
if (filteredTable.Rows.Count > 0)
myRow = filteredTable.Rows0;
if (myRow != null)
{
// get to work
}