locked
populate datagrid with datareader in vb.net RRS feed

  • Question

  • hello everybody
     iam unable to populate datagrid with datareader object in vb.net
    i am not interested in using dataset
    the following code is not working
    datagrid1.datasource=datareader
    datagrid1.databind
    Thursday, September 8, 2005 6:58 PM

Answers

  • The Windows Forms DataGrid has a different requirement than the Web Forms one: you need to be able to get data at a given row (IList) instead of just read it through forward once (IEnumerable, which is what DataReader is).  One way around this is to use BindingSource, which will read the data out:
      Dim source As New BindingSource
      source.DataSource = datareader
      datagrid1.DataSource = source

    -Scott

    Thursday, September 22, 2005 5:41 PM

All replies

  • The Windows Forms DataGrid has a different requirement than the Web Forms one: you need to be able to get data at a given row (IList) instead of just read it through forward once (IEnumerable, which is what DataReader is).  One way around this is to use BindingSource, which will read the data out:
      Dim source As New BindingSource
      source.DataSource = datareader
      datagrid1.DataSource = source

    -Scott

    Thursday, September 22, 2005 5:41 PM
  • Hi Scott, but I found BinidingSource is part of framework 2.0, Is there any alternative way in 1.1 to achieve the same.

    -prince

    Friday, September 23, 2005 1:59 PM
  • Hi Scott,
                   I am using the Vs 2008. i tryed what you have mention in the above.but i am getting error.Bellow i have mentioned my code.

     

    SqlConnection con = new SqlConnection("SERVER=.;UID=sa;PWD=123;DATABASE =NorthWind");

    con.Open();

     

    SqlCommand objCommand = new SqlCommand("SELECT FirstName, LAstName from Employees",con);

    sqlDataReader = objCommand.ExecuteReader(System.Data.

    CommandBehavior.CloseConnection);

     

    BindingSource bindingSource = new BindingSource();

    bindingSource = sqlDataReader;

    /// Here i am getting error "Can not implicity convert type"

     

    this.dataGridT1.DataSource = sqlDataReader;


    -Balamurugan.S

    Sunday, July 19, 2009 9:38 AM
  • Hello Pradeep Gupta,

    You can bind the Datagridview using the dataReader and that would need a work around.

    Using VB .Net 2010 & Access2007:

    Imports System.Data.OleDb

    Public Class Form1
    Dim CN as New Oledb.OledbConnection
    Dim CnrStr As String
    Dim MyReader As Oledb.OledbDataReader
    Dim MyTable as DataTable
    Dim CMD as New Oledb.OledbCommand

    Sub DataGridWork()
    Cnstr = ("Provier = Microsoft.ACE.OleDB.12.0 ; Data Source = DB.accdb")
    CN.ConnectionString = CnStr
    CN.Open

    With CMD
          .Connection = CN
          .CommandType = CommandType.Text
          .CommandText = ("Select * From TableName")
    End With

    MyReaer = CMD.ExecuteReader

    Dim I As Integer
            For I = 0 To MyTable.Rows.Count - 1
                MyReader.Read()       
    Next       

    MyTable.Load(MyReader)
    DataGridView1.DataSource = MyTable
    DataGridView1.Refresh

    End Sub

    Previous code won't only populate data into a datagrid control, but also will update the datagrid with no duplications each time you call it in a new sub like (Update Database table, Edit, Delete).

    For more details, Read This book


    http://adonetaccess2003.blogspot.com http://vb6access2003.blogspot.com http://ahmedsamir.awardspace.com


    • Edited by evry1falls Saturday, May 26, 2012 3:33 PM
    • Proposed as answer by evry1falls Saturday, May 26, 2012 3:33 PM
    Saturday, May 26, 2012 3:32 PM