第一种:
DataSet ds=new DataSet ();
this.dataGridView1.DataSource=ds.Table[0];
第二种:
DataTable dt=new DataTable();
this.dataGridView1.DataSource=dt;
第三种:
DataSet ds=new DataSet ();
this.dataGridView1.DataSource = ds.Tables["表名"];
第四种:
DataSet ds=new DataSet ();
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "表名";
第五种:
ArrayList Al = new ArrayList();
this.dataGridView1.DataSource = Al;
第六种:
Dictionary<string, string> dic = new Dictionary<string, string>();
this.dataGridView1.DataSource = dic;
第七种:
DataView dv = new DataView();
this.dataGridView1.DataSource = dv;
第八种:
this.dataGridVi.DataSource = new BindingList<Object>(List<Object>);
就知道这么多了,有些都搞忘了,应该还有很多。
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();
}
/// <summary>
/// IList接口(包括一维数组,ArrayList等)
/// </summary>
/// <returns></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;
}
/// <summary>
/// IList接口(包括一维数组,ArrayList等)
/// </summary>
/// <returns></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;
}
/// <summary>
/// IListSource接口(DataTable、DataSet等)
/// </summary>
/// <returns></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;
}
/// <summary>
/// IBindingListView接口(如BindingSource类)
/// </summary>
/// <returns></returns>
private BindingSource DataBindingByBindingSource()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
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 属性指示的简单数据绑定和复杂数据绑定。