Importuj dane Excel do DataGridView w Visual Studio 2010
Pomóż naprawić dane importowane zPrzewyższać dokument doDataGridView
sterowanie za pomocą następującego kodu:
private void button5_Click(object sender, EventArgs e)
{
Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook workbook =app.Workbooks.Open(@"C:\Users\Admin\Desktop\Dropbox\Vandit's Folder\Internship\test.xlsx");
Excel.Worksheet worksheet = workbook.ActiveSheet;
rcount = worksheet.UsedRange.Rows.Count;
int i = 0;
for(;i<rcount;i++)
{
dataGridView1.Rows[i].Cells["Column1"].Value = worksheet.Cells[i + 1, 1].Value;
dataGridView1.Rows[i].Cells["Column2"].Value = worksheet.Cells[i + 1, 2].Value;
}
}
kiedy uruchamiam ten kod, zawsze otrzymuję wyjątek
"Index was out of range. Must be non-negative and less than the size of the collection."
"Parameter name: index."