最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# DataGridView綁定數(shù)據(jù)源的方法

 更新時間:2018年09月20日 09:55:14   作者:五維思考  
這篇文章主要為大家詳細介紹了C# DataGridView綁定數(shù)據(jù)源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

開始以前,先認識一下WinForm控件數(shù)據(jù)綁定的兩種形式,簡單數(shù)據(jù)綁定和復(fù)雜數(shù)據(jù)綁定。

1. 簡單的數(shù)據(jù)綁定

例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"]; 
} 

簡單的數(shù)據(jù)綁定是將用戶控件的某一個屬性綁定至某一個類型實例上的某一屬性。

采用如下形式進行綁定:引用控件.DataBindings.Add("控件屬性", 實例對象, "屬性名", true);

例2

從數(shù)據(jù)庫中把數(shù)據(jù)讀出來放到一個數(shù)據(jù)集中,比如List<>、DataTable,DataSet,我一般用List<>,

然后綁定數(shù)據(jù)源:

IList<student> sList=StudentDB.GetAllList();
DataGridView.DataSource=sList;

如果你沒有設(shè)置DataGridView的列,它會自動生成所有列。

2. 復(fù)雜數(shù)據(jù)綁定

復(fù)雜的數(shù)據(jù)綁定是將一個以列表為基礎(chǔ)的用戶控件(例如:ComboBox、ListBox、ErrorProvider、DataGridView等控件)綁定至一個數(shù)據(jù)對象的列表。

基本上,Windows Forms的復(fù)雜數(shù)據(jù)綁定允許綁定至支持IList接口的數(shù)據(jù)列表。此外,如果想通過一個BindingSource組件進行綁定,還可以綁定至一個支持IEnumerable接口的數(shù)據(jù)列表。

對于復(fù)雜數(shù)據(jù)綁定,常用的數(shù)據(jù)源類型有(代碼以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(); 
  }

  /// <summary> 
  /// IList接口(包括一維數(shù)組,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接口(包括一維數(shù)組,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是一個結(jié)構(gòu)類型DictionaryEntry,同樣的DictionaryEntry并不能直接賦值給Combobox的DataSource,但通過BindingSource仍然可以間接實現(xiàn)。 這是因為:

BindingSource可以作為一個強類型的數(shù)據(jù)源。其數(shù)據(jù)源的類型通過以下機制之一固定。使用 Add 方法可將某項添加到 BindingSource 組件中。

將 DataSource 屬性設(shè)置為一個列表、單個對象或類型。(這三者并不一定要實現(xiàn)IList或IListSource)

這兩種機制都創(chuàng)建一個強類型列表。BindingSource 支持由其 DataSource 和 DataMember 屬性指示的簡單數(shù)據(jù)綁定和復(fù)雜數(shù)據(jù)綁定。 

總結(jié):

根據(jù)DataSource綁定的對象的不同,可以有一下幾種簡單的綁定:

// DataSet 、DataTable
// 方式1
DataSet ds=new DataSet ();
this.dataGridView1.DataSource=ds.Table[0];
this.dataGridView1.DataSource = ds.Tables["表名"];
// 方式2
DataTable dt=new DataTable();
this.dataGridView1.DataSource=dt;

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

// 設(shè)置了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<Object>
this.dataGridVi.DataSource = new BindingList<Object>(List<Object>);

3. 實例

3.1 手動給dataGridView綁定數(shù)據(jù)源的方法

C#中手動給dataGridView綁定數(shù)據(jù)源,能夠很自由地進行操作,但展示數(shù)據(jù)并沒有C#自動添加數(shù)據(jù)源那么方便??捎袝r為了方便操作數(shù)據(jù),我們更愿意手動連接數(shù)據(jù)源,代碼如下:

conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Restaurant.mdb");//建立數(shù)據(jù)庫連接 
cmd = new OleDbCommand("select * from data", conn);//執(zhí)行數(shù)據(jù)連接 
DataSet ds = new DataSet(); 
OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
da.Fill(ds);

this.dataGridView1.DataSource = ds.Tables[0];//數(shù)據(jù)源 
this.dataGridView1.AutoGenerateColumns = false;//不自動 
conn.Close();//關(guān)閉數(shù)據(jù)庫連接 

說明:解決DataGridView綁定了數(shù)據(jù)源無法更新保存當(dāng)前行的問題

this.dataGridView.currentCell=null;//該行的作用是取消datagridview行的編輯狀態(tài) 
adapter.Update(userTable); 
 

3.2 利用泛型集合向DataGridView中添加數(shù)據(jù)

List<>泛型集合:

private void Form1_Load(object sender, EventArgs e) 
{ 
 //使用List<>泛型集合填充DataGridView 
 List<Student> students = new List<Student>(); 
 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"); 
 students.Add(hat); 
 students.Add(peter); 
 students.Add(dell); 
 students.Add(anne); 
 this.dataGridView1.DataSource = students; 
}

