DataGridView自动添加行列做成矩阵卡

ExpandedBlockStart.gif 代码
 1  public  Card( string  a, string  b, string  c)
 2          {
 3              InitializeComponent();
 4               // 传进来的(ABCDEFG……)
 5              label1.Text  =  a;
 6               // 传进来的随机数字符串
 7              label2.Text  =  b;
 8              // 序列号也是传进来的随机数字符串数组
 9              label3.Text  =  c;
10          }
11           // 生成密保卡
12           public   void  createtable( string  X,  string  Axis)
13          {
14               this .dataGridView1.Columns.Clear();
15               this .dataGridView1.Rows.Clear();
16               // 声明一个字符数组tmpChars/X.ToCharArray方法将X字符串变成字符数组
17               char [] tmpChars  =  X.ToCharArray();
18               // 用“_”将tmpAxises分割
19               string [] tmpAxises  =  Axis.Split( new   string []{ " _ " }, StringSplitOptions.RemoveEmptyEntries);
20              
21               // datagridview中添加一空列,操作列名columnName,操作列名为空
22               this .dataGridView1.Columns.Add( " columnName " "" );
23               // 循环添加列,并对显示列名赋值。
24               for  ( int  i  =   0 ; i  <  tmpChars.Length; i ++ )
25              {
26                   this .dataGridView1.Columns.Add( " columnName "   +  i ,tmpChars[i].ToString());
27              }
28               // 构造8行(提前固定的)10列(X中的字符数目)的矩阵并填充数据
29               // 行循环
30               for  ( int  i  =   0 ; i  <   8 ; i ++ )
31              {
32                  DataGridViewRow dgvRow  =   new  DataGridViewRow();
33                  ArrayList list  =   new  ArrayList();
34                   // 列循环
35                   for  ( int  j  =   0 ; j  <   this .dataGridView1.Columns.Count; j ++ )
36                  {
37                       // ***********设置行单元格容器为textBox
38                      dgvRow.Cells.Add( new  DataGridViewTextBoxCell());
39                       // 添加每行的第一个单元格的值即行号
40                       if  (j  ==   0 )
41                      {
42                          list.Add(i  +   1 );
43                      }
44                       // 每一行的其他单元格值
45                       else
46                      {
47                          list.Add(tmpAxises[(j  -   1 *   8   +  i]);
48                      }
49                  }
50                   // 用设置的值填充当前行。list.ToArray()是将list由ArrayList变成object数组
51                  dgvRow.SetValues(list.ToArray());
52                   // 将当前行填充至datagridview中。
53                   this .dataGridView1.Rows.Add(dgvRow);
54              }
55               // 填充表
56          }

 

转载于:https://www.cnblogs.com/angleSJW/archive/2009/12/17/1626079.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值