C# 我得到的是“System.Collections.Generic.List”而不是数据
我尝试在WPF应用程序中使用C制作一个自动完成的文本框,如Google Search,基本上我想做的是创建一个自动完成的文本框,它绑定到SQL数据库表。表中有2个字段的代号和名称,我的代码如下: 在XAML中: 代码隐藏:C# 我得到的是“System.Collections.Generic.List”而不是数据,c#,wpf,c#-4.0,wpf-controls,C#,Wpf,C# 4.0,Wpf Controls,我尝试在WPF应用程序中使用C制作一个自动完成的文本框,如Google Search,基本上我想做的是创建一个自动完成的文本框,它绑定到SQL数据库表。表中有2个字段的代号和名称,我的代码如下: 在XAML中: 代码隐藏: List<string> nameList; List<Product> prodList; public List<string> SelProd4Sale(string str ) {
List<string> nameList;
List<Product> prodList;
public List<string> SelProd4Sale(string str )
{
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<Product>();
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<string>()
{
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<string> autoList = new List<string>();
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; }
}
}
运行此操作时,我得到的结果是System.Collections.Generic.List,而不是数据
有人能帮我一下吗?告诉我怎么了?问题出在以下代码中:
nameList = new List<string>()
{
prodList.ToString()
};
此代码中存在问题:
nameList = new List<string>()
{
prodList.ToString()
};
@爱埃及,就是这样:使用System.Linq;2Kay:我使用了它,调试这个应用程序时,我看到名称列表中有数据,但它没有出现在列表框中,&仍然不知道为什么。@Love\u Egypt,就是这样:使用System.Linq;2Kay:我使用了它,调试这个应用程序时,我看到名称列表中有数据,但它没有出现在列表框中,我仍然不知道为什么。