WPF DataGrid 绑定到List集合

Posted use-npoi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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集合的主要内容,如果未能解决你的问题,请参考以下文章

如何将 List<CustomObject> 绑定到 WPF DataGrid?

刷新 WPF Datagrid 未绑定到可观察集合?

将wpf datagrid导出为自定义Excel CSV文件

WPF DataGrid 将单元格绑定到具有动态列的数据模型

将按钮绑定到 DataGrid WPF 中的对象

wpf中怎么为datagrid绑定一个list 我的代码如下,为啥显示出来的全是空行?。。。 似乎数据个数是对的…