none
WPF SQL中用外键存储性别,再读取赋值给COMBOBOX时,设置COMBOBOX默认值的问题。 RRS feed

  • 问题

  • 学员表中的性别存储在另一张表里,学员表中的性别字段存储的是GUID类型的,是性别表中的GUID字段。

    XAML:

                <ComboBox Name="cbo_StuGender" SelectedValue="{Binding StuGenderId}" DisplayMemberPath="CategoryName" SelectedValuePath="CategoryId" Width=" 116" Grid.Row="3" Grid.Column="1" Margin="3"></ComboBox>
    

    XAML.CS:

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                StuDAL studal = new StuDAL();
                cbo_StuGender.ItemsSource = studal.GetByCategory("学员性别");
                }

    DAL:

            /// <summary>
            /// 获取类别列表中的数据
            /// </summary>
            /// <param name="category"></param>
            /// <returns></returns>
            public StudentInfo[] GetByCategory(string category)
            {
                DataTable table = SqlHelper.ExecuteDataTable("select Id,Name from StuCategory where Category=@Category",
                    new SqlParameter("@Category", category));
                StudentInfo[] SiItems = new StudentInfo[table.Rows.Count];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = table.Rows[i];
                    StudentInfo si = new StudentInfo();
                    si.CategoryId = (Guid)row["Id"];
                    si.CategoryName = (string)row["Name"];
                    SiItems[i] = si;
                }
                return SiItems;
            }

    SQLHelper:

            public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
            {
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.Parameters.AddRange(parameters);
                        DataSet dataset = new DataSet();
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset.Tables[0];
                    }
                }
            }

    stuModel:

            /// <summary>
            /// 类别列表ID
            /// </summary>
            public Guid CategoryId
            {
                get;
                set;
            }
    
            /// <summary>
            /// 类别列表名称
            /// </summary>
            public string CategoryName
            {
                get;
                set;
            }
    
            /// <summary>
            /// 学员性别
            /// </summary>
            private Guid stugenderid;
            public Guid StuGenderId
            {
                get;
                set;
            }
    

    性别表:

    学员表:

    现在的问题在于,在XAML中设置了ComboBox Name="cbo_StuGender" SelectedValue="{Binding StuGenderId}" DisplayMemberPath="CategoryName" SelectedValuePath="CategoryId",UI启动时,COMBOBOX默认值就没法设置 了,每次打开都是什么都没选。combobox所有设置默认选中的属性我都试了,都不行。求各位老师指点。


    C#初学者!~~~

    2015年3月10日 11:47

答案

全部回复