WPF DataGrid can't be refreshed after cell edit










0















I searched several times about datagrid refresh problem. But can't get any idea about it. My problem is as below. Bascially it can work.
But if user sort columns and then edit cell, datagrid can't bind. VS would list error ""Once AddNew or EditItem not allow “Sorting”。"."



But it's friendly for user to be allowed to sort datagrid columns. Please help me to address this problem.Thanks!



private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)


string sql="update table1 set field1='"+cell_value+''
BindGR();




private void BindGR()


//Bind DataGrid
if (dt != null)

DataGrid1.ItemsSource = dt.DefaultView;













share|improve this question
























  • Please share more code, like the source of the list how it is being sorted and all.

    – ViVi
    May 1 '16 at 3:28











  • Thanks. My list is "select id, job_name, job_type,job_status from F_job " executed in MS SQL. And nothing changed after I amended SQL . So I think it should be wrong with DataGrid rendering problem. When I end cell editing, DataGrid begin rendering. At that time, I can't bind data to it.

    – Leon Sun
    May 2 '16 at 2:02











  • There is a sorting event for datagrid. Handle that event and try to update the data in your source.

    – ViVi
    May 2 '16 at 7:59











  • Could you please guide me how to handle this event? thanks

    – Leon Sun
    May 5 '16 at 10:47











  • Ok I will post the code now. Please check and revert if any issues.

    – ViVi
    May 5 '16 at 17:58















0















I searched several times about datagrid refresh problem. But can't get any idea about it. My problem is as below. Bascially it can work.
But if user sort columns and then edit cell, datagrid can't bind. VS would list error ""Once AddNew or EditItem not allow “Sorting”。"."



But it's friendly for user to be allowed to sort datagrid columns. Please help me to address this problem.Thanks!



private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)


string sql="update table1 set field1='"+cell_value+''
BindGR();




private void BindGR()


//Bind DataGrid
if (dt != null)

DataGrid1.ItemsSource = dt.DefaultView;













share|improve this question
























  • Please share more code, like the source of the list how it is being sorted and all.

    – ViVi
    May 1 '16 at 3:28











  • Thanks. My list is "select id, job_name, job_type,job_status from F_job " executed in MS SQL. And nothing changed after I amended SQL . So I think it should be wrong with DataGrid rendering problem. When I end cell editing, DataGrid begin rendering. At that time, I can't bind data to it.

    – Leon Sun
    May 2 '16 at 2:02











  • There is a sorting event for datagrid. Handle that event and try to update the data in your source.

    – ViVi
    May 2 '16 at 7:59











  • Could you please guide me how to handle this event? thanks

    – Leon Sun
    May 5 '16 at 10:47











  • Ok I will post the code now. Please check and revert if any issues.

    – ViVi
    May 5 '16 at 17:58













0












0








0








I searched several times about datagrid refresh problem. But can't get any idea about it. My problem is as below. Bascially it can work.
But if user sort columns and then edit cell, datagrid can't bind. VS would list error ""Once AddNew or EditItem not allow “Sorting”。"."



But it's friendly for user to be allowed to sort datagrid columns. Please help me to address this problem.Thanks!



private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)


string sql="update table1 set field1='"+cell_value+''
BindGR();




private void BindGR()


//Bind DataGrid
if (dt != null)

DataGrid1.ItemsSource = dt.DefaultView;













share|improve this question
















I searched several times about datagrid refresh problem. But can't get any idea about it. My problem is as below. Bascially it can work.
But if user sort columns and then edit cell, datagrid can't bind. VS would list error ""Once AddNew or EditItem not allow “Sorting”。"."



But it's friendly for user to be allowed to sort datagrid columns. Please help me to address this problem.Thanks!



private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)


string sql="update table1 set field1='"+cell_value+''
BindGR();




private void BindGR()


//Bind DataGrid
if (dt != null)

DataGrid1.ItemsSource = dt.DefaultView;










c# wpf data-binding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 1 '16 at 7:37









AnjumSKhan

8,03111530




8,03111530










asked May 1 '16 at 2:00









Leon SunLeon Sun

12




