将VS.Net 2005 DataGridView控件的数据导出到二维数组

///
///  将VS.Net 2005 DataGridView控件的数据导出到二维数组。
///
///  VS.Net 2005 DataGridView控件。
///  是否要把列标题文本也导到数组中。
///   <作者> 长江支流
///   <日期>
public   string [,] ToStringArray(DataGridView dataGridView,  bool  includeColumnText)
{
    
#region  实现dot.gif
    
string [,] arrReturn  =   null ;
    
int  rowsCount  =  dataGridView.Rows.Count;
    
int  colsCount  =  dataGridView.Columns.Count;
    
if  (rowsCount  >   0 )
    {
        
// 最后一行是供输入的行时,不用读数据。
         if  (dataGridView.Rows[rowsCount  -   1 ].IsNewRow)
        {
            rowsCount
-- ;
        }
    }

    
int  i  =   0 ;
    
// 包括列标题
     if  (includeColumnText)
    {
        rowsCount
++ ;
        arrReturn 
=   new   string [rowsCount, colsCount];
        
for  (i  =   0 ; i  <  colsCount; i ++ )
        {
            arrReturn[
0 , i]  =  dataGridView.Columns[i].HeaderText;
        }
        i 
=   1 ;
    }
    
else
    {
        arrReturn 
=   new   string [rowsCount, colsCount];
    }

    
// 读取单元格数据
     int  rowIndex  =   0 ;
    
for  (; i  <  rowsCount; i ++ , rowIndex ++ )
    {
        
for  ( int  j  =   0 ; j  <  colsCount; j ++ )
        {
            arrReturn[i, j] 
=  dataGridView.Rows[rowIndex].Cells[j].Value.ToString();
        }
    }
    
return  arrReturn;
    
#endregion  实现
}

// 对两维数组的重新学习
// 二维数组转换成一维数组(C#版本)
using  System;
namespace  ConsoleApplication1
{
    
class  Program
    {
        
private   static   void  TwoarrTrancteOnearr( int  m, int  n)
        {
            
int [,] arr2;
            
int [] arr1;
            arr2 
=   new   int [m, n];
            arr1 
=   new   int [m * n];
          
            Console.WriteLine(
" output value of arr2: " );
            
for  ( int  i  =   0 ; i  <  m;  ++ i)
            {
                
for  ( int  j  =   0 ; j  <  n;  ++ j)
                {
                    arr2[i, j] 
=  i  *  n  +  j;
                    Console.Write(
" {0}  " ,arr2[i, j]);
                }
                Console.WriteLine();
            }

           Console.WriteLine(
" Output value of arr1: " );       
            
for ( int  i = 0 ;i < m * n; ++ i)
            {
                
int  r  =  i  /  n;
                
int  c  =  i  %  n;
                arr1[i] 
=  arr2[r, c];
                Console.Write(
" {0}  " ,   arr1[i]);
            }
            Console.WriteLine();
            Console.WriteLine(
" Press <enter> key exit! " );
            Console.ReadLine();
        }
        
static   void  Main( string [] args)
        {
            TwoarrTrancteOnearr(
4 3 );
        }
    }
}

string [][] fn = new   string [ 2 ][];

fn[
0 ] = new   string []{ 2 , 3 }
fn[
1 ] = new   string []{ 4 , 5 }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值