I’m trying to search a DataTable for a row I know exists.
// This is the row my search should find
DataRow goal = dtLkupCat.Rows[6];
// This finds the row correctly
string srchexpr = String.Format("sport = '{0}' and catcode = '{1}' and type = '{2}' and [parent] = '{3}' and code = '{4}'", goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"]);
DataRow[] test = dtLkupCat.Select(srchexpr);
// But if I set a PK and search for the values I know to be correct, it returns null
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
DataRow lkup = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"] });
There’s nothing special about the columns/values it’s searching. They’re all valid strings, and none are null/DBNull. What am I missing here? I can use the Select() as a workaround, obviously, but would like to know why Find() won’t work.
UPDATE: I’ve posted the xml from a subset of my lookup table if anyone wants to give this a try. You can download it from: http://www.flantech.net/files/lkup_cat2.zip
Then try running this code. It’s strange, it will find the row using different combinations of four columns, but never with all five columns.
DataTable dtLkupCat = new DataTable("lkup_cat");
dtLkupCat.ReadXml(@"lkup_cat2.xml");
// This is the row my search should find
DataRow goal = dtLkupCat.Rows[0];
// This is how I need to do the search, but it doesn't find the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
DataRow found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "sport" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["catcode"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "catcode" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["type"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "type" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
dtLkupCat.Columns["catcode"],
dtLkupCat.Columns["parent"],
dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
Per MS:
https://connect.microsoft.com/VisualStudio/feedback/details/694803/datatable-rows-find-fails-to-locate-row