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

[转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

时间:2017-05-23 19:45:19      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:static   font   ret   sel   username   clipboard   window   ace   解决   

一、问题

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

技术分享

代码如下:

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 的数据源绑定只能是被公开了的属性,而无权访问字段。很多其他控件也有同样的情况。

[转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

标签:static   font   ret   sel   username   clipboard   window   ace   解决   

原文地址:http://www.cnblogs.com/Raywang80s/p/6895527.html

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