Pyromancer
Pyromancer

Reputation: 2509

datagridview not showing numbers from MS Access

I have a datagrid view and it's datasource is MS Access(which have a datatype, currency, date/time, and numbers), It shows data in the database but doesn't show other data types, only words or any string, here is my code for adding rows

string[] rowData = new string[columnCount];
            while (dr.Read())
            {
                for (int k = 0; k < columnCount; k++)
                {
                    if (dr.GetFieldType(k).ToString() == "System.int32")
                    {
                        rowData[k] = dr.GetInt32(k).ToString();
                    }

                    if (dr.GetFieldType(k).ToString() == "System.String")
                    {
                        rowData[k] = dr.GetString(k);
                    }
                }
                dataGridView1.Rows.Add(rowData);
            }

can you help me with this? thanks

Upvotes: 1

Views: 300

Answers (1)

Pyromancer
Pyromancer

Reputation: 2509

Instead of using the code above, I use this code, and it works

private void Form6_Load(object sender, EventArgs e)
        {
            loadData();
        }

private void loadData()
            {
                str = new OleDbConnectionStringBuilder();
                str.Provider = "Microsoft.ace.Oledb.12.0";
                str.DataSource = @"\\sisc-erelim\4_Printing\VTDB\DB\VirginiTEADB2.accdb";
                con = new OleDbConnection(str.ConnectionString);
                dataGridView1.DataSource = fillTable("Select* from Accountstbl");
                dataGridView1.Columns["Password"].Visible = false;
                dataGridView1.Columns["Picture"].Visible = false;
            }

        private DataTable fillTable(string sql)
        {
            DataTable datatable = new DataTable();
            using (OleDbDataAdapter da = new OleDbDataAdapter(sql, con))
            {
                da.Fill(datatable);
            }

            return datatable;
        }

Upvotes: 1

Related Questions