Dictionary<>泛型集合

private void Form1_Load(object sender, EventArgs e) 
{ 
  //使用Dictionary<>泛型集合填充DataGridView 
  Dictionary<String, Student> students = new Dictionary<String, Student>(); 
  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"); 
  students.Add(hat.StuName,hat); 
  students.Add(peter.StuName,peter); 
  students.Add(dell.StuName,dell); 
  students.Add(anne.StuName,anne); 
       //在這里必須創(chuàng)建一個BindIngSource對象,用該對象接收Dictionary<>泛型集合的對象 
  BindingSource bs = new BindingSource(); 
       //將泛型集合對象的值賦給BindingSourc對象的數(shù)據(jù)源 
  bs.DataSource = students.Values; 
  this.dataGridView1.DataSource = bs; 
}

3.3 利用SqlDataReader填充DataGridView

//使用SqlDataReader填充DataGridView 
using (SqlCommand command = new SqlCommand("select * from product", DBService.Conn)) 
{ 
   SqlDataReader dr = command.ExecuteReader(); 
   BindingSource bs = new BindingSource(); 
   bs.DataSource = dr; 
   this.dataGridView1.DataSource = bs; 
}

3.4 利用SqlDataAdapter對象向DataGridView中添加數(shù)據(jù)

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實現(xiàn)XML文檔的增刪改查功能示例

    C#實現(xiàn)XML文檔的增刪改查功能示例

    這篇文章主要介紹了C#實現(xiàn)XML文檔的增刪改查功能,結(jié)合實例形式分析了xml文檔的創(chuàng)建及C#針對xml文檔的加載及增刪改查等操作技巧,需要的朋友可以參考下
    2017-01-01
  • C#簡單連接sql數(shù)據(jù)庫的方法

    C#簡單連接sql數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#簡單連接sql數(shù)據(jù)庫的方法,涉及C#基于控制臺的數(shù)據(jù)庫連接創(chuàng)建于命令執(zhí)行相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • C#實現(xiàn)老板鍵功能的代碼

    C#實現(xiàn)老板鍵功能的代碼

    最近在做項目中遇到需要增加個老板鍵功能,找一慣的方式,開始從網(wǎng)絡(luò)下手尋找: 關(guān)鍵字類似”C# 老板鍵“,一搜,一堆又一堆,然而出來的代碼大多數(shù)都不是太合適,下面給大家分享下自己的解決方案已經(jīng)一個網(wǎng)友的解決方案,有需要的小伙伴可以參考下。
    2015-05-05
  • c# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的方法

    c# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的方法

    下面小編就為大家?guī)硪黄猚# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • C#調(diào)用??倒I(yè)相機SDK采集圖像并在Halcon窗口中顯示方式

    C#調(diào)用??倒I(yè)相機SDK采集圖像并在Halcon窗口中顯示方式

    這篇文章主要介紹了C#調(diào)用??倒I(yè)相機SDK采集圖像并在Halcon窗口中顯示方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C#讀取或設(shè)置ScrollLock狀態(tài)的方法

    C#讀取或設(shè)置ScrollLock狀態(tài)的方法

    這篇文章主要介紹了C#讀取或設(shè)置ScrollLock狀態(tài)的方法,涉及C#操作ScrollLock滾動狀態(tài)的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • 程序中兩個Double類型相加出現(xiàn)誤差的解決辦法

    程序中兩個Double類型相加出現(xiàn)誤差的解決辦法

    本篇文章介紹了,程序中兩個Double類型相加出現(xiàn)誤差的解決辦法。需要的朋友參考下
    2013-04-04
  • C#多線程系列之任務(wù)基礎(chǔ)(二)

    C#多線程系列之任務(wù)基礎(chǔ)(二)

    本文詳細講解了C#多線程的任務(wù)基礎(chǔ),文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • c#如何獲取json數(shù)組里指定參數(shù)

    c#如何獲取json數(shù)組里指定參數(shù)

    這篇文章主要介紹了c#如何獲取json數(shù)組里指定參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • C# ?的使用小結(jié)

    C# ?的使用小結(jié)

    本文介紹了C#中可空類型標記符(?)及其相關(guān)運算符的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11

最新評論

油尖旺区| 改则县| 宽甸| 奇台县| 青田县| 黄骅市| 县级市| 江川县| 筠连县| 嘉兴市| 哈尔滨市| 延川县| 龙州县| 青海省| 盐城市| 清远市| 民乐县| 宁津县| 乐陵市| 沭阳县| 盘山县| 勃利县| 陈巴尔虎旗| 六安市| 柳州市| 宜城市| 襄城县| 新巴尔虎右旗| 来凤县| 陇川县| 体育| 石台县| 饶河县| 日土县| 乐陵市| 武安市| 弥勒县| 易门县| 崇阳县| 新源县| 云浮市|