WPF DataGrid 绑定到List集合

2021-01-04 07:27

阅读:638

标签:访问   tst   ssid   can   sel   str   int   ssi   cli   

最近尝试用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 GetAllStudent()
    {
        List studentslist = new List();
        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; }

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

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

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

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

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

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

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

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

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

    /// 
    /// 班级编号
    /// 
    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:









        
    

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

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

WPF DataGrid 绑定到List集合

标签:访问   tst   ssid   can   sel   str   int   ssi   cli   

原文地址:https://www.cnblogs.com/Use-NPOI/p/13196323.html

上一篇:C# 关键字:new

下一篇:MS-API。AJAS


评论


亲,登录后才可以留言!