C#和MS SQL Server中的组合框 [英] combo Box in C# and MS SQL Server
问题描述
您好,
我希望WPF中的组合框能够从MS SQL Server数据库中的表中获取记录。我曾尝试编码几行,但每次我都停下来。在启动数据集之后,我似乎无法获得"DisplayMember"。宾语。有没有办法让
解决这个问题。这是我的代码:评论的部分是应该显示的但它不是b
I want my combobox in WPF to fetch records from a table in MS SQL Server database. I have tried coding a few lines but each time i come to a halt. After initiating the dataset and i can't seem to get the "DisplayMember" object. Is there a way to
work around this. Here's my code: The commented parts is what is supposed to be displayed yet it is not
private void OnLoad(object sender,RoutedEventArgs e)
{
FillComboBox();
}
protected void FillComboBox()
{
SqlConnection con = new SqlConnection(cs);
DataSet ds = new DataSet();
试试
{
con.Open();&
SqlCommand cmd = new SqlCommand(" SELECT RegNumber,Make From Vehicles GROUP BY RegNumber,Make" con);
       SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
combovehicle.DisplayMemberPath =" RegNumber" ;; // DisplayMember
combovehicle.SelectedValue =" Make"; // ValueMember
combovehicle.DataContext = ds.Tables [0]; //数据源
}
catch(例外)
{
}
}
}
}
private void OnLoad(object sender, RoutedEventArgs e)
{
FillComboBox();
}
protected void FillComboBox()
{
SqlConnection con = new SqlConnection(cs);
DataSet ds = new DataSet();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT RegNumber,Make FROM Vehicles GROUP BY RegNumber,Make", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
combovehicle.DisplayMemberPath = "RegNumber"; //DisplayMember
combovehicle.SelectedValue= "Make"; //ValueMember
combovehicle.DataContext = ds.Tables[0]; //DataSource
}
catch(Exception)
{
}
}
}
}
Nickson Lukalaga
Nickson Lukalaga
推荐答案
您好Nickson,
Hi Nickson,
根据您的描述,您的问题与WPF应用程序更相关。此论坛正在讨论并提出有关Windows Form和ClickOnce的问题,我将把这个帖子移到相应的论坛:WPF论坛获取专门信息。
According to your description, your issue is more related about WPF apps. And this forum is discussing and asking questions about the Windows Form and ClickOnce, I will move this thread to corresponding forum: WPF Forum for dedicated information.
感谢您的理解。
问候,
Frankie
这篇关于C#和MS SQL Server中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!