c# datagridvew绑定数据源
来源:互联网 发布:扑克牌跑得快算法java 编辑:程序博客网 时间:2024/06/01 21:46
经过各种百度谷歌搜索,终于总结出一下几种绑定datagridview数据源的方法:
1.说明:
俗称数据操作多面手,是Microsoft Visual Studio中的一个控件。使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据。将数据绑定到 DataGridView 控件非常简单和直观,在大多数情况下,只需设置 DataSource 属性即可。在绑定到包含多个列表或表的数据源时,只需将 DataMember 属性设置为指定要绑定的列表或表的字符串即可。
DataGridView 控件支持标准 Windows 窗体数据绑定模型,因此该控件将绑定到下表所述的类的实例: 任何实现 IList 接口的类,包括一维数组。任何实现 IListSource 接口的类,例如 DataTable 和 DataSet 类。任何实现 IBindingList 接口的类,例如 BindingList 类。任何实现 IBindingListView 接口的类,例如 BindingSource 类。 DataGridView 控件支持对这些接口所返回对象的公共属性的数据绑定,如果在返回的对象上实现 ICustomTypeDescriptor 接口,则还支持对该接口所返回的属性集合的数据绑定。
通常绑定到 BindingSource 组件,并将 BindingSource 组件绑定到其他数据源或使用业务对象填充该组件。BindingSource 组件为首选数据源,因为该组件可以绑定到各种数据源,并可以自动解决许多数据绑定问题。有关更多信息,请参见 BindingSource 组件。 DataGridView 控件还可以在“取消绑定”模式下使用,无需任何基础数据存储区。有关使用未绑定的 DataGridView 控件的代码示例,请参见演练:创建未绑定的 Windows 窗体 DataGridView 控件。
DataGridView 控件具有极高的可配置性和可扩展性,它提供有大量的属性、方法和事件,可以用来对该控件的外观和行为进行自定义。当需要在 Windows 窗体应用程序中显示表格数据时,请首先考虑使用 DataGridView 控件,然后再考虑使用其他控件(例如 DataGrid)。若要以小型网格显示只读值,或者若要使用户能够编辑具有数百万条记录的表,DataGridView 控件将为您提供可以方便地进行编程以及有效地利用内存的解决方案。
2.绑定数据类型一 table
public void DataBind()
{
DBHelper.conn.Open();
string selStr = "select * from Consume";
SqlCommand cmd = new SqlCommand(selStr, DBHelper.conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "Consume");
datagridview1.DataSource = ds.Tables[0];
datagridview1.AutoGenerateColumns = false;
DBHelper.conn.Close();
}
2.绑定list
从数据库中把数据读出来放到一个数据集中,比如List<>、DataTable,DataSet,我一般用List<>,
然后绑定数据源:
IList<student> sList=StudentDB.GetAllList();
DataGridView.DataSource=sList;
如果你没有设置DataGridView的列,它会自动生成所有列
DataTable tempTable =
new
DataTable();
string
str =
@"AA|BB|CC|DD|MM@@EE|FF|GG|HH|MM@@II|JJ|KK|LL|MM@@"
;
string
[] newstr = str.Split(
new
string
[] {
"@@"
}, StringSplitOptions.RemoveEmptyEntries);
string
[] p = newstr[0].Split(
new
string
[] {
"|"
}, StringSplitOptions.RemoveEmptyEntries);
for
(
int
i = 1; i <= p.Count(); i++)
{
DataColumn dataColumn =
new
DataColumn();
dataColumn.DataType =
typeof
(
string
);
dataColumn.ColumnName =
"列名"
+ i.ToString();
tempTable.Columns.Add(dataColumn);
}
List<
string
> strsplit =
new
List<
string
>();
int
len = 0;
foreach
(
string
s
in
newstr)
{
DataRow dr = tempTable.NewRow();
strsplit = s.Split(
new
string
[] {
"|"
}, StringSplitOptions.RemoveEmptyEntries).ToList();
len = strsplit.Count;
for
(
int
i = 1; i <= len; i++)
{
dr[
"列名"
+ i.ToString()] = strsplit[i - 1];
}
tempTable.Rows.Add(dr);
}
this
.dataGridView1.DataSource = tempTable;
http://bbs.csdn.net/topics/390057119
- c# datagridvew绑定数据源
- c# combobox绑定数据源
- c#中combox绑定datatable数据源
- C#中ComboBox控件绑定数据源
- C# combobox绑定数据源(datasource)
- C# DataGridView控件绑定数据源的方式
- C# Datagridview定义好列后绑定dataset数据源
- c# winform checkedListBox动态绑定数据源
- c# dataGridView绑定string数组数据源
- C#中DataGridView控件绑定控件绑定数据源方式
- 数据源绑定
- C# ComboBox 数据源绑定后数据的添加
- C#中XML字符串做数据源绑定到控件
- c#中手动给dataGridView绑定数据源的方法
- C# Winform datagridview控件绑定数据源后列顺序混乱
- C#自定义控件绑定IList泛型、DataTable、DataSet数据源
- c# DataGridView绑定DataTable数据源并指定对应
- C# DataGridView绑定数据源的几种常见方式
- maven 教程二 深入
- 用Android实现推送解决方案
- 数学之美番外篇:平凡而又神奇的贝叶斯方法
- Android中的Intent详细讲解
- 根据身份证号,使用Java编写程序获取年龄、性别、出生日期
- c# datagridvew绑定数据源
- linux查看多核负载(经典)
- 【7gyy】XP升级Windows 7后xp系统中的文件该怎么转移呢?
- sip中update和re-invite方法的区别
- android中正确导入第三方jar包
- 题目1035:找出直系亲属
- jquery中的$(document).ready()方法和window.onload方法区别
- Android中通过Intent调用其他应用的方法
- RFC3581——SIP中的rport机制