Results 1 to 5 of 5

Thread: DataGrid clears when I select a value from a ComboBox?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    DataGrid clears when I select a value from a ComboBox?

    I have a small WPF form that contains a ComboBox and a DataGrid among other controls. The CB and DataGrid are dynamically populated via SQL Server SPROC. My problem is that when I select a value from the CB, the DG will clear and I don't know why. I have NO associated Events tied to either control at this point. It appear to be acting like a WebForm when it posts. Any ideas?

    Thanks,
    Blake

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataGrid clears when I select a value from a ComboBox?

    Can you show us the XAML for the controls as well as the code used to populate them?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: DataGrid clears when I select a value from a ComboBox?

    Here is my XAML code as well as the code-behind.

    Code:
    <Window x:Class="WPFTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WPFTest"
            mc:Ignorable="d"
            FontSize="16"
            Title="My Resume"
            Height="650"
            Width="1125"
            ResizeMode="NoResize"
            WindowStartupLocation="CenterScreen">
        <Grid Background="Aquamarine">
            <StackPanel Width="450" HorizontalAlignment="Left" Orientation="Vertical">
                <TabControl x:Name="tab1" Height="590" Margin="10 10 10 10" SelectionChanged="Tab1_SelectionChanged">
                    <TabItem x:Name="tabEmployer" Header="Employers" FontSize="16" FontWeight="Bold" Width="90" HorizontalContentAlignment="Right">
                        <Grid Margin="0,0,0,-0.333">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="140" />
                                <ColumnDefinition Width="310" />
                            </Grid.ColumnDefinitions>
    
                            <Grid.RowDefinitions>
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="50" />
                                <RowDefinition Height="80" />
                            </Grid.RowDefinitions>
    
                            <Label Content="Employer:" Grid.Row="0" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="0,15,0,0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="Address 1:" Grid.Row="1" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="15 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="Address 2:" Grid.Row="2" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="15 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="Address 3:" Grid.Row="3" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="15 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="City:" Grid.Row="4" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="59 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="State:" Grid.Row="5" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="59 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="Zip Code:" Grid.Row="6" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="59 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="Country:" Grid.Row="7" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="59 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
                            <Label Content="Phone:" Grid.Row="8" Grid.Column="0" FontSize="16" FontWeight="Regular" Margin="59 15 0 0" VerticalAlignment="Center"
                                   HorizontalAlignment="Right" />
    
                            <TextBox x:Name="txtEmployer" Grid.Row="0" Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                     Width="250" Margin="15 20 0 0" GotFocus="TxtEmployer_GotFocus" />
                            <TextBox x:Name="txtAddr1" Grid.Row="1"  Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                     Width="250" Margin="15 24 0 0" GotFocus="TxtAddr1_GotFocus" />
                            <TextBox x:Name="txtAddr2" Grid.Row="2" Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                     Width="250" Margin="15 24 0 0" GotFocus="TxtAddr2_GotFocus" />
                            <TextBox x:Name="txtAddr3" Grid.Row="3" Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                     Width="250" Margin="15 24 0 0" GotFocus="TxtAddr3_GotFocus" />
                            <TextBox x:Name="txtCity" Grid.Row="4" Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                     Width="250" Margin="15 24 0 0" GotFocus="TxtCity_GotFocus" /> 
                            <ComboBox x:Name="cmbState" Grid.Row="5" Grid.Column="1"  FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                      Width="250" Margin="15 24 0 0" GotFocus="CmbState_GotFocus" ItemsSource="{Binding Source=dtStates}" />
                            <TextBox x:Name="txtZipCode" Grid.Row="6" Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                     Width="250" Margin="15 24 0 0" GotFocus="TxtZipCode_GotFocus" />
                            <ComboBox x:Name="cmbCountry"  Grid.Row="7" Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                      Width="250" Margin="15 24 0 0" GotFocus="CmbState_GotFocus" />
                            <TextBox x:Name="txtPhone" Grid.Row="8" Grid.Column="1" FontSize="16" FontWeight="Regular" VerticalAlignment="Center" HorizontalAlignment="Left"
                                     Width="250" Margin="15 24 0 0" GotFocus="TxtPhone_GotFocus" />
    
                            <Button x:Name="btnSubmit" Content="Submit" Grid.ColumnSpan="2" Grid.Row="9" Margin="0 25 0 25" Height="25" Width="150" Click="BtnSubmit_Click" />
                        </Grid>
                    </TabItem>
    
                    <TabItem x:Name="tabJobs" Header="Jobs" FontSize="16" FontWeight="Bold" Width="90" HorizontalContentAlignment="Right">
                    </TabItem>
    
                    <TabItem x:Name="tabSkills" Header="SKill Sets" FontSize="16" FontWeight="Bold" Width="90" HorizontalContentAlignment="Right">
                    </TabItem>
    
                    <TabItem x:Name="tabEducation" Header="Education" FontSize="16" FontWeight="Bold" Width="90" HorizontalContentAlignment="Right">
                    </TabItem>
                </TabControl>
    
            </StackPanel>
    
            <StackPanel Width="450" HorizontalAlignment="Right" Orientation="Vertical">
                <DataGrid x:Name="Grid1" HorizontalScrollBarVisibility="Auto" FontSize="16" Height="560" BorderBrush="DarkBlue" AutoGenerateColumns="true"
                          Margin="-222,37,15,25" MouseDoubleClick="Grid1_MouseDoubleClick" AlternatingRowBackground="#FFB3E9F5" SelectionMode="Single"
                          CanUserResizeRows="False" AutoGeneratingColumn="Grid1_AutoGeneratingColumn" AreRowDetailsFrozen="True" />
            </StackPanel>
        </Grid>
    </Window>
    The highlighted line of code is the field that causes this. However, it did the same thing on the "cmbCountry" field as well. Below is my routine that load loads the dtStates datatable.

    Code:
            private void LoadStateComboBox()
            {
                if (DatabaseConnection())
                {
                    dtStates = new DataTable();
    
                    string sqlStr = "SELECT stateCD, state_Description FROM States;";
    
                    adp = new SqlDataAdapter();
                    adp.SelectCommand = new SqlCommand(sqlStr, cnn);
                    adp.Fill(dtStates);
    
                    cmbState.ItemsSource = dtStates.Rows;
                    cmbState.DisplayMemberPath = "[state_description]";
                    cmbState.SelectedValuePath = "[stateCD]";
                }
            }
    Blake

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataGrid clears when I select a value from a ComboBox?

    I'm not 100% sure but I don't think that you should have brackets around the column names when binding. That's a SQL thing and binding has nothing to do with SQL. Also, it seems doubly silly to use them there when you're not using them in the original SQL. The point of using brackets is to ensure that the characters within are interpreted as a single identifier by the SQL interpreter. There's no need to do that in your case because there's no reserved words or special characters and, even if there were, there's still no need to do it when binding because that has nothing to do with the SQL interpreter.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: DataGrid clears when I select a value from a ComboBox?

    Unfortunately removing the brackets didn't have any effect on the outcome. Also, the way I'm binding the data to the control through XAML doesn't seem to be working correctly either. There's no data in the CB.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width