C#中DataGrid控件的基本使用

本文详细介绍了如何使用datagrid控件将设备信息以表格形式展示,并通过代码实例展示了创建数据表、添加列和行的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/l289123557/p/3507330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值