Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET SQL-Server C#4.0
hi ,
 
In My Project I'm Using a Datagridview to load a excel file and show it in Datagridview.
While loading a excel file it takes more time so in between . I need to display a picture box with message "Please Wait" in Gif file format . Using backgroundWorker tool, I Wrote some coding . but it doesn't works . can any one help me with right coding using MultiThread Concept.
 

here is my code
 
  private void btnLoadData_Click(object sender, EventArgs e)
        {
             BackgroundWorker worker = new BackgroundWorker();
 
            worker.DoWork += delegate
            {   
            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.ApplicationClass();
 
            // Opening Excel file
            workbook = excelApp.Workbooks.Open(txtPath.Text, 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 = excelApp.get_Range("A1", Type.Missing);
            if (range != null)
            {
                var val = range.Value2;
                var sourceString= val.ToString().Trim();
                var remove="Period";
                var a = sourceString.IndexOf(remove);
                var b = string.Concat(sourceString.Substring(0, a), sourceString.Substring(a + remove.Length));
                var c = b.ToString().TrimStart();
 
                var remove1 = ":";
                var d = c.IndexOf(remove1);
                var f = string.Concat(c.Substring(0, d), c.Substring(d + remove1.Length));
                var g = f.ToString().TrimStart();
 
                var remove2 = "to";
                var h = g.IndexOf(remove2);
                var i = string.Concat(g.Substring(0, h), g.Substring(h + remove2.Length));
                var j = i.ToString().TrimStart();
 
                string sub = j.Substring(0,10);
                string sub1 = j.Substring(12,10);
 
                textEdit1.Text += Convert.ToString(sub).Trim();
                textEdit3.Text += Convert.ToString(sub1).Trim();
            }
            range = excelApp.get_Range("A2", Type.Missing);
            if (range != null)
            {
                var val1 = range.Value2;
                var sourceString= val1.ToString().Trim();
                var remove="Supplier";
                var a = sourceString.IndexOf(remove);
                var b = string.Concat(sourceString.Substring(0, a), sourceString.Substring(a + remove.Length));
                var c = b.ToString().TrimStart();
                var remove1=":";
                var d = c.IndexOf(remove1);
                var f = string.Concat(c.Substring(0, d), c.Substring(d + remove1.Length));
                var g = f.ToString().TrimStart();
                textEdit2.Text += Convert.ToString(g).Trim();
            }
            range = worksheet.UsedRange;
            
            int column = 0;
            int row = 0;            
 
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("Product");
            dt.Columns.Add("Item/Model Code");
            dt.Columns.Add("Item/Model");
            dt.Columns.Add("Qty.");
            dt.Columns.Add("Sales Value");
            dt.Columns.Add("Discount Amt.");
            dt.Columns.Add("Amount");
            dt.Columns.Add("Net Amount");
            dt.Columns.Add("SR Qty");
            dt.Columns.Add("SR Value");
            dt.Columns.Add("SR Discount Amt.");
            dt.Columns.Add("SR Tax Amt.");
            dt.Columns.Add("SR Net Amt.");
            dt.Columns.Add("Total Qty");
            dt.Columns.Add("Total Value");
            dt.Columns.Add("Total Discount");
            dt.Columns.Add("Total Tax");
            dt.Columns.Add("Total Net Value");
            
            for (row = 5; row <= range.Rows.Count; row++)
            {
                DataRow dr = dt.NewRow();                               
                for (column = 1; column <= range.Columns.Count; column++)
                {
                    dr[column - 1] = (range.Cells[row, column] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
                }
                dt.Rows.Add(dr);                
            }
 
            //workbook.Close();
            //excelApp.Quit();

            // Binding to DataGridView
            dataGridView1.DataSource = dt;
            };
 
            worker.RunWorkerCompleted += delegate
            {
                pictureEdit1.Visible = false;
                worker.Dispose();
                btnLoadData.Enabled = true;
            };
 
            //backgroundWorker1.RunWorkerAsync();
            worker.RunWorkerAsync();
        }      
Posted 8-Mar-13 3:46am
Edited 8-Mar-13 21:27pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use item template. image item template in data grid. populate it using a generic hangler. google it. there are lots of such examples
  Permalink  
Comments
KalaiPondy at 9-Mar-13 2:26am
   
i'm using picture edit tool for gif file loading ...

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

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 162
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100