WINFORM学习手册——数据绑定
绑定数据,需要注意的是,需要把数据源封装为BindingSource对象:
一、创建一个解决方案,并设计其页面如下图
二、新建一个类UserInfo.cs,作为数据源:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataBuildingList { public class UserInfo { public UserInfo() { } public UserInfo(int id, string name, string address, string category, bool isAdmin) { this.Id = id; this.Name = name; this.Address = address; this.Category = category; this.IsAdmin = isAdmin; } public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string Category { get; set; } public bool IsAdmin { get; set; } /// <summary> /// 获取初始数据 /// </summary> /// <returns></returns> public static IList<UserInfo> GetInitData() { IList<UserInfo> users = new List<UserInfo>(); users.Add(new UserInfo(1, "万红", "重庆", "办公室", true)); users.Add(new UserInfo(2, "陈丽", "上海", "办公室", false)); users.Add(new UserInfo(3, "王菲", "香港", "办公室", false)); users.Add(new UserInfo(4, "胡军", "香港", "办公室", false)); users.Add(new UserInfo(5, "陶行知", "深圳", "财务室", true)); users.Add(new UserInfo(6, "张静", "云南", "财务室", false)); users.Add(new UserInfo(7, "田静", "码头", "财务室", false)); users.Add(new UserInfo(8, "田忌", "青海", "财务室", false)); users.Add(new UserInfo(9, "刘德华", "重庆", "财务室", false)); users.Add(new UserInfo(10, "张杰", "珠江", "财务室", false)); users.Add(new UserInfo(11, "张翰", "宝山", "财务室", false)); users.Add(new UserInfo(12, "李希", "深圳", "后勤部", true)); users.Add(new UserInfo(13, "李伟", "云南", "后勤部", false)); users.Add(new UserInfo(14, "王晶", "码头", "后勤部", false)); users.Add(new UserInfo(15, "刘芳", "青海", "后勤部", false)); users.Add(new UserInfo(16, "万威", "重庆", "后勤部", false)); users.Add(new UserInfo(17, "赵飞", "珠江", "后勤部", false)); users.Add(new UserInfo(18, "张信哲", "宝山", "后勤部", false)); return users; } } }
三、绑定数据,将下面的代码复制到MainForm.cs中:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DataBuildingList { public partial class MainForm : Form { private IList<UserInfo> UserDatas = UserInfo.GetInitData(); private BindingSource BindingUsers = new BindingSource(); public MainForm() { InitializeComponent(); //获取combo数据 IList<string> categorys = UserDatas.Select(o => o.Category).Distinct().ToList(); //绑定combo数据 this.comboCategory.ComboBox.DataSource = categorys; //获取第一个类别的名称 string category = categorys[0]; //绑定数据 DataBuilding(category); //绑定ListView数据 this.listBox1.DataSource = BindingUsers; this.listBox1.DisplayMember = "Name"; //绑定ID文本框数据 this.txtId.DataBindings.Add("Text", BindingUsers, "Id"); //绑定姓名文本框数据 this.txtName.DataBindings.Add("Text", BindingUsers, "Name"); //绑定地址文本框数据 this.txtAddress.DataBindings.Add("Text", BindingUsers, "Address"); //绑定是否责任人数据 this.boxIsAdmin.DataBindings.Add("Checked", BindingUsers, "IsAdmin"); } /// <summary> /// 将选中类别下面的数据显示在ListView中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboCategory_SelectedIndexChanged(object sender, EventArgs e) { //获取选中的类别 var category = this.comboCategory.Text; //绑定ListView数据 DataBuilding(category); } /// <summary> /// 绑定ListView数据 /// </summary> /// <param name="category"></param> private void DataBuilding(string category) { IList<UserInfo> listDatas = UserDatas.Where(o => o.Category == category).ToList(); this.BindingUsers.DataSource = listDatas; } /// <summary> /// 新增 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void newToolStripButton_Click(object sender, EventArgs e) { AddUserForm userForm = new AddUserForm(); if (userForm.ShowDialog() == DialogResult.Yes) { this.BindingUsers.Add(userForm.User); } } } }
四、绑定combo的selectedIndexchanged事件为comboCategory_SelectedIndexChanged方法:
五、创建一个新的窗体,用于新增数据:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DataBuildingList { public partial class AddUserForm : Form { /// <summary> /// 用户数据 /// </summary> public UserInfo User { get; set; } public AddUserForm() { InitializeComponent(); User = new UserInfo(); //绑定ID文本框数据 this.txtId.DataBindings.Add("Text", User, "Id"); //绑定姓名文本框数据 this.txtName.DataBindings.Add("Text", User, "Name"); //绑定地址文本框数据 this.txtAddress.DataBindings.Add("Text", User, "Address"); //绑定是否责任人数据 this.boxIsAdmin.DataBindings.Add("Checked", User, "IsAdmin"); } } }
执行: