【发布时间】:2013-03-18 10:32:27
【问题描述】:
在我已经使用第一行之后,如何阻止DataGridView 控件为每个选定行触发UserDeletingRow-事件?
这个网格绑定到我的 wcf-webservice 中的对象,我只想对所有应该删除的对象执行一次删除方法。
这个事件处理程序为每个选定的行触发,它也总是触发“你真的要删除吗”-messageboxes:
private void Grid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
switch (this.Type)
{
case AdminType.Channel:
List<Channel> channels = GrdChannel.SelectedRows.Cast<DataGridViewRow>()
.Select(row => (Channel)row.DataBoundItem).ToList();
e.Cancel = !Delete_Channels(channels);
break;
// other types ...
default:
break;
}
}
此方法确认后调用webservice:
private bool Delete_Channels(List<Channel> channels)
{
var msg = string.Format("Do you really want to delete {0}?", channels.Count == 1 ? "this channel" : "these channels");
var title = channels.Count == 1 ? "Delete channel" : "Delete channels";
bool yes = MessageBox.Show(msg, title, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes;
if (yes)
{
using (var db = new ERP_ServiceClient())
db.DeleteChannels(channels, this.IdUser);
string message = string.Format("{0} deleted successfully: {1}"
, channels.Count == 1 ? "Channel" : "Channels"
, string.Join(",", channels.Select(p => p.Name)));
channelBindingSource.Remove(channels);
Main.ShowStatusMessage(message);
}
return yes;
}
【问题讨论】:
-
所以
Delete_Channels等处理您的删除过程,但您想将其与Delete_Objects结合起来 - 例如? -
您可能应该考虑构建您想要的行为。 AFAIK 没有任何事件可用于多次删除处理。可能对您来说就足够了: 1. 获取选定行的数量。 2.在每个事件中减少它并将可删除的行信息收集到某个列表中 3.在最后一行事件中将其添加到集合中,就像所有以前的事件一样,并立即删除它们,调用服务或您需要做的方式。跨度>
-
@LukeHennerley:是的,也不是。
Delete_Channels确认后通过网络服务删除所有频道注入。但是它将所有频道都作为List<Channel>,所以我想一次通过所有选定的频道。 -
@Tigran:我很惊讶 winforms 如此繁琐(我的 winforms 经验很生疏)。我希望会有一些内置的或更强大的东西。
-
@Tigran:添加了我对您建议的实施,它似乎运作良好。谢谢:)
标签: c# winforms data-binding datagridview event-handling