DataGrid.Datasource

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • freddy

    DataGrid.Datasource

    I have a class for DatabaseManager .cs that has all the connection path, add,
    update, delete.I have a problem with the datagrid because I have a form that
    contains the datagrid called frmAllProjects. cs, so how do I connected to my
    dbmanager.cs from the form.
    DatabaseManager Code:
    OleDbConnection Conn = new OleDbConnection ();
    string sConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
    Source=E:\\Fred dy\\Projects\\v b.net\\Project Track It\\bin\\Projec t.mdb";
    Conn.Connection String = sConnString;
    Conn.Open();

    string select = "select * from Project";
    OleDbDataAdapte r da = new OleDbDataAdapte r (select, Conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    frmAllProjects code: <-- this is the form Code
    DatabaseManager manager = new DatabaseManager ();
    manager.path();
    //Datagrid
    dg1.Datasource = ds (); <-- I get the error here because ds is not found --
    But when I have all the code on the datagrid it works fine.

    Also I do not what th hardcode the data Source like this:
    OleDbConnection Conn = new OleDbConnection ();
    string sConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
    Source=E:\\Fred dy\\Projects\\v b.net\\Project Track It\\bin\\Projec t.mdb";

    I want to do this:
    OleDbConnection Conn = new OleDbConnection ();
    string sConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
    Source=Project. mdb"; <-- it does not work like this
  • Dmytro Lapshyn [MVP]

    #2
    Re: DataGrid.Dataso urce

    Hi Freddy,

    The simplest solution would probably be to have the DatabaseManager class
    keep a reference to the retrieved dataset and return the reference as a
    read-only public property.
    Obviously the form containing the grid must keep a reference to the
    DatabaseManager instance during the form's lifetime.

    --
    Sincerely,
    Dmytro Lapshyn [Visual Developer - Visual C# MVP]


    "freddy" <freddy@discuss ions.microsoft. com> wrote in message
    news:6CA112AB-C791-4068-A2BC-18694277CB17@mi crosoft.com...[color=blue]
    >I have a class for DatabaseManager .cs that has all the connection path,
    >add,
    > update, delete.I have a problem with the datagrid because I have a form
    > that
    > contains the datagrid called frmAllProjects. cs, so how do I connected to
    > my
    > dbmanager.cs from the form.
    > DatabaseManager Code:
    > OleDbConnection Conn = new OleDbConnection ();
    > string sConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
    > Source=E:\\Fred dy\\Projects\\v b.net\\Project Track It\\bin\\Projec t.mdb";
    > Conn.Connection String = sConnString;
    > Conn.Open();
    >
    > string select = "select * from Project";
    > OleDbDataAdapte r da = new OleDbDataAdapte r (select, Conn);
    > DataSet ds = new DataSet();
    > da.Fill(ds);
    > frmAllProjects code: <-- this is the form Code
    > DatabaseManager manager = new DatabaseManager ();
    > manager.path();
    > //Datagrid
    > dg1.Datasource = ds (); <-- I get the error here because ds is not
    > found --
    > But when I have all the code on the datagrid it works fine.
    >
    > Also I do not what th hardcode the data Source like this:
    > OleDbConnection Conn = new OleDbConnection ();
    > string sConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
    > Source=E:\\Fred dy\\Projects\\v b.net\\Project Track It\\bin\\Projec t.mdb";
    >
    > I want to do this:
    > OleDbConnection Conn = new OleDbConnection ();
    > string sConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
    > Source=Project. mdb"; <-- it does not work like this[/color]

    Comment

    Working...