beginners problems with controls and classes

G

gordon

Hi

I am learning C# out of books - and soon will be attending some courses. I
like to write a bit of code and see what happens, but I find that I get
stuck at the same spot each time.

my code is simple and has 2 classes - the form class and the getdata class.
When I wish to reference the controls on the form with my getdata classs I
get a message that dgHH (my HH datagrid view) does not exist in the current
context.

I could fool around and find a solution - but I would like to know the best
way to do this.

If you like to show me a better way, taking into account my limited
understanding of C#, I would appreciate it.

The code below does not compile - and there maybe other bugs in it - but I
cant seem to resolve the context one first.

Thanks

Doug


namespace oleClass

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

}

public class GetData

{

private void Datagrab()

{

OleDbConnection conn = null;

OleDbDataReader reader = null;

string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\hh.mdb";

string strSelect = "Select * from Vars";

try

{

conn = new OleDbConnection(strConnection);

conn.Open();

OleDbCommand cmd = new OleDbCommand(strSelect);

reader = cmd.ExecuteReader();

dgHH.DataSource = reader;

dgHH.DataBind();

}

catch (Exception e)

{

MessageBox.Show(e.Message);

}

finally

{

if (reader != null) reader.Close();

if (conn != null) conn.Close();

}

}

}

}
 
D

dongmt

if you use datagrid why dont you use OleDataAdapter
then fill to DataSet from your dataapder
then set datagrid.datasource=dataset

for example

OleDbDataAdapter da=new OleDbadapter(" your select sql", your
connection);
DataSet ds=new DataSet()// it will store your data, not need " reader";
da.fill(ds,"tablename");
datagrid.datasource=ds;

DongMT
(e-mail address removed)
 
G

gordon

Thanks Dong

There are many variants of Ole connections and datasets, dataadapters etc.
But I am actually looking for a more general solution trying to resolve the
issue of the dgHH not being in the same context.

For example, my datagridView is called dgHH. When i try to place my oleDB
code in a separate class, I am no longer able to refer to that - and I get
the context message.

I am looking for some advice on how to get around that issue.

thanks
 
C

Chris Dunaway

gordon said:
For example, my datagridView is called dgHH. When i try to place my oleDB
code in a separate class, I am no longer able to refer to that - and I get
the context message.

In order to access any class you must have a reference to the class.
Since your DataGridView is just a class, in order to access it you must
have a reference to it. Probably the easiest way to get a reference to
your DataGridView is to pass a reference to the form it is on into the
class from which you wish to access it:

//(This code assumes that Form1 is the name of your form class)

public class SomeClass
{
private Form1 _formRef;

public SomeClass(Form1 f)
{
_formRef = f;
}

public Foo()
{
//to get to your datagrid (or any other property of the form
_formRef.dgHH. ..... ;
}
}

Hope this helps

Chris
 
G

gordon

Hi Chris

Thanks for your help - but this still isnt working for me.

My code is below, would you mind to have a look at let me know where I am
going wrong. I understand what you wrote - but not sure if i implemented it
correctly.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

namespace oleClass

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public void btnSmoke_Click(object sender, EventArgs e)

{

GetData.DataGrab();

}

}

public class GetData

{

public Form1 _formRef;

public GetData(Form1 f)

{

_formRef = f;

}

public static void DataGrab()

{


string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\hilda helpa.mdb";

string strSelect = "Select * from Vars";



DataSet DS = new DataSet();

OleDbConnection myConnection = null;

myConnection = new OleDbConnection(strConnection);

OleDbCommand strCommand = new OleDbCommand(strSelect, myConnection);

OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(strCommand);

myConnection.Open();

myDataAdapter.Fill(DS, "vars");

DataTable dataTable = DS.Tables[0];

_formRef.dgHH.DataSource = DS.Tables["vars"].DefaultView;

myConnection.Close();

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top