I have a standard .NET DataGrid that is bound to a DataView. When the user
types into a text box above the DataGrid I issue the following code:
try
{
// We need to search for whatever the user is typing based on the type of
search ...
if (this.txtValue.Text.Length == 0)
{
this.UnHighlightRow();
return;
}
if (this.cbSrhBy.SelectedIndex.Equals(3))
return;
string lcSort = this.SrchFields[cbSrhOn.SelectedIndex].ToString().Trim(),
lcValue = this.ConvertValue(this.txtValue.Text.Trim());
DataRow[] laRows = null;
switch (this.cbSrhBy.SelectedIndex)
{
case 0: // Start of Field
if (lcValue.IndexOf("'").Equals(-1))
lcValue += "%";
else
lcValue = lcValue.Insert(lcValue.Length - 1, "%");
laRows = this.dsSearch.Tables[0].Select(lcSort + " LIKE " + lcValue,
lcSort);
break;
case 1: // Exact Phrase
laRows = this.dsSearch.Tables[0].Select(lcSort + " = " + lcValue, lcSort);
break;
case 2: // Any Part of Field
if (lcValue.IndexOf("'").Equals(-1))
lcValue = "%" + lcValue + "%";
else
lcValue = lcValue.Insert(lcValue.Length - 1, "%").Insert(1, "%");
laRows = this.dsSearch.Tables[0].Select(lcSort + " LIKE " + lcValue,
lcSort);
break;
case 3: // Filter Results
return;
}
if (laRows == null || laRows.Length.Equals(0))
return;
DataView loView = this.dsSearch.Tables[0].DefaultView;
this.UnHighlightRow();
this.dataGrid_Results.CurrentRowIndex = loView.Find(laRows[0][lcSort]);
this.HighlightRow();
this.txtValue.Focus();
}
catch (Exception loExcep)
{
SharedCode.ErrorMessage(loExcep.ToString());
}
This UnHighlightRow and HighlightRow simply issue Unselect() and Select() on
the CurrentRowIndex.
What happens is when I search on a particular value, like let's say
"Last_Name" and I type in "SMI", it will go to the Smith's but it may not
choose the first one that is shown in the DataGrid when the Find() method is
issued. Why?
Mike