Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 4th, 2009, 04:33 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default append data in datagridview

I have already had some data in datagrid view and i want to import fresh records from excel file in such a way that imported data shd append in datagridview.


The code I provided is only get data from excel file and import to dgv. What modification shd i made that it would append and keep previous data in datagridview.







Code:
private DataTable getDataFromXLS(string strFilePath)
        {
            try
            {
                string strConnectionString = "";  //Data Source=HARIS\SQLEXPRESS;Initial Catalog=MFSAS;User ID=akram
                strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strConnectionString);
                conn.Open();
                OleDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = "Select * FROM [Sheet1$]";
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                da = null;
                return dt;
            }

            catch (Exception ex)
            {
                return null;
            }
            finally { }
        }
        private void btnImportData_Click(object sender, EventArgs e)
        {
            DataTable test = getDataFromXLS(@"c:\temp.xls");
            if (test != null)
            {
                dgvViewJDRecords.DataSource = test;
            }
        }
  #2 (permalink)  
Old March 4th, 2009, 06:38 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Because your grid is bounded to the source, you have to append the new data to the datatable you are supplying to the datagrid.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #3 (permalink)  
Old March 4th, 2009, 06:45 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default dgv issue

dgvViewJDRecords.DataSource = test;


which property shd i use to append data instead of <DataSource>
  #4 (permalink)  
Old March 4th, 2009, 06:52 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

DataSource is the correct one.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
append/insert data to xml file andhiez XML 5 April 18th, 2008 11:13 AM
create table/append data. bpdineen Access VBA 1 January 4th, 2007 11:47 AM
What is the best method to append data? rdmapes PHP How-To 0 December 27th, 2006 01:06 PM
Save data on datagridview salemkoten SQL Server 2005 1 November 2nd, 2006 11:04 PM
append data to xml andhiez General .NET 0 June 14th, 2005 04:15 AM



All times are GMT -4. The time now is 08:51 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.