dataGridView列标题

时间:2012-10-11 19:01:56

标签: c# .net datagridview

我正在将Excel文件读入C#.NET中的dataGridView,但它会为第一行呈现列标题,如何消除渲染列标题以使所有行看起来都相同?

我有openDialogBox1和button1打开此对话框并选择excel文件:

private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {                
                Excel.Application excelApp = new Excel.Application();
                //excelApp.Visible = true;

                Excel.Workbook theWorkbook = excelApp.Workbooks.Open(openFileDialog1.FileName, 0, true, 5,
                                                                    "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
                                                                    0, true);
                Excel.Sheets sheets = theWorkbook.Worksheets;
                Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);

                /*
                for (int i = 1; i <= 3; i++)
                {
                    Excel.Range range = worksheet.get_Range("A"+i.ToString(), "B" + i.ToString());
                    dataGridView1.DataSource = rangeTable(range);                                        
                }
                 */
                Excel.Range range = worksheet.get_Range("A1", "B3");
                dataGridView1.DataSource = rangeTable(range);                                                                 
            }            
        }

        private DataTable rangeTable(Excel.Range range)
        {
            DataTable dataTable = new DataTable();
            int rowCnt = range.Rows.Count;
            int colCnt = range.Columns.Count;

            for (int i = 1; i <= rowCnt; i++)
            {
                DataRow newRow = dataTable.NewRow();
                for (int j = 1; j <= colCnt; j++)
                {
                    if (i == 1)
                    {
                        dataTable.Columns.Add(((Excel.Range)range.get_Item(i, j)).Value2.ToString(), typeof(string));
                    }
                    else
                    {
                        newRow[j - 1] = ((Excel.Range)range.get_Item(i, j)).Value2;
                    }
                }
                if (i > 1)
                {
                    dataTable.Rows.Add(newRow);
                }
            }
            return dataTable;
        }

由于

1 个答案:

答案 0 :(得分:0)