zoukankan      html  css  js  c++  java
  • 【转】DataGridView绑定数据源的几种方式

    第一种:
    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>);


    非绑定模式 (ref:http://blog.csdn.net/yudandan10/article/details/9618763)

    所谓的非绑定模式就是dataGridView控件显示的数据不是来自于绑定的数据源,而是可以通过代码手动将数据填充到DataGridView控件中,这样就为DataGridView控件增加了很大的灵活性

    在此我们先来了解一下DataGridView控件有多种类型的列 ,而这些类型都是间接的或直接的继承了DataGridViewColumns()    ,下面是我们能够长用的几种类型

    说明

    DataGridViewTextBoxColumn

    与基于文本的值一起使用,在绑定到数字和字符串类型的值时自动生成

    DataGridViewCheckBoxColumn

    booleancheckState值一起使用,在绑定到这些类型的值时自动生成

    DataGridViewImageColumn

    用于显示图像,在绑定到字节数组、Image对象或Icon对象自动生成

    DataGridViewButtonColumn

    用于在单元格中显示按钮,不会在绑定时自动生成,通常用来做未绑定列

    DataGridViewComboBoxColumn

    用户在单元格中显示下拉列表,不会在绑定时自动生成,通常收到进行数据绑定

    DataGridViewLinkColumn

    用于在单元格中显示超链接,不会在绑定时自动生成,通常需要进行手动绑定数据

    了解了这些看下面的例子 

     

    1.  //'创建一个显示textBox的列()  
       Dim col1 As DataGridViewTextBoxColumn =New DataGridViewTextBoxColumn();  
       col1.Name = "Name"  ;
      col1.HeaderText = "姓名" ;//'设置标题中显示的文本  
        
      Dim col3 As DataGridViewTextBoxColumn = NewDataGridViewTextBoxColumn();  
       col3.Name = "sex"  ;
      col3.HeaderText = "性别" ; 
        
      //'将新建的列添加到控件中  
       DataGridView1.Columns.Add(col1) ; 
       DataGridView1.Columns.Add(col3)  ;
        
       //'添加行  
       //'创建新行   
       Dim row As DataGridViewRow = NewDataGridViewRow();  
       row.CreateCells(DataGridView1)  ;
       //'设置单元格的值  
      row.Cells(0).Value = "张三"  ;
      row.Cells(1).Value = ""  ;
       DataGridView1.Rows.Add(row)  ;
       //'添加第二行  
      im row1 As String() = {"李四", ""}  ;
       DataGridView1.Rows.Add(row1);

    
    

     

    另外对于添加标题列也可以不用代码添加,而是通过手动添加列  操作步骤如下

     

    1,点击DataGridView控件  就可以看到一个小三角,点击小三角弹出菜单  选择编辑列如下所示

    编辑列:

    同时也可以修改列的属性

     

  • 相关阅读:
    向对象数组中添加新的属性 Jim
    vuecli3.0 postcsspxtoviewport将px转化为vwvh适配/Web 端屏幕适配方案 Jim
    js深拷贝与浅拷贝 Jim
    行业死亡案例汇总(客观记录不做评价)
    wins和linux 系统不同编码格式导致的.py执行问题: bad interpreter: No such or file directory
    Pyhon之类学习1
    How to handle error In $.get()
    sql 修改列名及表名
    程序设计类网站
    数据类型
  • 原文地址:https://www.cnblogs.com/leenice/p/5159365.html
Copyright © 2011-2022 走看看