PC Review


Reply
Thread Tools Rate Thread

beginners problems with controls and classes

 
 
gordon
Guest
Posts: n/a
 
      25th Aug 2006
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();

}

}

}

}


 
Reply With Quote
 
 
 
 
dongmt@gmail.com
Guest
Posts: n/a
 
      25th Aug 2006
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 Removed)

 
Reply With Quote
 
gordon
Guest
Posts: n/a
 
      25th Aug 2006
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




<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 Removed)
>



 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      25th Aug 2006
gordon wrote:
> 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

 
Reply With Quote
 
gordon
Guest
Posts: n/a
 
      25th Aug 2006
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();

}

}

}





"Chris Dunaway" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> gordon wrote:
>> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: beginners problems with controls and classes gordon Microsoft C# .NET 6 27th Aug 2006 04:22 PM
repost : Re: beginners problems with controls and classes gordon Microsoft C# .NET 0 26th Aug 2006 07:24 AM
Re: beginners problems with controls and classes gordon Microsoft C# .NET 0 26th Aug 2006 06:41 AM
Accessing controls in classes DotNetJunkies User Microsoft Dot NET 0 19th Aug 2004 02:58 PM
Re: Library of controls, classes Dmitriy Lapshin [C# / .NET MVP] Microsoft C# .NET 1 23rd Mar 2004 05:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:07 AM.