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();
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM