绑定泛型List,DataGridView绑定泛型List的各种

背景:无意间遭逢了2个非常的小相当的大的题材,希望对有个别境遇的人全数帮忙!

背景:无意间遭受了3个适中的标题,希望对有个别赶上的人有所帮忙!

WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不展现的缘由和平解决决,arraylist泛型

背景:无意间遇到了叁个适中的标题,希望对部分赶上的人持有支持!

1、DataGridView数据绑定相比较(DataTable与泛型List):
当DataGridView的DataSource是DataTable的时候,DataTable的数码变动时,DataGridView的数额会跟着改变,无需重新绑定到DataGridView。
当DataGridView的DataSource是泛型List,当List的数目变动时,则要求先将DataGridView的DataSource设置为new
List<T>(),再将转移后的List<T>赋给DataGridView的DataSource。
美高梅开户网址 ,绑定泛型List,DataGridView绑定泛型List的各种。绑定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; } 
    }

就好了

2、消除情势

实则非常的粗略,只是很多仇人只怕未有设想到,因为那压根不是哪些泛型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; } 
    }

就好了

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

3、简单讲解

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

三、简单讲解

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

3、简单讲解

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

DataGridView
绑定泛型List(ListT)/ArrayList不显得的原故和化解,arraylist泛型
背景:无意间碰到了一个相当的小一点都不小的题材,希望对部分会合的人…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图