12












  • Please share more code, like the source of the list how it is being sorted and all.

    – ViVi
    May 1 '16 at 3:28











  • Thanks. My list is "select id, job_name, job_type,job_status from F_job " executed in MS SQL. And nothing changed after I amended SQL . So I think it should be wrong with DataGrid rendering problem. When I end cell editing, DataGrid begin rendering. At that time, I can't bind data to it.

    – Leon Sun
    May 2 '16 at 2:02











  • There is a sorting event for datagrid. Handle that event and try to update the data in your source.

    – ViVi
    May 2 '16 at 7:59











  • Could you please guide me how to handle this event? thanks

    – Leon Sun
    May 5 '16 at 10:47











  • Ok I will post the code now. Please check and revert if any issues.

    – ViVi
    May 5 '16 at 17:58

















  • Please share more code, like the source of the list how it is being sorted and all.

    – ViVi
    May 1 '16 at 3:28











  • Thanks. My list is "select id, job_name, job_type,job_status from F_job " executed in MS SQL. And nothing changed after I amended SQL . So I think it should be wrong with DataGrid rendering problem. When I end cell editing, DataGrid begin rendering. At that time, I can't bind data to it.

    – Leon Sun
    May 2 '16 at 2:02











  • There is a sorting event for datagrid. Handle that event and try to update the data in your source.

    – ViVi
    May 2 '16 at 7:59











  • Could you please guide me how to handle this event? thanks

    – Leon Sun
    May 5 '16 at 10:47











  • Ok I will post the code now. Please check and revert if any issues.

    – ViVi
    May 5 '16 at 17:58
















Please share more code, like the source of the list how it is being sorted and all.

– ViVi
May 1 '16 at 3:28





Please share more code, like the source of the list how it is being sorted and all.

– ViVi
May 1 '16 at 3:28













Thanks. My list is "select id, job_name, job_type,job_status from F_job " executed in MS SQL. And nothing changed after I amended SQL . So I think it should be wrong with DataGrid rendering problem. When I end cell editing, DataGrid begin rendering. At that time, I can't bind data to it.

– Leon Sun
May 2 '16 at 2:02





Thanks. My list is "select id, job_name, job_type,job_status from F_job " executed in MS SQL. And nothing changed after I amended SQL . So I think it should be wrong with DataGrid rendering problem. When I end cell editing, DataGrid begin rendering. At that time, I can't bind data to it.

– Leon Sun
May 2 '16 at 2:02













There is a sorting event for datagrid. Handle that event and try to update the data in your source.

– ViVi
May 2 '16 at 7:59





There is a sorting event for datagrid. Handle that event and try to update the data in your source.

– ViVi
May 2 '16 at 7:59













Could you please guide me how to handle this event? thanks

– Leon Sun
May 5 '16 at 10:47





Could you please guide me how to handle this event? thanks

– Leon Sun
May 5 '16 at 10:47













Ok I will post the code now. Please check and revert if any issues.

– ViVi
May 5 '16 at 17:58





Ok I will post the code now. Please check and revert if any issues.

– ViVi
May 5 '16 at 17:58












2 Answers
2






active

oldest

votes


















0














View.xaml



 <DataGrid x:Name="MyDataGrid" 
Sorting="DataGrid_OnSorting"
ItemsSource="Binding DataGridItems, Mode="TwoWay"">
</DataGrid>


View.xaml.cs



 private ObservableCollection<DataClass> dataGridItems = new ObservableCollection<DataClass>();

public ObservableCollection<DataClass> DataGridItems

get return dataGridItems;
set SetProperty(ref dataGridItems, value);



Please try with TwoWay binding ie Mode="TwoWay" for Itemssource



If it doesn't work, try code below. I did not test the code, since it is not fully implemented. Try to get an idea from this!



private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)

var sortedGrid = sender as DataGrid;
foreach (var item in sortedGrid.Items)

DataGridItems.Add(item);







share|improve this answer























  • Wow, thanks a lot for your code. I tested them yesterday. Though they doesn't work, you give me some new idea. I would share it with you after debugging successfully. On the other side, I wonder why the problem happens.

    – Leon Sun
    May 7 '16 at 0:11












  • Yea, they might not work since I could not test it and it's not fully implemented version for me. So it's the basic idea to update list while sorting. Try it and let me know!

    – ViVi
    May 7 '16 at 3:31


















