New to data manipulation using DataTable
, DataColumn
, DataRow
. Why is employee
not found in the DataTable
? I was expected indexFound
to have the value "2", and when I debug the values all look correct.
// Manually create a new DataTable
DataTable dtEmployees = new DataTable("Employee");
// Add columns
dtEmployees.Columns.Add("ID", typeof(int));
dtEmployees.Columns.Add("Name", typeof(string));
dtEmployees.Columns.Add("Salary", typeof(int));
// Add rows to the table
dtEmployees.Rows.Add(52, "Sally", 29000);
dtEmployees.Rows.Add(63, "Harry", 22000);
dtEmployees.Rows.Add(72, "Alain", 23000);
dtEmployees.Rows.Add(110, "Pete", 24500);
// Look for an employee...
int index = 2;
Console.WriteLine("(index of DataRow({0}) should return {0})", index);
DataRow employee = dtEmployees.NewRow();
employee["ID"] = dtEmployees.Rows[index]["ID"];
employee["Name"] = dtEmployees.Rows[index]["Name"];
employee["Salary"] = dtEmployees.Rows[index]["Salary"];
int indexFound = dtEmployees.Rows.IndexOf(employee);
if (indexFound != -1)
Console.Write(" Employee has index {0}", indexFound); // Was expecting this...
else
Console.Write(" Employee not found in table..."); // ...but actually get this. Why??
The Microsoft documentation didn't provide any meaningful help.
question from:
https://stackoverflow.com/questions/65944204/why-is-datarow-not-found-in-datatable 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…