WPF DataGrid 绑定到List集合

最近尝试用WPF重新编写之前用WinForm编写的应用程序,在使用中,需要从数据库查询到一系列数据库,在前台DataGrid里面显示出来。
后台 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using StudentsModules;
using ELP;

namespace StudentDAL
{
public class StudentServices
{
private ErrorLogProcessing errorLogp = new ErrorLogProcessing();
private SqlHelper helper = new SqlHelper();

    public void StudentAdd(Students students)
    {
        try
        {
            string str = "insert into Students(StudentName,Gender,Birthday,StudentIdNo,CarNo,StuImage,Age,PhoneNumber,studentAddress,ClassId)" +
              " values (‘{0}‘,‘{1}‘,{2},‘{3}‘,‘{4}‘,‘{5}‘,{6},‘{7}‘,‘{8}‘,{9})";
            str = string.Format(str, students.StudentName, students.Gender, students.Birthday, students.StudentIdNo, students.CarNo, students.StuImage, students.Age,
             students.PhoneNumber, students.StudentAddress, students.ClassId);

              int a= helper.UpdateData(str);

         上面有直接拼接SQL语句的,使用参数化的SQL最好还是使用SqlParameter封装参数,然后传递给数据库访问方法

        }
        catch(Exception ex)
        {
            errorLogp.WriteErrorLog(ex);
        }


    }

    public List<StudentExt> GetAllStudent()
    {
        List<StudentExt> studentslist = new List<StudentExt>();
        try
        {
          string str = "select Students.StudentId,Students.StudentName,Students.Age,StudentClass.ClassName," +
                "Students.Gender,Students." + "StudentIdNo,Students.PhoneNumber,StudentAddress " +
              "from Students inner join StudentClass on(Students.ClassId = StudentClass.ClassId) ";

           SqlDataReader dataReader=  helper.DataReaderSingle(str);

           
             
            while(dataReader.Read())
            {
                studentslist.Add(new StudentExt()
                {
                    StudentId = Convert.ToInt32(dataReader["StudentId"]),
                    StudentName = dataReader["StudentName"].ToString(),
                    Age= Convert.ToInt32(dataReader["Age"]),
                    ClassName= dataReader["ClassName"].ToString(),
                    Gender= dataReader["Gender"].ToString(),
                    StudentIdNo = dataReader["StudentIdNo"].ToString(),
                    PhoneNumber= dataReader["PhoneNumber"].ToString(),
                    StudentAddress = dataReader["StudentAddress"].ToString()

                }); 
              
            }
            dataReader.Close();


        }
        catch (Exception ex)
        {
            errorLogp.WriteErrorLog(ex);
        }

        return studentslist;
    
    }


}

}

学员实体类:
using System;
using System.Collections.Generic;
using System.Text;

namespace StudentsModules
{
///


/// 学员实体类
///

public class Students
{
///
/// 学员编号
///

public int StudentId { get; set; }

    /// <summary>
    /// 学员姓名
    /// </summary>
    public string StudentName { get; set; }

    /// <summary>
    /// 学员性别
    /// </summary>
    public string Gender { get; set; }
    
    

     /// <summary>
     /// 学员生日
     /// </summary>
    public DateTime Birthday { get; set; }

    /// <summary>
    /// 学员身份证编号
    /// </summary>
    public string StudentIdNo { get; set; }

    /// <summary>
    /// 学员证编号 
    /// </summary>
    public string CarNo { get; set; }

    /// <summary>
    /// 学员头像
    /// </summary>
    public  string StuImage { get; set; }

    /// <summary>
    ///  学员年龄
    /// </summary>
    public int Age { get; set; }

    /// <summary>
    /// 学员电话号码
    /// </summary>
    public string PhoneNumber { get; set; }

    /// <summary>
    /// 学员住址
    /// </summary>
    public string StudentAddress { get; set; }

    /// <summary>
    /// 班级编号
    /// </summary>
    public int ClassId { get; set; }
   



}

}
学员实体扩展类:

using System;
using System.Collections.Generic;
using System.Text;

namespace StudentsModules
{
public class StudentExt:Students
{
public string ClassName { get; set; }
}
}
前台XAML:

<DataGrid.Columns>







        </DataGrid.Columns>
    </DataGrid>

后台设置数据源:
private void BtnLoaddata_Click(object sender, RoutedEventArgs e)
{

        this.DGview.ItemsSource = GetStudentServices.GetAllStudent();
    }

WPF DataGrid 绑定到List集合

上一篇:真-API.DALBLL.AJAS


下一篇:win32单例执行