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 testdb
{
public partial class Form1 : Form
{
private string constr =
@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:/Users/Xprts_3/Documents/Database1.accdb";
public Form1()
{
InitializeComponent();
Bind();
}
private void Bind()
{
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select * from tb1";
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dataGridView1.Rows.Add(new DataGridViewRow());
int j;
for (j = 0; j < ds.Tables[0].Columns.Count; j++)
{
dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
}
}
con.Close();
}
}
}
这是我在DataGridView
中从数据库中获取数据的代码,但它显示此错误:
当控件是数据绑定时,不能以编程方式将行添加到 DataGridView 的行集合中 在这一行:
dataGridView1.Rows.Add(new DataGridViewRow());
Lucian的答案是对的,在这种情况下你应该使用DataBind()。但是,如果要以其他方式在 DataGridView 中添加行,下面是一个示例:
private void Bind()
{
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select * from tb1";
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
var row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
row.Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
}
dataGridView1.Rows.Add(row);
}
con.Close();
}
没有经过测试,但我认为它应该有效。只是评论结果。
更新的答案:
首先,您绝对应该将对数据库的调用与其余代码分开。让我们创建一个GetData()
方法,负责井...获取数据:)
private DataSet GetData(string constr) {
//'using' constructs are always a good idea when dealing with database operations
//your connection will automatically close
using(OleDbConnection con = new OleDbConnection(constr)){
using(OleDbCommand cmd = new OleDbCommand()){
cmd.Connection = con;
cmd.CommandText = "select * from tb1";
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
然后,若要绑定数据网格视图,有两个选项:
- 使用
DataBind()
方法 - 以编程方式创建网格列和行
1 - DataBind() 方法:
private void BindWithDataBind() {
dataGridView1.DataSource = GetData();
//call the databind method to bound the data to the grid
//the grid structure will be created automatically from the datatable structure
dataGridView1.DataBind();
}
方法 2 - 编程方式
private void BindProgramatically() {
//obtain the data from your OleDB connection
DataSet ds = GetData(constr);
if(ds == null) {
return;
}
DataTable dt = ds.Tables[0];
//build the grid structure, add a new grid column for each datatable column
for(int i = 0; i < dt.Columns.Count; i++) {
DataGridViewColumn newColumn = new DataGridViewColumn();
newColumn.Name = dt.Columns[i].ColumnName;
dataGridView1.Columns.Add(newColumn);
}
for (int i = 0; i < dt.Rows.Count; i++) {
//call ToArray to pass a copy of the data from the datatable
//(make sure you have 'using System.Linq;' at the top of your file
dataGridView1.Rows.Add(dt.Rows[i].ItemArray.ToArray());
}
}
在 MSDN 有关 DataGridView.Columns 属性的官方文档中,可以找到有关以编程方式绑定的详细信息。这是链接。