澳门至尊网站-首页

您的位置:澳门至尊网站 > 软件综合 > 绑定泛型List

绑定泛型List

2019-10-21 07:22

背景:无意间蒙受了壹当中等的主题素材,希望对风姿洒脱部分遇上的人负有助于!

一、问题

WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不出示,UI

图片 1

代码如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public delegate T BorrowReader<out T>(IDataReader reader);

    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();

        }

        private List<User> GetUsers(IDataReader reader)
        {
            var list = new List<User>();
            while (reader.Read())
            {
                list.Add(new User()
                {
                    ID = reader.GetInt32(reader.GetOrdinal("ID")),
                    UserName = reader.GetString(reader.GetOrdinal("UserName")),
                    NickName = reader.GetString(reader.GetOrdinal("NickName")),
                    Phone = reader.GetString(reader.GetOrdinal("Phone")),
                    QQ = reader.GetString(reader.GetOrdinal("QQ")),
                });
            }
            return list;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            var list = MyDb.LendReader("select * from Users where 0=0", GetUsers);
            dataGridView1.DataSource = list;
        }
    }

    public class User
    {
        public int ID;
        public string UserName;
        public string NickName;
        public string Phone;
        public string QQ;

    }

    public class MyDb
    {
        public static T LendReader<T>(string sql, BorrowReader<T> borrowReader)
        {
            using (OleDbConnection connection = CreateConnection())
            {
                connection.Open();
                OleDbCommand c = new OleDbCommand(sql, connection);
                OleDbDataReader r = c.ExecuteReader();
                return borrowReader(r);
            }
        }

        private static OleDbConnection CreateConnection()
        {
            string dbName = Path.Combine(Application.StartupPath, "MyData.mdb");
            OleDbConnection c = new OleDbConnection
            {
                ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName
            };
            return c;
        }
    }
}
二、化解情势

实在很简短,只是成都百货上千仇人大概未有思索到,因为这压根不是怎样泛型List只怕ArrayList的主题材料,

只要改代码:

    public class User
    {
        public int ID;
        public string UserName;
        public string NickName;
        public string Phone;
        public string QQ;
    }

为:

    public class User
    {
        public int ID{ get; set; }
        public string UserName { get; set; }
        public string NickName { get; set; }
        public string Phone { get; set; }
        public string QQ { get; set; } 
    }

就好了

三、轻便批注

没概念get、set的是字段,定义了正是性质了,为了安全性思念,DataGridView 的数据源绑定只好是被公开了的属性,而无权访谈字段。非常多任何控件也会有平等的场合。

本文由澳门至尊网站发布于软件综合,转载请注明出处:绑定泛型List

关键词: