0
votes

I have a 'DataTable' populated with items from a SqlServer table. There are two columns in the 'DataTable': 'id' which is the identity value for the row in the SqlServer table and 'job' which is the name of an object.

I have a database client written in 'C#' using the '.Net' framework. It contains a 'ListBox', a 'TextBox', a 'DataTable', and a 'DataView'. The 'DataView' 'DataSource' is set to the 'DataTable'. The 'ListBox' 'DataSource' is set to the 'DataView'. There are 1700 rows in the table.

My preferred method of finding the correct item is for the user to begin to type in the textbox and as with each letter the list diminishes until the user can easily spot the desired one. The user can then double click on it and some action is taken. I have used this technique with 'BindingSource' and it works well. I am having trouble using with the 'DataView'. The first two letters the user types work well. But when the third letter is typed, the 'ListBox' goes blank, even though the filter string is correct. Why does it stop working with the third letter? Any help would be appreciated.

This is a sample of the data in the field 'job': '08-CBM-01 -- Water Utilities Topo Survey' The data in the field 'id' is just an 'int' identity from SqlServer.

I've tried examining the variable strLookup and strFilter as entry is made using the keyboard and it appears these are correct.

DataTable tblSelectList;
DataView dvSelectList; 
private string strFilter = "";
private string strLookup = "";

private void tbLookup_TextChanged(object sender, EventArgs e)
{
     strLookup = tbLookup.Text.ToString();
     if (strLookup.Length > 1)
     {
           strFilter = "job Like '%" + strLookup + "%'";
           dvSelectList.RowFilter = strFilter;
     }
     else { dvSelectList.RowFilter = "job = 'z'"; }
     btnDone.Enabled = false;
}

private void SetupLbSelect()
{
     if (dvSelectList == null) { dvSelectList = new DataView(tblSelectList); }
     lbSelect.DataSource = dvSelectList;
     dvSelectList.RowFilter = "job = 'z'";
     lbSelect.DisplayMember = "job";
     lbSelect.ValueMember = "id";
}

No error messages. The ListBox begins to populate as expected with the 1st two characters and then goes blank with the third. I would expect to type 'Topo' and have all the items containing 'Topo' appear.

1
Code works. "job = 'z'" should mean the list is empty, based on your sample data. You probably want if (strLookup.Length > 0) instead of > 1. - LarsTech
Thank you LarsTech. After I read your text I deleted the procedure from the code page, along with several others and typed them in afresh. The code then worked for me as expected. I used strLookup.Length > 1 because using 0 produced way too many records. In a list of 1,700 items I thought it reasonable to ask for two characters before producing the result. I used a DataView this time instead of a BindingSource because I thought a DataView would use fewer resources. Do you know if that is true? - tjmaher
Doesn't matter. Use either tool that works for you. - LarsTech

1 Answers

0
votes

I deleted the procedure and several others from the code page and typed them in again. After that the function worked as expected. Thank you to LarsTech who told me the original code worked for him.