locked
how to fill DataGridView with DataReader RRS feed

  • Question

  • hi
    i need to fill DataGridView with DataReader
    i have this code:

     

    OracleConnection con = new OracleConnection ("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + IP + ")(PORT=" + PORT + ")))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" + Server + ")));User Id=" + UID + ";Password=" + PASS + ";");

     

    OracleCommand cmd;

     

    OracleDataReader dr;


    cmd =

    new OracleCommand("SELECT * FROM trap", con);

    con.Open();

    dr = cmd.ExecuteReader();

    Friday, December 4, 2009 6:24 PM

Answers

  • COuld you try this code.

                DataSet ds = new DataSet();
                DataTable dt = new DataTable("trap");
                ds.Tables.Add(dt);
                ds.Load(dr, LoadOption.PreserveChanges, ds.Tables[0]);
                gridControl1.DataSource = ds.Tables[0];
    • Proposed as answer by Tamer Oz Tuesday, December 8, 2009 8:39 AM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 6:59 PM
  • Use the code to read into a DataTable instead  of reading using a DataReader and then you can build the table.

    If you really have to use a datareader, I believe that there is a command to create a DataTable from the results of your data reader.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Tamer Oz Tuesday, December 8, 2009 8:39 AM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 7:00 PM
  • Hi
                If you want to use datagridview with Database , you must assign the datasource property for retrieving data,
    you can not use the datareader as Datasource because it is a valid datasource element , you can use dataset or datatable for data retrieval , further refer this resouce

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource%28VS.80%29.aspx

    • Proposed as answer by JohnGrove Friday, December 4, 2009 10:07 PM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 7:11 PM
  • Generally you use a data reader to convert the "raw" data into a collection of business objects which you then apply to a DGV.

    If you just need to go directly to a DGV, use a data table and data adapter instead as Raja states above as that's much simpler.
    • Proposed as answer by JohnGrove Friday, December 4, 2009 10:07 PM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 9:06 PM

All replies

  • hi
    i need to fill DataGridView with DataReader
    i have this code:

     

    OracleConnection con = new OracleConnection ("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + IP + ")(PORT=" + PORT + ")))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" + Server + ")));User Id=" + UID + ";Password=" + PASS + ";");

     

    OracleCommand cmd;

     

    OracleDataReader dr;


    cmd =

    new OracleCommand("SELECT * FROM trap", con);

    con.Open();

    dr = cmd.ExecuteReader();

    • Merged by Harry Zhu Monday, December 7, 2009 9:45 AM
    Friday, December 4, 2009 6:24 PM
  • COuld you try this code.

                DataSet ds = new DataSet();
                DataTable dt = new DataTable("trap");
                ds.Tables.Add(dt);
                ds.Load(dr, LoadOption.PreserveChanges, ds.Tables[0]);
                gridControl1.DataSource = ds.Tables[0];
    • Proposed as answer by Tamer Oz Tuesday, December 8, 2009 8:39 AM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 6:59 PM
  • Use the code to read into a DataTable instead  of reading using a DataReader and then you can build the table.

    If you really have to use a datareader, I believe that there is a command to create a DataTable from the results of your data reader.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Tamer Oz Tuesday, December 8, 2009 8:39 AM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 7:00 PM
  • Ditto
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Friday, December 4, 2009 7:00 PM
  • Hi
                If you want to use datagridview with Database , you must assign the datasource property for retrieving data,
    you can not use the datareader as Datasource because it is a valid datasource element , you can use dataset or datatable for data retrieval , further refer this resouce

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource%28VS.80%29.aspx

    • Proposed as answer by JohnGrove Friday, December 4, 2009 10:07 PM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 7:11 PM
  • Generally you use a data reader to convert the "raw" data into a collection of business objects which you then apply to a DGV.

    If you just need to go directly to a DGV, use a data table and data adapter instead as Raja states above as that's much simpler.
    • Proposed as answer by JohnGrove Friday, December 4, 2009 10:07 PM
    • Marked as answer by Harry Zhu Friday, December 11, 2009 2:33 AM
    Friday, December 4, 2009 9:06 PM