Datarepeater删除仅删除顶部记录
我有一个数据中继器,以下代码仅删除第一条记录,无论选择哪一条。我并不完全相信这是使用数据中继器执行此操作的正确方法,但我找不到更好的解决方案。我需要能够选择任何记录并将其删除。
//delete document
private void cmdDeleteDoc_Click(object sender, EventArgs e)
{
if (this.dataRepeater1.CurrentItemIndex == 0)
{
//begin reset
this.dataRepeater1.BeginResetItemTemplate();
// Delete Row Here
DataClasses1DataContext db = new DataClasses1DataContext();
System.Data.DataRowView SelectedRowView;
newCityCollectionDataSet.DocumentsRow SelectedRow;
SelectedRowView = (System.Data.DataRowView)documentsBindingSource.Current;
SelectedRow = (newCityCollectionDataSet.DocumentsRow)SelectedRowView.Row;
var matchedDocument = (from c in db.GetTable<Document>()
where c.DocIDKey == SelectedRow.DocIDKey
select c).SingleOrDefault();
db.Documents.DeleteOnSubmit(matchedDocument);
db.SubmitChanges();
LoadCaseNumberKey(matchedDocument.CaseNumberKey, false, "documents");
this.dataRepeater1.EndResetItemTemplate();
}
}
任何帮助都会很棒!
I have a datarepeater and the following code ONLY deletes the FIRST record regardless of which one is selected. I am not entirely convinced this is the correct way to do it with a datarepeater but I could not find a better solution. I need to be able to select any record and delete it.
//delete document
private void cmdDeleteDoc_Click(object sender, EventArgs e)
{
if (this.dataRepeater1.CurrentItemIndex == 0)
{
//begin reset
this.dataRepeater1.BeginResetItemTemplate();
// Delete Row Here
DataClasses1DataContext db = new DataClasses1DataContext();
System.Data.DataRowView SelectedRowView;
newCityCollectionDataSet.DocumentsRow SelectedRow;
SelectedRowView = (System.Data.DataRowView)documentsBindingSource.Current;
SelectedRow = (newCityCollectionDataSet.DocumentsRow)SelectedRowView.Row;
var matchedDocument = (from c in db.GetTable<Document>()
where c.DocIDKey == SelectedRow.DocIDKey
select c).SingleOrDefault();
db.Documents.DeleteOnSubmit(matchedDocument);
db.SubmitChanges();
LoadCaseNumberKey(matchedDocument.CaseNumberKey, false, "documents");
this.dataRepeater1.EndResetItemTemplate();
}
}
Any help would be great!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您的
documentsBindingSource
和dataRepeater
混淆了。您直观地“看到”的是 dataRepeater,而您“得到”的是
documentsBindingSource.Current
(您将其检索为SelectedRowView
)它始终是设置为 0 索引。这是一个非常常见的 Winforms 控制陷阱。
My guess is that your are mixed up between your
documentsBindingSource
and yourdataRepeater
.What you "see" visually is the dataRepeater, while what you "get", is the
documentsBindingSource.Current
(that you retrieve as beingSelectedRowView
)which is always set to 0 index. This is an all-too-common Winforms control trap.