一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - C# DataGridView綁定數據源的方法

C# DataGridView綁定數據源的方法

2022-03-01 14:18五維思考 C#

這篇文章主要為大家詳細介紹了C# DataGridView綁定數據源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

1. 簡單的數據綁定

例1

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

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

然后綁定數據源:

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

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

2. 復雜數據綁定

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

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

對于復雜數據綁定,常用的數據源類型有(代碼以DataGridView作為示例控件)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 屬性指示的簡單數據綁定和復雜數據綁定。 

總結:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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;
 
// 設置了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綁定數據源的方法

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

?
1
2
3
4
5
6
7
8
9
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Restaurant.mdb");//建立數據庫連接
cmd = new OleDbCommand("select * from data", conn);//執行數據連接
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
 
this.dataGridView1.DataSource = ds.Tables[0];//數據源
this.dataGridView1.AutoGenerateColumns = false;//不自動
conn.Close();//關閉數據庫連接

說明:解決DataGridView綁定了數據源無法更新保存當前行的問題

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

3.2 利用泛型集合向DataGridView中添加數據

List<>泛型集合:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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<>泛型集合

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
       //在這里必須創建一個BindIngSource對象,用該對象接收Dictionary<>泛型集合的對象
  BindingSource bs = new BindingSource();
       //將泛型集合對象的值賦給BindingSourc對象的數據源
  bs.DataSource = students.Values;
  this.dataGridView1.DataSource = bs;
}

3.3 利用SqlDataReader填充DataGridView

?
1
2
3
4
5
6
7
8
//使用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中添加數據

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/zhaoshujie/p/9679002.html

延伸 · 閱讀

精彩推薦
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
主站蜘蛛池模板: ysl千人千色t9t9t9 | 四虎影院久久久 | 男女肉粗暴进来下面好紧 | 国产精品日韩在线观看 | 手机看片一区二区 | zol中关村在线官网 yy6080欧美三级理论 | 亚洲无限 | 美女脱得一二净无内裤全身的照片 | 91国内精品久久久久怡红院 | sao虎在线精品永久在线 | 免费看打屁股视频的软件 | 国产成人影院 | 亚洲欧美日韩国产一区图片 | 欧美一区二区福利视频 | 久久久久久久尹人综合网亚洲 | 国产老村长足疗店对白 | 日本mv精品中文字幕 | 美女69xx | 日本一区二区不卡久久入口 | 国产精品成人一区二区 | 国产成人精品三级在线 | 成人夜视频寂寞在线观看 | 亚洲激情网站 | 亚洲第一国产 | 国产老太婆hd老头 | 四虎在线成人免费网站 | les在宿舍吃她奶 | 男人天堂网av | 我的漂亮朋友在线观看全集免费 | 国产精品四虎在线观看免费 | 天天色天天舔 | 二次元美女挤奶漫画 | 久久久久久久国产精品视频 | 性做久久久久免费观看 | 欧美第一视频 | 国产精品视频人人做人人爱 | 免费国产福利 | www91在线观看 | 羲义嫁密着中出交尾gvg794 | 久久99精品涩AV毛片观看 | 欧美午夜网站 |