I have this grid below
ig:WebDataGrid StyleSetName="Office2007Black" ID="WebDataGridNotas" runat="server" DataSourceID="AccessDataSourceNotas" ItemCssClass="borderClass" Height="425px" Width="100%" AutoGenerateColumns="False" DataKeyFields="CodNotaMensal"
And I have the DataSource with a SelectCommand and an UpdateCommand that works fine when I am updating rows using client-side editing.
But when the user select a new value in a DropDownBox, I use this value to change the SelectCommand (just the WHERE clause) using codebehind triggered by the Change event of that DropDownBox. Then when I try to edit the same row I receive the "Requested record cannot be found by key"
Here is the CodeBehind
protected void WebDropDownDisciplina_SelectionChanged(object sender, Infragistics.Web.UI.ListControls.DropDownSelectionChangedEventArgs e) { notas.etapa = Convert.ToInt32(WebDropDownEtapa.SelectedValue); notas.codUnidade = Convert.ToInt32(WebDropDownUnidade.SelectedValue); notas.codGrau = Convert.ToInt32(WebDropDownGrau.SelectedValue); notas.codSerie = Convert.ToString(WebDropDownSerie.SelectedValue); notas.codTurma = Convert.ToString(WebDropDownTurma.SelectedValue); notas.codDisciplina = Convert.ToInt32(WebDropDownDisciplina.SelectedValue); notas.matriculaProfessor = Convert.ToInt32(WebDropDownProfessor.SelectedValue); string selectCommand = "SELECT * FROM [NotasAvaliacaoSelect" + notas.etapa + "] WHERE ([NOTA-MENSAL].CodUnidade =" + notas.codUnidade + ") AND ([NOTA-MENSAL].CodGrau=" + notas.codGrau + ") AND ([NOTA-MENSAL].CodSérie='" + notas.codSerie + "') AND ([NOTA-MENSAL].Turma='" + notas.codTurma + "') AND ([NOTA-MENSAL].CodDisciplina=" + notas.codDisciplina + ") AND ([AULA].MatrículaProfessor=" + notas.matriculaProfessor + ") ORDER BY [NúmeroPauta]"; //string updateCommand = "UPDATE [NotasAvaliacaoUpdate" + notas.etapa + "] SET Faltas = ?, Avaliação1 = ?, Avaliação2 = ?, Avaliação3 = ?, Avaliação4 = ?, Avaliação5 = ?, Avaliação6 = ?, Avaliação7 = ?, Avaliação8 = ?, Avaliação9 = ?, Avaliação10 = ? WHERE (CodNotaMensal = ?)"; AccessDataSourceNotas.SelectCommand = selectCommand; //AccessDataSourceNotas.UpdateCommand = updateCommand; //AccessDataSourceNotas.DataBind(); WebDataGridNotas.ClearDataSource(); WebDataGridNotas.DataSourceID = AccessDataSourceNotas.ID; WebDataGridNotas.DataKeyFields = "CodNotaMensal"; WebDataGridNotas.DataBind(); }
Hello Edson,
Thank you for posting in our forum.
Are you updating any of the values in the grid before you change the data source?
Make sure that all changes have been committed before the data source is changed otherwise the grid will not be able to commit the changes since the rows in it will be changed before the updating events have taken place .
Check of the selection changes is fired before the RowUpdating event and if it does then either:
1) Change the data source later in the page lifecycle (for example on Page_PreRender). In that way all the changes will be committed during the RowUpdating/ed events and then only then the data source will be changed.
2) Commit the changes manually for example on ExitedEditMode client side event of the cell. In that case the changes would be directly committed to the data source via an ajax callback when you exit edit mode of a cell whose value has been changed. You can manually commit the data in the grid using the commit method: grid.get_behaviors().get_editingCore().commit();
Let me know if you have any questions or concerns.
Best Regards,
Maya Kirova
Developer Support Engineer II
Infragistics, Inc.
http://es.infragistics.com/support
Hi Maya,
Thanks for your reply! I found a solution searching the web and I wrote the following code and it works fine, but actually I didn't understand why, please take a look at this.
The events occur in this sequence:
1) The user select a value using a dropdownbox
2) The SelectionChanged event is fired and the code below is executed: string selectCommand = "some SQL SELECT statement"; string updateCommand = "some SQL UPDATE statement"; AccessDataSourceNotas.SelectCommand = selectCommand; AccessDataSourceNotas.UpdateCommand = updateCommand; AccessDataSourceNotas.DataBind(); WebDataGridNotas.ClearDataSource(); WebDataGridNotas.DataSourceID = AccessDataSourceNotas.ID; WebDataGridNotas.DataBind();
3) The WebDataGrid displays the correct data
4) The user edit a cell and press Enter and nothing happens.
5) The user press Down Arrow and now the Page_Load is fired and the follow code is executed if (WebDataGridNotas.RunBot.IsCallback) { string selectCommand = "some SQL SELECT statement"; string updateCommand = "some SQL UPDATE statement"; AccessDataSourceNotas.SelectCommand = selectCommand; AccessDataSourceNotas.UpdateCommand = updateCommand; AccessDataSourceNotas.DataBind(); WebDataGridNotas.ClearDataSource(); WebDataGridNotas.DataSourceID = AccessDataSourceNotas.ID; WebDataGridNotas.DataBind(); }
6) Now the RowUpdatingEvent is fired but I am not using it
I didn't understand Why I have to set the select and update Commands again!
Can you explain that?
Is there a better way or this is fine?
Thanks !
Edson
Hello Edson ,
I’m just following up to see if you’ve been able to resolve your issue. If you have any questions or concerns or if you need further assistance please let me know.
Developer Support Engineer
You should set the select and update statement on each postback if there’re no such set in the mark-up. If you’re changing them on some event( in this case SelectionChanged) then you should make sure that on the next postback the select and update statements are set and match the ones you’ve set on the SelectionChanged event.
Basically the grid need to have a data source set for it no later than Page_load or all CRUD operations would fails. You set a Access data source for it but if that data source has no select and update statements and you bind the grid to it , then the grid would be empty. It will then attempt to find a record that has been passed from the client as updated in the grid’s rows collection but since now the grid’s data is empty it will throw an error that there’s no such record in the grid(”Requested record cannot be found by key”).
Let me know if you have any questions or concerns on this.