C# DataGridView绑定数据源的几种常见方式 – wenglabs

  开端先前,率先,在WiFrm管理中辨认标明绑定的两种塑造。,简略标明绑定和复杂标明绑定。

1. 简略标明绑定

例1

using (SqlConnection conn = new SqlConnection(["connStr"].ToString())) 
{  

  SqlDataAdapter sda = new SqlDataAdapter("Select * From T_Class Where F_Type=''Product'' order by F_RootID,F_Orders", 康恩) 
  DataSet Ds = new DataSet(); 
  (DS, "T_Class"); 

  //运用标明绑时代,葡萄汁同时按生活指数调整标明文件。 
  this.dataGridView1.DataSource = Ds; 
  this.dataGridView1.DataMember = "T_Class"; 

  //它也可以连续的与DATABLE绑定。 
  this.dataGridView1.DataSource = Ds.Tables["T_Class"]; 
} 

  简略标明绑定是将用户管理的某本人属性绑定至某本人典型案件上的属性

  按以下体式绑定:提及把持。(把持性, 案件不赞成, “属性名”, 真的)

 例2

  从标明库中读取标明并将其放入标明集中。,拿 … 来说,列表、DataTable,DataSet,我通常运用列表,
  与绑定标明源:

IList sList=();

  即使你不设置DataGridView的柱,它会自动的产生一切列。

2. 复杂标明绑定


  复杂标明绑定是由于列表的用户管理(拿 … 来说):ComboBox、ListBox、ErrorProvider、DataGridView管理和否则)绑定到本人标明不赞成的列表。。 


  主要,Windows 形状的复杂标明绑定容许绑定到证实的标明列表。。以及,即使你想绑定经过BindingSource隶属的小组织,它也可以绑定到证实I可详表使联系的标明列表。。 


  在流行中的复杂标明绑定,经用的标明源典型是(法典运用DATGRIDVIEW作为示例管理)。。

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 
{ 
publicpartialclass 塑造1 : Form 
{ 

public 塑造1() {   InitializeComponent(); } privatevoid button1_Click(object sender, EventArgs e) {   //this.dataGridView1.DataSource = DataBindingByList1();   //this.dataGridView1.DataSource = DataBindingByList2();   //this.dataGridView1.DataSource = DataBindingByDataTable();   this.dataGridView1.DataSource = DataBindingByBindingSource(); } ////// ILIST使联系(包含一维装饰),ArrayList等) //////private ArrayList DataBindingByList1() {   ArrayList Al = new ArrayList();   (new PersonInfo("a","-1"));   (new PersonInfo("b","-2"));   (new PersonInfo("c","-3"));   return Al; } ////// ILIST使联系(包含一维装饰),ArrayList等) //////private ArrayList DataBindingByList2() {   ArrayList list = new ArrayList();   for (int i = 0; i < 10; i++)   {     (new DictionaryEntry((),()+"_List"));   }   return list; } ////// IListSource使联系(DataTable、标明集等。 //////private DataTable DataBindingByDataTable() {   DataTable dt = new DataTable();   DataColumn dc1 = new DataColumn("Name");   DataColumn dc2 = new DataColumn("Value");   (DC1)   (DC2)   for (int i = 1; i <= 10; i++)   {     DataRow dr = ();     dr[0] = i;     dr[1] = () + "_DataTable";     (DR)   }   return dt; } ////// IBindingListView使联系(如BindingSource类) //////private BindingSource DataBindingByBindingSource() {   Dictionary<string, string> dic = new Dictionary<string, string>();   for (int i = 0; i < 10; i++)   {     ((),()+"_Dictionary");   }   returnnew BindingSource(dic,null);
}
} }

  下面法典中BindingSource的Datasource是本人构造典型DictionaryEntry,同一的DictionaryEntry并不克不及连续的诊断给Combobox的DataSource,但它依然可以经过BindingSource二手的完成。。 这是因: 
BDIN源可以用作强典型的标明源。。标明源的典型经过以下机制经过常作复合词。运用 Add 办法可以将某项添加到
BindingSource 隶属的小组织中。 
将 DataSource
属性设置为列表、单一不赞成或典型。(三不一定要完成IList或IListSource)
这两种机制都引起了本人强典型列表。。BindingSource
证实的 DataSource 和 DataMember 属性表现的简略标明绑定和复杂标明绑定。。

