2 Replies - 769 Views - Last Post: 05 November 2011 - 03:40 AM Rate Topic: -----

#1 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

[Problem] Passing data between form and class

Posted 04 November 2011 - 04:59 PM

Hello,

I'm having problem parsing data between my Form1Main.cs and DataCode.cs.

In Form1Main.cs I have this:
private void Form1Main_Load(object sender, EventArgs e)
        {
            datacode.LoadData(dataGridView1, dt);
        }

In DataCode.cs I have this:
public void LoadData(DataGridView dgv, DataTable dt)
        {
                dt = GetDataTable(@"C:\Users\" + user + @"\AppData\Local\KFredje & Co\Doga\Doga.csv");
                dgv.DataSource = dt;
        }

        public static DataTable GetDataTable(string strFileName)
        {
            ADODB.Connection oConn = new ADODB.Connection();
            oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0);
            string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
            ADODB.Recordset rs = new ADODB.Recordset();
            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
            DataTable dt = new DataTable();
            rs.Open(strQuery, "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";",
                ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
            adapter.Fill(dt, rs);
            return dt;
        }


Now the strange thing happens to me. So when I start my program, the dataGridView1 in Form1Main.cs get filled with the data from dt in DataCode.cs. Now, I want the dt as a DataTable being parsed from DataCode.cs to Form1Main.cs but this doesn't do a thing.

Any clue what I'm doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: [Problem] Passing data between form and class

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: [Problem] Passing data between form and class

Posted 04 November 2011 - 05:19 PM

Quote

So when I start my program, the dataGridView1 in Form1Main.cs get filled

Makes sense since you are getting your table data when the FormLoad event is raised.
public void LoadData(DataGridView dgv, DataTable dt)



Quote

Now, I want the dt as a DataTable being parsed from DataCode.cs to Form1Main.cs but this doesn't do a thing.

I don't see any method where you are trying to pass it from one class to another. When and why do you think it should be passed? There doesn't seem to be any action (such as a button click) that you react to that would do the sending.

See FAQ #2 for class-to-class (form-to-form) communication and data passing.


FAQ (Frequently Asked Questions - Updated Oct 2011

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: [Problem] Passing data between form and class

Posted 05 November 2011 - 03:40 AM

Sorry to say this, but your post didn't help me a lot.

But, I tried this instead and this seem to work out for now.
public DataTable dt
        {
            get { return GetDataTable(@"C:\Users\" + user + @"\AppData\Local\KFredje & Co\Doga\Doga.csv"); }
        }

This post has been edited by KFredje: 05 November 2011 - 03:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1