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());
答案 0 :(得分:1)
更新回答:
首先,您应该将对数据库的调用与其余代码分开。让我们创建一个负责良好的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;
}
}
}
然后要绑定DataGridView,您有两个选择:
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属性的官方文档中,您可以通过编程方式找到有关绑定的更多信息。这是link。
答案 1 :(得分:1)
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();
}
未经测试,但我认为它应该可行。只是评论结果。