码迷,mamicode.com
首页 > Windows程序 > 详细

WPF DataGrid 绑定到List集合

时间:2020-06-26 21:52:31      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:访问   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<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集合

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!