Hi in my application I'm importing from excel to datagridview. Columns in my excel is imported as rows to datagridview.
Eg in Excel Item1 Item2 Item 3 are available
But in datagridview I'm displaying it as
Item1
ITem2
Item3
I've done this successfully after a long run.
I'm actually binding the dataset to datagridiview before the import is done, ie during the form load itself.
So I want to check whether newly added row is already existing in the dataset before binding to datagridview a form of validation.
for Eg
DataGridview
Item1
Item2
Item3
Again if Item2 Item3 exists while adding new row (ie importing from excel)
Following is wat I've done so far
private void btImport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excelApp;
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Worksheet worksheet;
Microsoft.Office.Interop.Excel.Range range;
excelApp = new Microsoft.Office.Interop.Excel.Application();
string filename = string.Empty;
var missing = System.Reflection.Missing.Value;
try
{
OpenFileDialog ofdGetFile = new OpenFileDialog();
ofdGetFile.Filter = "Excel files |*.xls|All files (*.*)|*.*";
ofdGetFile.FilterIndex = 2;
ofdGetFile.CheckFileExists = true;
ofdGetFile.Title = "Select an Excel File";
ofdGetFile.RestoreDirectory = true;
ofdGetFile.ShowDialog();
filename = ofdGetFile.FileName;
workbook = excelApp.Workbooks.Open(filename, 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1);
range = worksheet.UsedRange;
Array myValues = (Array)range.Cells.Value2;
int vertical = myValues.GetLength(0);
int horizontal = myValues.GetLength(1);
for (int i = 1; i <= horizontal; i++)
{
DataRow objdr = dsobbtemp.Tables[0].NewRow();
objdr["objectRevisedName"] = myValues.GetValue(1, i).ToString();
dsobbtemp.Tables[0].Rows.Add(objdr);
}
dgDtParameters.DataSource = dsobbtemp.Tables[0];
workbook.Close(true,null,null);
excelApp.Quit();
releaseObject(workbook);
releaseObject(worksheet);
releaseObject(excelApp);
}
catch (Exception exGeneral)
{
MessageBox.Show(exGeneral.Message);
}
finally
{
GC.Collect();
}
}
How can I accomplish this.
Anyone help me,really breaking my heads
Thanks in advance