Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();// i;
                    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
Posted
Updated 5-Sep-11 21:13pm
v2
Comments
CyborgForever 5-Sep-11 7:28am    
No one have some solution for this

1 solution

C#
for (int i = 1; i <= horizontal; i++)
 {
          DataRow objdr = dsobbtemp.Tables[0].NewRow();
       objdr["objectRevisedName"] = myValues.GetValue(1, i).ToString();// i;
        if(i>1)
         {
            if( dsobbtemp.Tables[0].Rows[i-1]!=objdr)
             {
                 dsobbtemp.Tables[0].Rows.Add(objdr);
              }
          }
        else
        {
            dsobbtemp.Tables[0].Rows.Add(objdr);
        }
 }
 
Share this answer
 
Comments
CyborgForever 7-Sep-11 1:52am    
Can u pls explain on what basis u've given the condition i>1. FYI it's just importing everything from the excel without any validation, imports data which is already existing in dsobtemp dataset

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900