DataTable.Rows.Find() not finding row
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.Wri开发者_如何学PythonteLine((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");
Try changing the last row to the following:
DataRow lkup = dtLkupCat.Rows.Find(new object[]
{
goal["sport"].ToString(),
goal["catcode"].ToString(),
goal["type"].ToString(),
goal["parent"].ToString(),
goal["code"].ToString()
});
Assuming your values are all strings
Per MS:
Thanks for reporting this issue. We investigated the problem and it is a bug in our code. As fixing the issue will cause a breaking change, we will need to carefully evaluate when and how to fix this issue so that it will cause minimum negative impact on our existing customers. We already have an existing Connect bug which tracks this problem (http://connect.microsoft.com/VisualStudio/feedback/details/491319/dataset-designer-generates-invalid-datarelations). I will resolve this bug as “duplicate” of Connect bug #491319, but we will keep you updated as we make progress. Meantime as a workaround, you either can set PK to null before setting the primary key, make the Constraint on the XML to match the order of the new PK on code, or cleanup Constraints and relations before setting the PK and perform Find.
https://connect.microsoft.com/VisualStudio/feedback/details/694803/datatable-rows-find-fails-to-locate-row
I had the same problem and solved it by explicitly sizing the new object. found = dtLkupCat.Rows.Find(new object[5] { goal["sport"], goal["catcode"], goal["parent"], goal["code"] });
精彩评论