0














Item source update on RowEditEnding event. use RowEditEnding event instead of CellEndEditing and on the column binding use UpdateSourceTrigger=LostFocus.



<DataGridTextColumn Binding="Binding bindedProperty,StringFormat=n0 , Mode=TwoWay , UpdateSourceTrigger=LostFocus"></DataGridTextColumn>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36962256%2fwpf-datagrid-cant-be-refreshed-after-cell-edit%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    View.xaml



     <DataGrid x:Name="MyDataGrid" 
    Sorting="DataGrid_OnSorting"
    ItemsSource="Binding DataGridItems, Mode="TwoWay"">
    </DataGrid>


    View.xaml.cs



     private ObservableCollection<DataClass> dataGridItems = new ObservableCollection<DataClass>();

    public ObservableCollection<DataClass> DataGridItems

    get return dataGridItems;
    set SetProperty(ref dataGridItems, value);



    Please try with TwoWay binding ie Mode="TwoWay" for Itemssource



    If it doesn't work, try code below. I did not test the code, since it is not fully implemented. Try to get an idea from this!



    private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)

    var sortedGrid = sender as DataGrid;
    foreach (var item in sortedGrid.Items)

    DataGridItems.Add(item);







    share|improve this answer























    • Wow, thanks a lot for your code. I tested them yesterday. Though they doesn't work, you give me some new idea. I would share it with you after debugging successfully. On the other side, I wonder why the problem happens.

      – Leon Sun
      May 7 '16 at 0:11












    • Yea, they might not work since I could not test it and it's not fully implemented version for me. So it's the basic idea to update list while sorting. Try it and let me know!

      – ViVi
      May 7 '16 at 3:31















    0














    View.xaml



     <DataGrid x:Name="MyDataGrid" 
    Sorting="DataGrid_OnSorting"
    ItemsSource="Binding DataGridItems, Mode="TwoWay"">
    </DataGrid>


    View.xaml.cs



     private ObservableCollection<DataClass> dataGridItems = new ObservableCollection<DataClass>();

    public ObservableCollection<DataClass> DataGridItems

    get return dataGridItems;
    set SetProperty(ref dataGridItems, value);



    Please try with TwoWay binding ie Mode="TwoWay" for Itemssource



    If it doesn't work, try code below. I did not test the code, since it is not fully implemented. Try to get an idea from this!



    private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)

    var sortedGrid = sender as DataGrid;
    foreach (var item in sortedGrid.Items)

    DataGridItems.Add(item);







    share|improve this answer























    • Wow, thanks a lot for your code. I tested them yesterday. Though they doesn't work, you give me some new idea. I would share it with you after debugging successfully. On the other side, I wonder why the problem happens.

      – Leon Sun
      May 7 '16 at 0:11












    • Yea, they might not work since I could not test it and it's not fully implemented version for me. So it's the basic idea to update list while sorting. Try it and let me know!

      – ViVi
      May 7 '16 at 3:31













    0












    0








    0







    View.xaml



     <DataGrid x:Name="MyDataGrid" 
    Sorting="DataGrid_OnSorting"
    ItemsSource="Binding DataGridItems, Mode="TwoWay"">
    </DataGrid>


    View.xaml.cs



     private ObservableCollection<DataClass> dataGridItems = new ObservableCollection<DataClass>();

    public ObservableCollection<DataClass> DataGridItems

    get return dataGridItems;
    set SetProperty(ref dataGridItems, value);



    Please try with TwoWay binding ie Mode="TwoWay" for Itemssource



    If it doesn't work, try code below. I did not test the code, since it is not fully implemented. Try to get an idea from this!



    private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)

    var sortedGrid = sender as DataGrid;
    foreach (var item in sortedGrid.Items)

    DataGridItems.Add(item);







    share|improve this answer













    View.xaml



     <DataGrid x:Name="MyDataGrid" 
    Sorting="DataGrid_OnSorting"
    ItemsSource="Binding DataGridItems, Mode="TwoWay"">
    </DataGrid>


    View.xaml.cs



     private ObservableCollection<DataClass> dataGridItems = new ObservableCollection<DataClass>();

    public ObservableCollection<DataClass> DataGridItems

    get return dataGridItems;
    set SetProperty(ref dataGridItems, value);



    Please try with TwoWay binding ie Mode="TwoWay" for Itemssource



    If it doesn't work, try code below. I did not test the code, since it is not fully implemented. Try to get an idea from this!



    private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)

    var sortedGrid = sender as DataGrid;
    foreach (var item in sortedGrid.Items)

    DataGridItems.Add(item);








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 5 '16 at 18:07









    ViViViVi

    3,55061941




    3,55061941












    • Wow, thanks a lot for your code. I tested them yesterday. Though they doesn't work, you give me some new idea. I would share it with you after debugging successfully. On the other side, I wonder why the problem happens.

      – Leon Sun
      May 7 '16 at 0:11












    • Yea, they might not work since I could not test it and it's not fully implemented version for me. So it's the basic idea to update list while sorting. Try it and let me know!

      – ViVi
      May 7 '16 at 3:31

















    • Wow, thanks a lot for your code. I tested them yesterday. Though they doesn't work, you give me some new idea. I would share it with you after debugging successfully. On the other side, I wonder why the problem happens.

      – Leon Sun
      May 7 '16 at 0:11












    • Yea, they might not work since I could not test it and it's not fully implemented version for me. So it's the basic idea to update list while sorting. Try it and let me know!

      – ViVi
      May 7 '16 at 3:31
















    Wow, thanks a lot for your code. I tested them yesterday. Though they doesn't work, you give me some new idea. I would share it with you after debugging successfully. On the other side, I wonder why the problem happens.

    – Leon Sun
    May 7 '16 at 0:11






    Wow, thanks a lot for your code. I tested them yesterday. Though they doesn't work, you give me some new idea. I would share it with you after debugging successfully. On the other side, I wonder why the problem happens.

    – Leon Sun
    May 7 '16 at 0:11














    Yea, they might not work since I could not test it and it's not fully implemented version for me. So it's the basic idea to update list while sorting. Try it and let me know!

    – ViVi
    May 7 '16 at 3:31





    Yea, they might not work since I could not test it and it's not fully implemented version for me. So it's the basic idea to update list while sorting. Try it and let me know!

    – ViVi
    May 7 '16 at 3:31













    0














    Item source update on RowEditEnding event. use RowEditEnding event instead of CellEndEditing and on the column binding use UpdateSourceTrigger=LostFocus.



    <DataGridTextColumn Binding="Binding bindedProperty,StringFormat=n0 , Mode=TwoWay , UpdateSourceTrigger=LostFocus"></DataGridTextColumn>





    share|improve this answer



























      0














      Item source update on RowEditEnding event. use RowEditEnding event instead of CellEndEditing and on the column binding use UpdateSourceTrigger=LostFocus.



      <DataGridTextColumn Binding="Binding bindedProperty,StringFormat=n0 , Mode=TwoWay , UpdateSourceTrigger=LostFocus"></DataGridTextColumn>





      share|improve this answer

























        0












        0








        0







        Item source update on RowEditEnding event. use RowEditEnding event instead of CellEndEditing and on the column binding use UpdateSourceTrigger=LostFocus.



        <DataGridTextColumn Binding="Binding bindedProperty,StringFormat=n0 , Mode=TwoWay , UpdateSourceTrigger=LostFocus"></DataGridTextColumn>





        share|improve this answer













        Item source update on RowEditEnding event. use RowEditEnding event instead of CellEndEditing and on the column binding use UpdateSourceTrigger=LostFocus.



        <DataGridTextColumn Binding="Binding bindedProperty,StringFormat=n0 , Mode=TwoWay , UpdateSourceTrigger=LostFocus"></DataGridTextColumn>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 5:09









        mehdimehdi

        19527




        19527



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36962256%2fwpf-datagrid-cant-be-refreshed-after-cell-edit%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo