I am developing a windows form application in C#. My Program will read the values from excel cells and insert the data into DataGridView to display the users.
After I have read the excel files, I get the results in string arrays. How can I insert into DataGridView?
private void btnImport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application ExcelObj = null;
ExcelObj = new Microsoft.Office.Interop.Excel.Application();
if (ExcelObj != null)
{
if (tbxFileDirectory.Text != "")
{
Workbook workbook = ExcelObj.Workbooks.Open(
openFD.FileName, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true);
Sheets sheets = workbook.Worksheets;
//Get excel sheet number
Worksheet worksheet = (Worksheet)sheets.get_Item(1);
for (int i = 6; i <= worksheet.UsedRange.Rows.Count; i++)
{
Range range = worksheet.get_Range("A" + i.ToString(), "K" + i.ToString());
Array myvalues = (Array)range.Cells.Value;
string[] strArray = ConvertToStringArray(myvalues);
//INSERT INTO DATAGRIDVIEW
}
MessageBox.Show("File was imported successfully");
}
else
{
MessageBox.Show("Please select the file to import.");
}
}
else
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
}
}
My DataGrid has 4 columns, ProductID, ProductName, Price, & Qty. I want to insert these values, strArray[0], strArray[1], strArray[9] & strArray[10] in each of the column. How can I do this? Any help will be much appreciated!