【发布时间】:2015-11-27 06:44:09
【问题描述】:
我尝试在 datagridview 中的两个现有行之间添加插入新的空白行(已绑定到数据库),但出现以下异常:
当控件绑定数据时,不能以编程方式将行添加到 DataGridView 的行集合中
我的代码
private void load()
{
string strProvider = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AMOL\Desktop\Maratha.accdb";
string strSql = "Select * from Maratha";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable scores = new DataTable();
da.Fill(scores);
DGMaratha.DataSource = scores;
}
public void BtnRefresh_Click(object sender, EventArgs e)
{
try
{
int row = (this.DGMaratha.CurrentCell.RowIndex);
int column = (this.DGMaratha.CurrentCell.ColumnIndex);
load();
this.DGMaratha.CurrentCell = this.DGMaratha[column, row];
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
}
private void BtnAdd_Click(object sender, EventArgs e)
{
int row = (this.DGMaratha.CurrentCell.RowIndex);
DGMaratha.Rows.Insert(row, 1);
}
【问题讨论】:
标签: c# winforms datagridview