我正在使用Paging在datagridview
中显示数据,但是当我尝试使用updatebutton
数据更新任何数据时,应在datagridview
以及数据库中更新。
但是我收到了这个错误:
更新在传递DataRow集合时需要有效的UpdateCommand 修改行
发生在这一行:
adp1.Update(dt);//here I am getting error
以下是代码
public partial class EditMediClgList : Form
{
public EditMediClgList()
{
InitializeComponent();
try
{
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb");
con.Open();
}
catch (Exception err)
{
MessageBox.Show("Error:" +err);
}
cmd1 = new OleDbCommand("Select * from MedicalColeges order by MedicalClgID", con);
ds = new DataSet();
adp1 = new OleDbDataAdapter(cmd1);
adp1.Fill(ds, "MedicalColeges");
dataGridView1.DataSource = ds;
// Get total count of the pages;
this.CalculateTotalPages();
// Load the first page of data;
this.dataGridView1.DataSource = GetCurrentRecords(1, con);
}
private void CalculateTotalPages()
{
int rowCount = ds.Tables["MedicalColeges"].Rows.Count;
this.TotalPage = rowCount / PageSize;
if (rowCount % PageSize > 0) // if remainder is more than zero
{
this.TotalPage += 1;
}
}
private DataTable GetCurrentRecords(int page, OleDbConnection con)
{
dt = new DataTable();
if (page == 1)
{
cmd2 = new OleDbCommand("Select TOP " + PageSize + " * from MedicalColeges ORDER BY MedicalClgID", con);
// CurrentPageIndex++;
}
else
{
int PreviouspageLimit = (page - 1) * PageSize;
cmd2 = new OleDbCommand("Select TOP " + PageSize +
" * from MedicalColeges " +
"WHERE MedicalClgID NOT IN " +
"(Select TOP " + PreviouspageLimit + " MedicalClgID from MedicalColeges ORDER BY MedicalClgID) ", con); // +
//"order by customerid", con);
}
try
{
// con.Open();
this.adp1.SelectCommand = cmd2;
this.adp1.Fill(dt);
txtPaging.Text = string.Format("page{0} of {1} pages", this.CurrentPageIndex, this.TotalPage);
}
finally
{
// con.Close();
}
return dt;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
adp1.Update(dt);//here I am getting error
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
}
答案 0 :(得分:9)
您仅使用OleDbDataAdapter
命令创建了Select
:
adp1 = new OleDbDataAdapter(cmd1);
OleDbDataAdapter
需要使用有效的Update
,Insert,
Delete
命令来保存这样的数据:
adp1.Update(dt);//here I am getting error
您只需使用可为您生成命令的OleDbCommandBuilder
:
adp1 = new OleDbDataAdapter();
adp1.SelectCommand = cmd1; // cmd1 is your SELECT command
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp1);
修改强>
由于您在运行时更改了OleDbDataAdapter
的Select命令以进行分页,因此每次保存数据时都需要初始化:
private void button1_Click(object sender, EventArgs e)
{
try
{
adp1.SelectCommand = cmd1; // cmd1 is your SELECT command
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp1);
adp1.Update(dt); //here I hope you won't get error :-)
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
答案 1 :(得分:3)
可能是您在表中缺少主键。您需要确保在数据库表的列上设置主键。
答案 2 :(得分:1)
我必须将我的(递增)索引列更改为primary key of my table(如Eaint建议的那样)。在此之后,我不得不在设计器视图中提取DataSet.xsd,右键单击visual DataTable对象并选择configure。 TableAdapter配置向导打开后,我单击了“高级选项”按钮。我选中了Generate Insert,Update和Delete语句复选框,然后单击OK和Finish。在此之后(仍然在设计师视图中),我选择了可视化的TableAdapter对象,它给了我所有的完整属性。 SQL是自动生成的。花了一段时间让我跟踪它,所以我希望它可以帮助别人。
答案 3 :(得分:0)
感谢“ @Chris”,上面的代码对我有用。 我需要指定在更新时将更新的数据库表名称。 您可以在这里阅读有关此内容的更多信息:DataAdapter: Update unable to find TableMapping['Table'] or DataTable 'Table'
// This Adapter and Dataset are used for Populating my datagridview, so I use them also when I need to Update the Datagridview
SqlDataAdapter kundeTlfAdapter;
DataSet kundeTlfDataSet;
try
{
SqlConnection connection = new SqlConnection("Data source=BG-1-PC\\SQLEXPRESS; Database = Advokathuset; User Id = abc; Password = abc;");
SqlCommand cmd1 = new SqlCommand("Select* From Kunde_Tlf", connection);
SqlCommandBuilder builder = new SqlCommandBuilder(kundeTlfAdapter);
kundeTlfAdapter.SelectCommand = cmd1; // cmd1 is your SELECT command
kundeTlfAdapter.Update(kundeTlfDataSet, "Kunde_Tlf"); //I get eror here if I dont add the name of the table that needs Update "Kunde_Tlf"
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}