none
treeview problems RRS feed

  • Question

  •  

    hi .. i encouter this problem when i do the treeview.. hope expert here can help.

     

    i want to populate my treeview automatic display the first year data when i run the project.

    so i set that to be IsSelected="True"

    Code Block

    <ComboBox HorizontalAlignment="Left" Margin="5,12,0,0" x:Name="diaryYear" Style="{DynamicResource SimpleComboBox}" VerticalAlignment="Top" Width="120" Height="28" IsSynchronizedWithCurrentItem="True" SelectionChanged="diaryYear_SelectionChanged" FontSize="12" FontWeight="Bold">
            <ComboBoxItem IsSelected="True"  Style="{DynamicResource SimpleComboBoxItem}" Content="2000"/>
            <ComboBoxItem Style="{DynamicResource SimpleComboBoxItem}" Content="2001"/>
            <ComboBoxItem Style="{DynamicResource SimpleComboBoxItem}" Content="2002"/>
            <ComboBoxItem Style="{DynamicResource SimpleComboBoxItem}" Content="2003"/>
            <ComboBoxItem Style="{DynamicResource SimpleComboBoxItem}" Content="2004"/>
                   </ComboBox>

     

     

    when i run .. it has error.. Object reference not set to an instance of an object. ( error happen as highlighted in red below)..

    if i delete the IsSelected="True" everthing is fine.. why ?

     

     

    private void diaryYear_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

     

    CreateDataSet();

    Binding masterBinding = new Binding();

    masterBinding.Source = dsDiary.Tables["Life_journal_month"];

    diary_treeview.SetBinding(TreeView.ItemsSourceProperty, masterBinding);

    }

     

     

    void CreateDataSet()

    {

    string yearvalue = "";

    System.Windows.Controls.ComboBoxItem curItem = ((System.Windows.Controls.ComboBoxItem)diaryYear.SelectedItem);

     

    yearvalue = curItem.Content.ToString();

     

    //string yearvalue =diaryYear.Text;

    //MessageBox.Show("fdgdfgfd");

     

    DataTable tbl = new DataTable();

    DataTable tbl2 = new DataTable();

    SqlDataAdapter da2 = new SqlDataAdapter();

    cmd.Connection = con;

    cmd.CommandText = "Select * From Life_journal_month;"

    + "Select * From LIFEJOURNAL where year = '" + yearvalue + "';";

    da2.SelectCommand = cmd;

    da2.TableMappings.Add("Table", "Life_journal_month");

    da2.TableMappings.Add("Table1", "LIFEJOURNAL");

     

    try

    {

    con.Open();

    da2.Fill(dsDiary);

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    finally

    {

    con.Close();

    }

     

    tbl = dsDiary.Tables["Life_journal_month"];

    tbl2 = dsDiary.Tables["LIFEJOURNAL"];

     

    DataRelation subject = new DataRelation("MonthHeader",

    tbl.Columns["monthID"],

    tbl2.Columns["monthID"], true);

    dsDiary.Relations.Add(subject);

    }

     

     

    Friday, January 11, 2008 5:38 AM

Answers

  • looks the diaryYear_SelectionChanged is call before the combobox is loaded, I dont know the details of why, but I think you can handle this by the following

     

     

         private void diaryYear_SelectionChanged(Object sender, SelectionChangedEventArgs e)
            {
                ComboBoxItem curItem;
                if (diaryYear.IsLoaded)
                {
                     curItem = diaryYear.SelectedItem as ComboBoxItem;
                    string yearvalue = curItem.Content.ToString();
                }
            }

     

    hope this helps

     

    thanks

     

    regards

     

    Alex

    Friday, January 11, 2008 8:43 AM

All replies

  • looks the diaryYear_SelectionChanged is call before the combobox is loaded, I dont know the details of why, but I think you can handle this by the following

     

     

         private void diaryYear_SelectionChanged(Object sender, SelectionChangedEventArgs e)
            {
                ComboBoxItem curItem;
                if (diaryYear.IsLoaded)
                {
                     curItem = diaryYear.SelectedItem as ComboBoxItem;
                    string yearvalue = curItem.Content.ToString();
                }
            }

     

    hope this helps

     

    thanks

     

    regards

     

    Alex

    Friday, January 11, 2008 8:43 AM
  •  

    great .. thanks.. it really helps..

    but i do encouter problems when i select again the combobox value.. it will duplicate the data..

    mean whatever data that shown by default (e.g. 2008) ..when i click on 2001.. it will show the 2001 data below the 2008 data...

     

    anything like .. ClearValue of the previous one?

    Friday, January 11, 2008 12:21 PM