I tried to make a autoComplete textbox like google Search with C# in a WPF application, basically what I want to do is have a autocomplete textbox which is bound to a sql database table. the table has 2 fields(Barcode and Name),my code as below:
In XMAL :
Code Behind:
List
List
public
List
{
string constr = "Data Source=.;Initial Catalog=AgamistaStore;User ID=emad2012;Password=emad_2012";
SqlConnection SqlCon = new SqlConnection(constr);
SqlCommand SqlCmdProds = new SqlCommand();
SqlCmdProds.Connection = SqlCon;
SqlCmdProds.CommandType = CommandType.Text;
SqlCmdProds.CommandText = "SELECT dbo.ProductsTbl.ProductID,ProductsTbl.ProductBarcode," +
"dbo.ProductsTbl.ProductName, dbo.ProductsTbl.SalePrice FROM dbo.ProductsTbl ";
SqlCon.Open();
SqlDataAdapter dapProds = new SqlDataAdapter();
dapProds.SelectCommand = SqlCmdProds;
DataSet dsProds = new DataSet();
dapProds.Fill(dsProds);
SqlCon.Close();
prodList =
new List
for (int i = 0; i < dsProds.Tables[0].Rows.Count; i++)
{
prodList.Add(new Product
(dsProds.Tables[0].Rows[i]["ProductBarcode"].ToString(),
dsProds.Tables[0].Rows[i]["ProductName"].ToString());
}
dsProds = null;
nameList =
new List
{
prodList.ToString()
};
return nameList;
}
public Window2()
{
InitializeComponent();
SelProd4Sale(txtCAuto.Text);
txtCAuto.TextChanged += new TextChangedEventHandler(txtAuto_TextChanged);
}
#region TextBox-TextChanged-txtAuto
private void txtAuto_TextChanged(object sender, TextChangedEventArgs e)
{
string typedString = txtCAuto.Text.ToUpper();
List
autoList.Clear();
foreach (string item in nameList)
{
if (!string.IsNullOrEmpty(txtCAuto.Text))
{
if (item.StartsWith(typedString))
{
autoList.Add(item);
}
}
}
if (autoList.Count > 0)
{
lbSuggestion.ItemsSource = autoList;
lbSuggestion.Visibility = Visibility.Visible;
}
else if (txtCAuto.Text.Equals(""))
{
lbSuggestion.Visibility = Visibility.Collapsed;
lbSuggestion.ItemsSource = null;
}
else
{
lbSuggestion.Visibility = Visibility.Collapsed;
lbSuggestion.ItemsSource = null;
}
}
#endregion
#region ListBox-SelectionChanged-lbSuggestion
private void lbSuggestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lbSuggestion.ItemsSource != null)
{
lbSuggestion.Visibility = Visibility.Collapsed;
txtCAuto.TextChanged -= new TextChangedEventHandler(txtAuto_TextChanged);
if (lbSuggestion.SelectedIndex != -1)
{
txtCAuto.Text = lbSuggestion.SelectedItem.ToString();
}
txtCAuto.TextChanged += new TextChangedEventHandler(txtAuto_TextChanged);
}
}
#endregion
}
class Product
{
private string _ProductBarcode = "";
private string _ProductName = "";
public Product(string prodName,string prodBarcode)
{
this._ProductBarcode = prodBarcode;
this._ProductName = prodName;
}
public string ProductBarcode
{
get { return _ProductBarcode; }
set { _ProductBarcode = value; }
}
public string ProductName
{
get { return _ProductName; }
set { _ProductName = value; }
}
}
When I run this I got "System.Collections.Generic.List" as result instead of data.
Can somebody help me please & tell me what 's wrong?
Thanks.
Santhosh Kumar JayaramanPosted Dec 16, 2012, 8:53 AM
I dont have sql server, so i added data into list in code behind and its working for me
DoudyPosted Dec 15, 2012, 8:23 AM
Santhosh Kumar JayaramanPosted Dec 14, 2012, 12:28 AM
nameList = new List()
{
prodList.ToString()
};
Here you are adding an object into name list and only the first.
Instead of the above code, write,
nameList = new List();
nameList=prodList.Select(m=>m.ProductName).ToList();
So now the list will have all productnames.
This will work.
I also found out another issue that if you type any character apart from first character as small case, then autosuggest is not working. I have found out solution for that also
Instead of the below line
if (item.StartsWith(typedString))
try this
if (item.ToUpper().StartsWith(typedString))
Its working fine for me now.
Please let me know if it didnt work