在 WPF 中使用 ComboBox 搜索 SQL-Database
Searching SQL-Database with a ComboBox in WPF
我有一个包含多个列的 SQL 数据库:
StartTime EndTime MsgType Description
DateTim2 DateTim2 2 Some Text
DateTim2 DateTim2 2 Some Text1
DateTim2 DateTim2 1 Some Text2
DateTim2 DateTim2 3 Some Text3
列 MsgType
的值可以是 1、2 或 3。我正在使用 WPF 创建一个搜索选项框以根据 start/end time
进行筛选,并且还想使用 [=15] 进行筛选=].搜索选项框有一个应用按钮来执行存储过程。 When MsgType = 0
SP 显示所有 returns 所有 MsgType。
我已经使用 Linq SQL 类 让存储过程工作:
LinqDataContext dc = new LinqDataContext(Properties.Settings.Default.ConnectionString);
private void ApplyButton_Click(object sender, RoutedEventArgs e)
{
spGetHistoric.ItemsSource = dc.spMessages_GetHistoric(1033, BeginDate.SelectedDate, StopDate.SelectedDate, 0 <this is the msgType>);
}
当我手动将 MsgType 设置为其中一个值时它工作正常,但我如何让它与组合框一起工作?
有多种方法,在许多情况下,您会将 ComboBox 绑定到数据源,然后以不同方式访问该值。然而,基本的方法是这样的:
<ComboBox Name="TheComboBox" SelectionChanged="TheComboBox_SelectionChanged">
<ComboBoxItem Tag="1">One</ComboBoxItem>
<ComboBoxItem Tag="2">Two</ComboBoxItem>
<ComboBoxItem Tag="3">Three</ComboBoxItem>
</ComboBox>
然后:
int msgType = int.Parse((TheComboBox.SelectedItem as ComboBoxItem).Tag as string);
当 MsgType 是一个枚举时,您将进行不同的解析,如果以编程方式或通过数据绑定填充 ComboBox,您将使用正确的类型而不是字符串解析。
我有一个包含多个列的 SQL 数据库:
StartTime EndTime MsgType Description
DateTim2 DateTim2 2 Some Text
DateTim2 DateTim2 2 Some Text1
DateTim2 DateTim2 1 Some Text2
DateTim2 DateTim2 3 Some Text3
列 MsgType
的值可以是 1、2 或 3。我正在使用 WPF 创建一个搜索选项框以根据 start/end time
进行筛选,并且还想使用 [=15] 进行筛选=].搜索选项框有一个应用按钮来执行存储过程。 When MsgType = 0
SP 显示所有 returns 所有 MsgType。
我已经使用 Linq SQL 类 让存储过程工作:
LinqDataContext dc = new LinqDataContext(Properties.Settings.Default.ConnectionString);
private void ApplyButton_Click(object sender, RoutedEventArgs e)
{
spGetHistoric.ItemsSource = dc.spMessages_GetHistoric(1033, BeginDate.SelectedDate, StopDate.SelectedDate, 0 <this is the msgType>);
}
当我手动将 MsgType 设置为其中一个值时它工作正常,但我如何让它与组合框一起工作?
有多种方法,在许多情况下,您会将 ComboBox 绑定到数据源,然后以不同方式访问该值。然而,基本的方法是这样的:
<ComboBox Name="TheComboBox" SelectionChanged="TheComboBox_SelectionChanged">
<ComboBoxItem Tag="1">One</ComboBoxItem>
<ComboBoxItem Tag="2">Two</ComboBoxItem>
<ComboBoxItem Tag="3">Three</ComboBoxItem>
</ComboBox>
然后:
int msgType = int.Parse((TheComboBox.SelectedItem as ComboBoxItem).Tag as string);
当 MsgType 是一个枚举时,您将进行不同的解析,如果以编程方式或通过数据绑定填充 ComboBox,您将使用正确的类型而不是字符串解析。