datagridview数据绑定[优质文档]
DataGridView数据绑定
开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据
绑定和复杂数据绑定。
1) 简单数据绑定
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString()))
{
SqlDataAdapter sda = new SqlDataAdapter("Select * From T_Class Where F_Type='Product' order by F_RootID,F_Orders", conn);
DataSet Ds = new DataSet();
sda.Fill(Ds, "T_Class");
使用DataSet绑定时,必须同时指明
DateMember
this.dataGridView1.DataSource = Ds;
this.dataGridView1.DataMember = "T_Class";
也可以直接用DataTable来绑定
this.dataGridView1.DataSource = Ds.Tables["T_Class"];
}
简单的数据绑定是将用户控件的某一个属性绑定至
某一个类型实例上的某一属性。采用如下形式进行绑定:引用控
件.DataBindings.Add("控件属性", 实例对象, "属性名", true);
2) 复杂数据绑定
复杂的数据绑定是将一个以列
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
为基础的用户控件(例如:ComboBox、ListBox、ErrorProvider、DataGridView等控件)绑定至一个数据对象的列表。
基本上,Windows Forms的复杂数据绑定允许绑定至支持IList接口的数据列表。此外,如果想通过一个BindingSource
组件进行绑定,还可以绑定至一个支持IEnumerable接口的数据列表。
对于复杂数据绑定,常用的数据源类型有(代码以DataGridView作为示例控件):
using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace DataGridViewBindingData {
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = DataBindingByList1();
this.dataGridView1.DataSource = DataBindingByList2();
this.dataGridView1.DataSource = DataBindingByDataTable();
this.dataGridView1.DataSource = DataBindingByBindingSource();
}
IList接口(包括一维数组,ArrayList等)
< summary>
< returns>
private ArrayList DataBindingByList1()
{
ArrayList Al = new ArrayList();
Al.Add(new PersonInfo("a","-1"));
Al.Add(new PersonInfo("b","-2"));
Al.Add(new PersonInfo("c","-3"));
return Al;
}
IList接口(包括一维数组,ArrayList等)
< summary>
< returns>
private ArrayList DataBindingByList2() {
ArrayList list = new ArrayList(); for (int i = 0; i < 10; i++)
{
list.Add(new
DictionaryEntry(i.ToString(),i.ToString()+"_List"));
}
return list;
}
IListSource接口(DataTable、DataSet等)
< summary>
< returns>
private DataTable DataBindingByDataTable() {
DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Name"); DataColumn dc2 = new DataColumn("Value");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
for (int i = 1; i <= 10; i++) {
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i.ToString() + "_DataTable"; dt.Rows.Add(dr);
}
return dt;
}
IBindingListView接口(如BindingSource类)
< summary>
< returns>
private BindingSource DataBindingByBindingSource()
{
Dictionary dic = new Dictionary();
for (int i = 0; i < 10; i++)
{
dic.Add(i.ToString(),i.ToString()+"_Dictionary");
}
return new BindingSource(dic,null); }
}
}
上面代码中BindingSource的Datasource是一
个结构类型DictionaryEntry,同样的DictionaryEntry并不能直接赋值给Combobox的DataSource,但通过BindingSource仍然可以间接实现。
这是因为:
BindingSource可以作为一个强类型的数据源。其数据源的类型通过以下机制之一固定:
? 使用 Add 方法可将某项添加到 BindingSource 组件中。
? 将 DataSource 属性设置为一个列表、单个对象或类型。(这三者并不一定要实现IList或IListSource)
这两种机制都创建一个强类型列表。BindingSource 支持由其 DataSource 和 DataMember 属性指示的简单数据绑定和复杂数据绑定。
C#_以SQLDataReader为数据源,实现TreeView与ListView之间的动态绑定
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection("Server=.;database=jxc;uid=sa;pwd=198910");
SqlCommand commd;
SqlDataReader dataread = null;
DataTable dt;
ArrayList arraylist = new ArrayList();
private void Form1_Load(object sender, EventArgs e)
{
commd = new SqlCommand("select * from Operater",conn);
conn.Open();
try
{
dataread = commd.ExecuteReader();
while(dataread.Read())
{
string value = dataread.GetValue(1).ToString();
获取第二列的值
TreeNode node = new TreeNode(value);
treeView1.Nodes.Add(node); 为treeView控件增加
一个节点
object[] values = new object[dataread.FieldCount];
以读取到的字段数声明一个Object数组
dataread.GetValues(values); 将所有数据放到
object数组中
arraylist.Add(values); 加入ArrayList中
}
dt = dataread.GetSchemaTable(); 获取表结构
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
listView1.Columns.Add(dr["ColumnName"].ToString(), 100,
HorizontalAlignment.Center); ListView添加列头
}
}
dataread.Close();
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void treeView1_AfterSelect(object sender,
TreeViewEventArgs e)
{
listView1.Items.Clear();
int index = treeView1.SelectedNode.Index; 选中节点的索引号
ListViewItem vi = new ListViewItem();
vi.Text = ((object[])arraylist[index])[0].ToString();
for (int i = 1; i < ((object[])arraylist[index]).Length;
i++)
{
vi.SubItems.Add(((object[])arraylist[index])[i].ToString())
;
}
listView1.Items.Add(vi);
}
}