Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Display data in datagridview from access database

I have the codes below here in displaying data in datagridview from access database. I have different rows but in only display the last row of data in database. I dont know what's wrong in my code.

    dataGridView1.Columns.Add("UserID", "UserID");
    dataGridView1.Columns.Add("FirstName", "FirstName");
    dataGridView1.Columns.Add("MI", "MI");
    dataGridView1.Columns.Add("LastName", "LastName");
    dataGridView1.Columns.Add("Birthdate", "Birthdate");
    dataGridView1.Columns.Add("Address", "Address");
    dataGridView1.Columns.Add("UserName", "UserName");
    dataGridView1.Columns.Add("UserPassword", "UserPassword");
    dataGridView1.Columns.Add("Rights", "Rights");


    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\SISC-STRONGHOLD\MIS!\wilbert.beltran\SEEDBucksDbase.accdb");
    conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = "SELECT * From TableAcct";
    OleDbDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        dataGridView1.Rows.Add();

        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserID"].Value = reader[0].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["FirstName"].Value = reader[1].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["MI"].Value = reader[2].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["LastName"].Value = reader[3].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Birthdate"].Value = reader[4].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Address"].Value = reader[5].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserName"].Value = reader[7].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserPassword"].Value = reader[8].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Rights"].Value = reader[9].ToString();
    }
    conn.Close();
}
like image 754
bhert Avatar asked Mar 01 '13 02:03

bhert


People also ask

How retrieve data from database and display in DataGridView in C#?

Step 1: Make a database with a table in SQL Server. Step 2: Create a Windows Application and add DataGridView on the Form. Now add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.


1 Answers

You can directly bind dataGridView1 using OleDbDataAdapter. Make sure that the names of the columns in the datagridview matches with the field names return by the query so it will contain blank columns and create another column for every fields.

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;
        Data Source=\\SISC-STRONGHOLD\MIS!\wilbert.beltran\SEEDBucksDbase.accdb";
string query = "SELECT * From TableAcct";
using(OleDbConnection conn = new OleDbConnection(connStr))
{
    using(OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
    {
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        DataGridView1.DataSource= ds.Tables[0];
    }
}
like image 64
John Woo Avatar answered Oct 19 '22 04:10

John Woo