Inquiridor
Linq atualização pelo Grid

Pergunta
-
Olá,
Estou carregando um grid com a lista de feriados usando o seguinte código
private void ShowCalendar(PLN_CALENDARIO_ANUAL cal) { //Carregar Feriados List<PLN_FERIADO> listFeriados = (from fer in cal.PLN_FERIADOs where fer.DATA.GetValueOrDefault() != null ? fer.DATA.GetValueOrDefault().Year == dtInicio.Year : false select fer).ToList(); bdsFeriados.DataSource = listFeriados; }
cal = Calendário
PLN_FERIADOs = lista de feriados para aquele calendário
o problema é que quando vou alterar ou incluir um registro no grid o registro é alterado apenas no bdsFeriados.DataSource e não co contexto, assim o comando
this.Validate(); this.bdsFeriados.EndEdit(); ctx.SubmitChanges();
não tem efeito pois não existe alteração no ctx (Contexto) e sim no bdsFeriados.DataSource.
Alguém já usou linq bindado no grid, fazendo alterações no próprio grid?
Riderman
Todas as Respostas
-
ja tentou usar a query linq direto no DataSource do grid ao inves de usar um List ?
private void ShowCalendar(PLN_CALENDARIO_ANUAL cal) { //Carregar Feriados bdsFeriados.DataSource = (from fer in cal.PLN_FERIADOs where fer.DATA.GetValueOrDefault() != null ? fer.DATA.GetValueOrDefault().Year == dtInicio.Year : false select fer); }
de outra formar seria usar o metodo Attach da sua entidade para carregar as alteracoes no DataContextvar feriados = (bdsFeriados.DataSource as List<PLN_FERIADO>);
cal.PLN_FERIADOS.AttachAll(feriados, true);
cal.SubmitChanges();
outra coisa... vi q vc nao usa o DataContext ctx na consulta que usar para carregar o Grid... dessa forma nao teria como ele saber quais os dados a serem atualizados. -
Rui,
da forma abaixo ...
private void ShowCalendar(PLN_CALENDARIO_ANUAL cal) { //Carregar Feriados bdsFeriados.DataSource = (from fer in cal.PLN_FERIADOs where fer.DATA.GetValueOrDefault() != null ? fer.DATA.GetValueOrDefault().Year == dtInicio.Year : false select fer); }
quando altero um registro no grid é chamado o evento RowUpdate do grid e o comando abaixo é executado
this.Validate(); this.bdsFeriados.EndEdit(); ctx.SubmitChanges(); é executado com sucesso para alteração de registros,
porém quando vou incluir tentar incluir um novo registro o botão de addnew está desabilitado do dataNavigator.
Mesmo forçando com o comando AddNew do databinding não consigo incluir um novo registro, pelo contrário,
forçando este comando é dada a exceção abaixo.
{"Item cannot be added to a read-only or fixed-size list."}System.InvalidOperationException: Item cannot be added to a read-only or fixed-size list.
at System.Windows.Forms.BindingSource.AddNew()
at SIG.UI.UserControls.SIGMAIN.uctCalendario.bntImpressao_Click(Object sender, EventArgs e) in D:\Riderman\Workfolder\SIG2005.root\SIG2005\Dados\Developer\SIG2005\SourceCode\SIG.UI\UserControls\SIGMAIN\uctCalendario.cs:line 191
Da segunda forma de salvar é dada a exceção abaixo
Cannot attach an entity that already existsat System.Data.Linq.Table`1.Attach(TEntity entity, Boolean asModified)
at System.Data.Linq.Table`1.AttachAll[TSubEntity](IEnumerable`1 entities, Boolean asModified)
at SIG.UI.UserControls.SIGMAIN.uctCalendario.grvFeriado_RowUpdated(Object sender, RowObjectEventArgs e) in D:\Riderman\Workfolder\SIG2005.root\SIG2005\Dados\Developer\SIG2005\SourceCode\SIG.UI\UserControls\SIGMAIN\uctCalendario.cs:line 411
at DevExpress.XtraGrid.Views.Base.ColumnView.RaiseRowUpdated(RowObjectEventArgs e)
at DevExpress.XtraGrid.Views.Grid.GridView.DevExpress.Data.IDataControllerValidationSupport.OnCurrentRowUpdated(ControllerRowEventArgs e)
at DevExpress.Data.BaseGridController.OnCurrentRowUpdated(Boolean prevEditing, Int32 controllerRow, Object row)
Riderman