1
我在窗體上有2個dataGridViews,當我運行該應用程序時,什麼也沒有顯示出來。我相信在代碼中添加dataGridView信息,如下所示,我不需要使用Visual Studio的「服務器資源管理器」來添加數據庫。這將是多餘的和/或改變dataGridView的填充結果,對吧?我在代碼中丟失了什麼?將Access數據庫添加到C#dataGridView(僅使用代碼)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OleDbDataAdapter daOrders = new OleDbDataAdapter();
OleDbDataAdapter daReceived = new OleDbDataAdapter();
OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Query Form\Database.accdb");
OleDbCommand slctOrders = new OleDbCommand("SELECT * FROM script_Orders ORDER BY script");
daOrders.SelectCommand = slctOrders;
daOrders.Fill(ds, "tblOrders");
OleDbCommand slctReceived = new OleDbCommand("SELECT * FROM script_Received");
daOrders.SelectCommand = slctReceived;
daOrders.Fill(ds, "tblReceived");
dgOrders.DataSource = ds.Tables ["tblOrders"];
dgReceived.DataSource = ds.Tables ["tblReceived"];
}
}
}
工作正常!萬分感謝! – Geo 2012-04-25 14:09:41
很高興我能幫到你。 – 2012-04-25 14:10:14