鸡蛋西红柿CC

导航

C#中DataGrid控件的基本使用

datagrid控件的作用简单点说就是将表格显示出来,用的多的是直接链接数据库文件,也可以自己见一个表格对象,链接到datagrid控件,下面是一个简单的例子

 1 //xinjiabiao
 2 DataTable dt = new DataTable();
 3             //新建列
 4             DataColumn col1 = new DataColumn("设备名", typeof(string));
 5             DataColumn col2 = new DataColumn("管理员", typeof(string));
 6             DataColumn col3 = new DataColumn("设备ID", typeof(string));
 7             DataColumn col4 = new DataColumn("所属权限", typeof(string));
 8             DataColumn col5 = new DataColumn("状态", typeof(string));
 9             DataColumn col6 = new DataColumn("购买时间", typeof(string));
10             DataColumn col7 = new DataColumn("最新维护时间", typeof(string));
11             DataColumn col8 = new DataColumn("设备管理员ID", typeof(string));
12             //添加列
13             dt.Columns.Add(col1);
14             dt.Columns.Add(col2);
15             dt.Columns.Add(col3);
16             dt.Columns.Add(col4);
17             dt.Columns.Add(col5);
18             dt.Columns.Add(col6);
19             dt.Columns.Add(col7);
20             dt.Columns.Add(col8);
21             //新建行
22             DataRow row1 = dt.NewRow();
23             //行赋值
24             row1["设备名"] = "打印机";
25             row1["管理员"] = "李居明";
26             row1["设备ID"] = "JFKSJFKSDFJK151";
27             row1["所属权限"] = "普通用户";
28             row1["状态"] = "在库";
29             row1["购买时间"] = "2012-03-20";
30             row1["最新维护时间"] = "2012-03-27";
31             row1["设备管理员ID"] = "";
32             //添加行
33             dt.Rows.Add(row1);
34             //数据绑定
35             this.dataGrid1.DataSource = dt;
36             //设置属性
37             DataGridTableStyle tablestyle = new DataGridTableStyle();
38             this.dataGrid1.TableStyles.Add(tablestyle);
39             dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 75;
40             dataGrid1.TableStyles[0].GridColumnStyles[1].Width = 75;
41             dataGrid1.TableStyles[0].GridColumnStyles[2].Width = 75;
42             dataGrid1.TableStyles[0].GridColumnStyles[3].Width = 75;
43             dataGrid1.TableStyles[0].GridColumnStyles[4].Width = 75;
44             dataGrid1.TableStyles[0].GridColumnStyles[5].Width = 120;
45             dataGrid1.TableStyles[0].GridColumnStyles[6].Width = 120;
46             dataGrid1.TableStyles[0].GridColumnStyles[6].Width = 120;

转自http://blog.sina.com.cn/s/blog_7e7b66a801012fuz.html

posted on 2014-01-06 15:38  鸡蛋西红柿CC  阅读(3135)  评论(0编辑  收藏  举报