总结:

形形色色的的不赞成绑定到标明源,有各自的简略的绑定

// DataSet 、DataTable

// 方法1
DataSet ds=new DataSet ();
[0];
this.dataGridView1.DataSource = ds.Tables["表名"];

//  方法2
DataTable dt=new DataTable();
this.dataGridView1.DataSource=dt;

// DataView
DataView dv = new DataView();
this.dataGridView1.DataSource = dv;

// 设置DataMember
DataSet ds=new DataSet ();
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "表名";

// ArrayList
ArrayList Al = new ArrayList();
this.dataGridView1.DataSource = Al;

// dic
Dictionary<string, string> dic = new Dictionary<string, string>();
this.dataGridView1.DataSource = dic;

// List
this.dataGridVi.DataSource = new BindingList(列表));

3. 案件

本人标明源人工伪造绑定DataGridView的办法

在C中人工伪造绑定DATAGIDVIEW到标明源,能自在伪造,但显示标明不如C自动的添加标明源这么近便的。。偶然标明伪造近便的,我们的更爱人人工伪造衔接标明源,法典如次:

conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Restaurant.mdb");//使成为标明库衔接  
cmd = new OleDbCommand("select * from data", 康恩)//器械标明衔接  
DataSet ds = new DataSet();  
OleDbDataAdapter da = new OleDbDataAdapter(cmd);  
(DS)
this.dataGridView1.DataSource = ds.Tables[0];//标明源 this.dataGridView1.AutoGenerateColumns = false;//不自动的 ();//停业标明库衔接

阐明:

receive 接纳DATAGIDVIEW绑定标明源无法替换同意当至将来PRO

this.dataGridView.currentCell=null;//该行的功能是约datagridview行的汇编者房地产  
(userTable);  

运用泛型集将标明添加到DATAGIDVIEW

列表< >泛型集:

privatevoid 塑造1_Load(object sender, EventArgs e)  
{  
    //运用列表< >泛型集装填物DataGridView  
    List students = new List();  
    Student hat = new Student("Hathaway", "12", "Male");  
    Student peter = new Student("Peter","14","Male");  
    Student dell = new Student("Dell","16","Male");  
    Student anne = new Student("Anne","19","Female");  
    (帽子)  
    (彼得)  
    (小谷)  
    (安妮)  
    this.dataGridView1.DataSource = students;  
}

字典>流通时间集

privatevoid 塑造1_Load(object sender, EventArgs e)  
{  
    //运用字典>流通时间集装填物DataGridView  
    Dictionary students = new Dictionary();  
    Student hat = new Student("Hathaway", "12", "Male");  
    Student peter = new Student("Peter","14","Male");  
    Student dell = new Student("Dell","16","Male");  
    Student anne = new Student("Anne","19","Female");  
    ();  
    ();  
    ();  
    ();  
         //喂葡萄汁引起本人绑定源不赞成,用该不赞成接纳字典>流通时间集的不赞成  
    BindingSource bs = new BindingSource();  
         //将泛型集中不赞成的值赋给  
    bs.DataSource = students.Values;  
    this.dataGridView1.DataSource = bs;  
}

应用SqlDataReader装填物DataGridView

//运用SqlDataReader装填物DataGridView  using (SqlCommand command = new SqlCommand("select * from product", ))  
{  
      SqlDataReader dr = ();  
      BindingSource bs = new BindingSource();  
      bs.DataSource = dr;  
      this.dataGridView1.DataSource = bs;  
}

应用SqlDataAdapter不赞成向DataGridView中添加标明

using (SqlDataAdapter da = new SqlDataAdapter("select * from Product", ))  
{  
      DataSet ds = new DataSet();  
      (DS)  
      this.dataGridView1.DataSource = ds.Tables[0];  
}

提及文字

1. 小白,datagridview绑定标明源的几种平民方法。

2. aspnet2005, DATGRIDVIEW管理到何种地步绑定C中标明库的标明

3. officialxiofe,  DATAGIDVIEW管理在C中绑定标明源的方法有几种

4. lllljz, c#中本人标明源人工伪造绑定DataGridView的办法.

发表评论

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