1
votes

I have a SQL-database with several columns:

StartTime   EndTime   MsgType   Description
DateTim2    DateTim2  2         Some Text
DateTim2    DateTim2  2         Some Text1
DateTim2    DateTim2  1         Some Text2
DateTim2    DateTim2  3         Some Text3

The column MsgType can have a value of 1, 2 or 3. I'm making a search option box With WPF to filter on start/end time and also want to filter with Msgtype. The search option box has an apply button to execute the stored procedure. When MsgType = 0 the SP shows all returns all MsgTypes.

I have used Linq to SQL classes to get the stored procedure to work:

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>);
    }

When I manually set MsgType to one of the values it works fine, but how do i get it to work with a combobox?

1
MstType is a enum?Bruno Caceiro

1 Answers

1
votes

There are multiple ways, and in many cases you would bind the ComboBox to a datasource, and then access the value differently. However, the basic way is this:

<ComboBox Name="TheComboBox" SelectionChanged="TheComboBox_SelectionChanged">
    <ComboBoxItem Tag="1">One</ComboBoxItem>
    <ComboBoxItem Tag="2">Two</ComboBoxItem>
    <ComboBoxItem Tag="3">Three</ComboBoxItem>
</ComboBox>

And then:

int msgType = int.Parse((TheComboBox.SelectedItem as ComboBoxItem).Tag as string);

When MsgType is an enum, you'd parse differently, and if populating the ComboBox programmatically or via data binding, you'd use the correct type already instead of string parsing.