将数组值放入DataGridView

时间:2013-10-23 19:59:39

标签: c# arrays datagridview

我一直试图弄清楚如何将数组值添加到DataGridView中。就像现在一样,当我真的在寻找类似的东西时,所有的值都被添加到一个列中:

Col1  Col2
Name  Number
Adam  3
Ryan  4

现在它们显示为

Col1  Col2
Name
Number
Adam
3
Ryan
4

通过读取Excel电子表格的值来填充数组。我搜索并尝试了不同的方法,但没有成功。这是我目前正在使用的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp ;
        Excel.Workbook xlWorkBook ;
        Excel.Worksheet xlWorkSheet ;
        Excel.Range range ;

        //string str;
        int rCnt = 0;
        int cCnt = 0;

        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Open("C:\\Text Files\\test1.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        range = xlWorkSheet.UsedRange;

        string[,] excelArray = new string[range.Rows.Count, range.Columns.Count];
        for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
        {
            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            {
                excelArray[rCnt - 1, cCnt - 1] = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2.ToString();
                //excelArray[rCnt - 1,cCnt - 1] = str;
                //listBox1.Items.Add("location " + rCnt + "," + cCnt + " value " + excelArray[rCnt - 1, cCnt - 1] + excelArray[rCnt, cCnt - 1]);

                this.dataGridView1.Rows.Add(excelArray[rCnt -1, cCnt - 1]);
                //dataGridView1.DataSource = excelArray[rCnt - 1, cCnt - 1];
            }
        }

        xlWorkBook.Close(true, null, null);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

}

}

非常感谢任何正确方向的推动。

1 个答案:

答案 0 :(得分:1)

为了避免协调问题(并保持当前的循环),我认为最好的解决方案是将行创建和细胞群部分分开。也就是说,首先添加所有行(我知道您已将所有必需的列添加到DataGridView1):

for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
    dataGridView1.Rows.Add();
}

这样您就可以继续使用两个嵌套循环并依靠列/行索引填充相应的单元格:

for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
    {
        excelArray[rCnt - 1, cCnt - 1] = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2.ToString();

        this.dataGridView1[cCnt - 1, rCnt - 1].Value = excelArray[rCnt - 1, cCnt - 1]; //Note that column goes first in the DGV
    }
}

您可以使用this.dataGridView1.Rows.Add("col1 val", "col2 val")一次性执行这两项操作(行添加和单元格填充);但是,为了做到这一点,你必须重做你的两个循环。