【亚洲必赢官网】DataGridView绑定泛型List的种种,ArrayList不显得的缘故和缓解

背景:无意间遇到了二当中等的题材,希望对有个别会面包车型大巴人有着援助!

背景:无意间遭逢了二个适中的难题,希望对有个别遇到的人全数扶助!

WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不展现的原委和消除,arraylist泛型

【亚洲必赢官网】DataGridView绑定泛型List的种种,ArrayList不显得的缘故和缓解。背景:无意间蒙受了二个适中的标题,希望对一些蒙受的人拥有援助!

一、DataGridView数据绑定相比(DataTable与泛型List):
当DataGridView的DataSource是DataTable的时候,DataTable的数额变动时,DataGridView的多少会跟着更改,无需重新绑定到DataGridView。
当DataGridView的DataSource是泛型List,当List的数额变动时,则需求先将DataGridView的DataSource设置为new
List<T>(),再将改成后的List<T>赋给DataGridView的DataSource。
绑定List时,注意:切莫将DataGridView的DataSource设置为Null,不然会破坏DataGridView的列结构。

一、问题

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;
        }
    }
}
一、问题

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

亚洲必赢官网 2

代码如下:

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;
        }
    }
}
一、问题

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

二、数据绑定后的丰盛删除难题:
假如要对绑定在DataGridView中的List<T>举办数量的增进删除,先要把List<T>转换来BindingList<T>,再开始展览绑定:DataGridView.DataSource=new
BindingList<T>(new
List<T>)。不然的话会生出过多意料之外的荒谬。
如:初步绑定空数据后再添加多少绑定后,却取不到DataGridView.CurrentCell属性。

贰、化解措施

实际上很不难,只是广大爱人也许未有思考到,因为那压根不是怎样泛型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; } 
    }

就好了

二、消除形式

实质上很简单,只是不少恋人恐怕未有设想到,因为那压根不是如何泛型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; } 
    }

就好了

二、化解格局

实质上很简单,只是很多恋人恐怕未有思索到,因为那压根不是怎么泛型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; } 
    }

就好了

叁、使用泛型绑定照旧能够增加删减:
IList<T> list= new List<T>();
DataGridView.DataSource=list;//DataGridView的行无法添加删除;
DataGridView.DataSource=new
BindingList<T>(list);//DataGridView的行可以增加删减(唯有同意添加行、删除行时)。

3、简单讲解

没概念get、set的是字段,定义了就是性质了,为了安全性思量,DataGridView
的数据源绑定只好是被公开了的习性,而无权访问字段。很多别的控件也有1样的景色。

亚洲必赢官网,三、简单讲解

没概念get、set的是字段,定义了便是性质了,为了安全性怀恋,DataGridView
的数据源绑定只可以是被公开了的性质,而无权访问字段。很多此外控件也有一致的意况。

3、简单讲解

没概念get、set的是字段,定义了正是性质了,为了安全性考虑,DataGridView
的数据源绑定只好是被公开了的质量,而无权访问字段。很多别样控件也有同等的情况。

DataGridView
绑定泛型List(ListT)/ArrayList不出示的缘故和缓解,arraylist泛型
背景:无意间遭逢了3个中等的题材,希望对有个别相逢的人…

网站地图xml地图