在我已经消费第一行后,我如何停止触发UserDeletingRow
-event的DataGridView
控件?如何防止datagridview为每个选定的行触发UserDeletingRow事件?
这个网格绑定到我的wcf-webservice中的对象,我想只对所有应该删除的对象执行一次delete方法。
此触发事件处理程序对每个选定行也触发总是“你真的要删除” -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;
}
}
此方法调用确认后的web服务:
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' - 例如? – LukeHennerley 2013-03-18 10:36:36
你可能应该考虑构建一个你想要的行为。 AFAIK不存在多个删除处理的事件。对你来说可能就足够了:1.获取选定行数。 2.在每个事件中减少它并收集可删除的行信息到某个列表中3.在最后一行事件中将它添加到集合中,就像以前的所有事件一样,并立即删除它们调用服务或您需要执行的操作。 – Tigran 2013-03-18 10:38:30
@LukeHennerley:是的,没有。 'Delete_Channels'确认后通过web服务删除所有通道对象。但它需要所有的频道作为'列表',所以我想一次通过所有选定的频道。 –
2013-03-18 10:43:43