How can I refresh c# dataGridView after update ?












21















I have a dataGridView when I click on any row a form is opened to update the row data, but after ending updates the updating form is closed but the dataGridView data is not updated



How can i do that ?










share|improve this question

























  • dataGridView1.Refresh();

    – Gilad Naaman
    Aug 10 '11 at 11:08






  • 2





    Refresh(); is graphics refresh, not data, I made the same mistake! Summary: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

    – Coops
    Aug 16 '11 at 14:35






  • 4





    @amer: You should accept some answers to your questions...

    – Ian
    Aug 16 '11 at 14:35
















21















I have a dataGridView when I click on any row a form is opened to update the row data, but after ending updates the updating form is closed but the dataGridView data is not updated



How can i do that ?










share|improve this question

























  • dataGridView1.Refresh();

    – Gilad Naaman
    Aug 10 '11 at 11:08






  • 2





    Refresh(); is graphics refresh, not data, I made the same mistake! Summary: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

    – Coops
    Aug 16 '11 at 14:35






  • 4





    @amer: You should accept some answers to your questions...

    – Ian
    Aug 16 '11 at 14:35














21












21








21


3






I have a dataGridView when I click on any row a form is opened to update the row data, but after ending updates the updating form is closed but the dataGridView data is not updated



How can i do that ?










share|improve this question
















I have a dataGridView when I click on any row a form is opened to update the row data, but after ending updates the updating form is closed but the dataGridView data is not updated



How can i do that ?







c# datagridview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 10 '11 at 11:05









Can Poyrazoğlu

15k31111236




15k31111236










asked Aug 10 '11 at 9:10









ameramer

2033410




2033410













  • dataGridView1.Refresh();

    – Gilad Naaman
    Aug 10 '11 at 11:08






  • 2





    Refresh(); is graphics refresh, not data, I made the same mistake! Summary: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

    – Coops
    Aug 16 '11 at 14:35






  • 4





    @amer: You should accept some answers to your questions...

    – Ian
    Aug 16 '11 at 14:35



















  • dataGridView1.Refresh();

    – Gilad Naaman
    Aug 10 '11 at 11:08






  • 2





    Refresh(); is graphics refresh, not data, I made the same mistake! Summary: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

    – Coops
    Aug 16 '11 at 14:35






  • 4





    @amer: You should accept some answers to your questions...

    – Ian
    Aug 16 '11 at 14:35

















dataGridView1.Refresh();

– Gilad Naaman
Aug 10 '11 at 11:08





dataGridView1.Refresh();

– Gilad Naaman
Aug 10 '11 at 11:08




2




2





Refresh(); is graphics refresh, not data, I made the same mistake! Summary: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

– Coops
Aug 16 '11 at 14:35





Refresh(); is graphics refresh, not data, I made the same mistake! Summary: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

– Coops
Aug 16 '11 at 14:35




4




4





@amer: You should accept some answers to your questions...

– Ian
Aug 16 '11 at 14:35





@amer: You should accept some answers to your questions...

– Ian
Aug 16 '11 at 14:35












6 Answers
6






active

oldest

votes


















34














BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful.



If your source is say for example a list of user strings



List<string> users = GetUsers();
BindingSource source = new BindingSource();
source.DataSource = users
dataGridView1.Datasource = source;


then when your done editing just update your data object whether that be a DataTable or List of user strings like here and ResetBindings on the BindingSource;



users = GetUsers(); //Update your data object
source.ResetBindings(false);





share|improve this answer


























  • hm, coming over from "simple DataGridView refresh question", this ResetBinding() call still doesn't work with my simple test project ;) Can you give an actual, buildable, working example of this doing the refresh properly?

    – Alan
    Oct 9 '11 at 8:46








  • 2





    ResetBindings seems to work only with .net types. I think if you use a custom collection you need to implement the IBindingList interface, but i haven't tried that yet.

    – Ed Sykes
    Jan 11 '12 at 13:12











  • What do you mean by custom collection?

    – Coops
    Jan 11 '12 at 16:58











  • Haven't tried per @EdSykes IBindingList suggestion, but this does not work with BindingList<T> either unfortunately.

    – Toby
    Sep 20 '17 at 12:21



















31














Rebind your DatagridView to the source.



DataGridView dg1 = new DataGridView();
dg1.DataSource = src1;

// Update Data in src1

dg1.DataSource = null;
dg1.DataSource = src1;





share|improve this answer
























  • where I should put this code in the parent form or child ?

    – amer
    Aug 10 '11 at 10:24











  • This is just an example, you only want the bottom two rows, and you want them after an update. So imagine on the parent, on the child closing event.

    – Ian
    Aug 10 '11 at 10:45











  • This is how I used to do it and it does work fine but I found having a 'BindingSource' with that one 'ResetBindings' method (as per my answer) superb!

    – Coops
    Aug 16 '11 at 14:38











  • @Ian: it might be worth updating your example and mentioning that if one nulls out the DataSource property, then they lose the schema (columns). It's usually better to set it to typeof(ElementType), where ElementType is the type of bound rows.

    – Alan
    Oct 9 '11 at 8:44











  • although it works but you have to resetting some properties of your datagridview such as column header text, column size and visibility of some column such as id and ...

    – Vahid Ghadiri
    Nov 18 '11 at 17:43





















0














I don't know if this has really been solved or not... but by looking at all the other answers, nothing seems quite clear. The best way I found to do this is to put the same code, that was used to populate your datagridview into a method and pass it your form's datagridview, as so:



public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
{ }


The code within the method is the exact same as the code used to populate the datagirdview originally, except for the datagridview name changing to whatever you called it in your method.



Now this method is called in your parent form.



The child form is launched via a .ShowDialog() then the method is called after so that it is called right after the child for is closed... as so:



ChildForm.ShowDialog();

ConnectAndPopulateDataGridView(dataGridView1);





share|improve this answer

































    0














    You can use the DataGridView refresh method. But... in a lot of cases you have to refresh the DataGridView from methods running on a different thread than the one where the DataGridView is running. In order to do that you should implement the following method and call it rather than directly typing DataGridView.Refresh():



        private void RefreshGridView()
    {
    if (dataGridView1.InvokeRequired)
    {
    dataGridView1.Invoke((MethodInvoker)delegate ()
    {
    RefreshGridView();
    });
    }
    else
    dataGridView1.Refresh();
    }





    share|improve this answer































      -1














      You just need to redefine the DataSource. So if you have for example DataGridView's DataSource that contains a, b, i c:



      DataGridView.DataSource = a, b, c


      And suddenly you update the DataSource so you have just a and b, you would need to redefine your DataSource:



      DataGridView.DataSource = a, b


      I hope you find this useful.



      Thank you.






      share|improve this answer































        -1














        You can use SqlDataAdapter to update the DataGridView



             using (SqlConnection conn = new SqlConnection(connectionString))
        {
        using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn))
        {
        DataTable dt = new DataTable();
        ad.Fill(dt);
        dataGridView1.DataSource = dt;
        }
        }





        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%2f7008361%2fhow-can-i-refresh-c-sharp-datagridview-after-update%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          34














          BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful.



          If your source is say for example a list of user strings



          List<string> users = GetUsers();
          BindingSource source = new BindingSource();
          source.DataSource = users
          dataGridView1.Datasource = source;


          then when your done editing just update your data object whether that be a DataTable or List of user strings like here and ResetBindings on the BindingSource;



          users = GetUsers(); //Update your data object
          source.ResetBindings(false);





          share|improve this answer


























          • hm, coming over from "simple DataGridView refresh question", this ResetBinding() call still doesn't work with my simple test project ;) Can you give an actual, buildable, working example of this doing the refresh properly?

            – Alan
            Oct 9 '11 at 8:46








          • 2





            ResetBindings seems to work only with .net types. I think if you use a custom collection you need to implement the IBindingList interface, but i haven't tried that yet.

            – Ed Sykes
            Jan 11 '12 at 13:12











          • What do you mean by custom collection?

            – Coops
            Jan 11 '12 at 16:58











          • Haven't tried per @EdSykes IBindingList suggestion, but this does not work with BindingList<T> either unfortunately.

            – Toby
            Sep 20 '17 at 12:21
















          34














          BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful.



          If your source is say for example a list of user strings



          List<string> users = GetUsers();
          BindingSource source = new BindingSource();
          source.DataSource = users
          dataGridView1.Datasource = source;


          then when your done editing just update your data object whether that be a DataTable or List of user strings like here and ResetBindings on the BindingSource;



          users = GetUsers(); //Update your data object
          source.ResetBindings(false);





          share|improve this answer


























          • hm, coming over from "simple DataGridView refresh question", this ResetBinding() call still doesn't work with my simple test project ;) Can you give an actual, buildable, working example of this doing the refresh properly?

            – Alan
            Oct 9 '11 at 8:46








          • 2





            ResetBindings seems to work only with .net types. I think if you use a custom collection you need to implement the IBindingList interface, but i haven't tried that yet.

            – Ed Sykes
            Jan 11 '12 at 13:12











          • What do you mean by custom collection?

            – Coops
            Jan 11 '12 at 16:58











          • Haven't tried per @EdSykes IBindingList suggestion, but this does not work with BindingList<T> either unfortunately.

            – Toby
            Sep 20 '17 at 12:21














          34












          34








          34







          BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful.



          If your source is say for example a list of user strings



          List<string> users = GetUsers();
          BindingSource source = new BindingSource();
          source.DataSource = users
          dataGridView1.Datasource = source;


          then when your done editing just update your data object whether that be a DataTable or List of user strings like here and ResetBindings on the BindingSource;



          users = GetUsers(); //Update your data object
          source.ResetBindings(false);





          share|improve this answer















          BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful.



          If your source is say for example a list of user strings



          List<string> users = GetUsers();
          BindingSource source = new BindingSource();
          source.DataSource = users
          dataGridView1.Datasource = source;


          then when your done editing just update your data object whether that be a DataTable or List of user strings like here and ResetBindings on the BindingSource;



          users = GetUsers(); //Update your data object
          source.ResetBindings(false);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 18 '15 at 10:11

























          answered Aug 16 '11 at 14:29









          CoopsCoops

          3,05342845




          3,05342845













          • hm, coming over from "simple DataGridView refresh question", this ResetBinding() call still doesn't work with my simple test project ;) Can you give an actual, buildable, working example of this doing the refresh properly?

            – Alan
            Oct 9 '11 at 8:46








          • 2





            ResetBindings seems to work only with .net types. I think if you use a custom collection you need to implement the IBindingList interface, but i haven't tried that yet.

            – Ed Sykes
            Jan 11 '12 at 13:12











          • What do you mean by custom collection?

            – Coops
            Jan 11 '12 at 16:58











          • Haven't tried per @EdSykes IBindingList suggestion, but this does not work with BindingList<T> either unfortunately.

            – Toby
            Sep 20 '17 at 12:21



















          • hm, coming over from "simple DataGridView refresh question", this ResetBinding() call still doesn't work with my simple test project ;) Can you give an actual, buildable, working example of this doing the refresh properly?

            – Alan
            Oct 9 '11 at 8:46








          • 2





            ResetBindings seems to work only with .net types. I think if you use a custom collection you need to implement the IBindingList interface, but i haven't tried that yet.

            – Ed Sykes
            Jan 11 '12 at 13:12











          • What do you mean by custom collection?

            – Coops
            Jan 11 '12 at 16:58











          • Haven't tried per @EdSykes IBindingList suggestion, but this does not work with BindingList<T> either unfortunately.

            – Toby
            Sep 20 '17 at 12:21

















          hm, coming over from "simple DataGridView refresh question", this ResetBinding() call still doesn't work with my simple test project ;) Can you give an actual, buildable, working example of this doing the refresh properly?

          – Alan
          Oct 9 '11 at 8:46







          hm, coming over from "simple DataGridView refresh question", this ResetBinding() call still doesn't work with my simple test project ;) Can you give an actual, buildable, working example of this doing the refresh properly?

          – Alan
          Oct 9 '11 at 8:46






          2




          2





          ResetBindings seems to work only with .net types. I think if you use a custom collection you need to implement the IBindingList interface, but i haven't tried that yet.

          – Ed Sykes
          Jan 11 '12 at 13:12





          ResetBindings seems to work only with .net types. I think if you use a custom collection you need to implement the IBindingList interface, but i haven't tried that yet.

          – Ed Sykes
          Jan 11 '12 at 13:12













          What do you mean by custom collection?

          – Coops
          Jan 11 '12 at 16:58





          What do you mean by custom collection?

          – Coops
          Jan 11 '12 at 16:58













          Haven't tried per @EdSykes IBindingList suggestion, but this does not work with BindingList<T> either unfortunately.

          – Toby
          Sep 20 '17 at 12:21





          Haven't tried per @EdSykes IBindingList suggestion, but this does not work with BindingList<T> either unfortunately.

          – Toby
          Sep 20 '17 at 12:21













          31














          Rebind your DatagridView to the source.



          DataGridView dg1 = new DataGridView();
          dg1.DataSource = src1;

          // Update Data in src1

          dg1.DataSource = null;
          dg1.DataSource = src1;





          share|improve this answer
























          • where I should put this code in the parent form or child ?

            – amer
            Aug 10 '11 at 10:24











          • This is just an example, you only want the bottom two rows, and you want them after an update. So imagine on the parent, on the child closing event.

            – Ian
            Aug 10 '11 at 10:45











          • This is how I used to do it and it does work fine but I found having a 'BindingSource' with that one 'ResetBindings' method (as per my answer) superb!

            – Coops
            Aug 16 '11 at 14:38











          • @Ian: it might be worth updating your example and mentioning that if one nulls out the DataSource property, then they lose the schema (columns). It's usually better to set it to typeof(ElementType), where ElementType is the type of bound rows.

            – Alan
            Oct 9 '11 at 8:44











          • although it works but you have to resetting some properties of your datagridview such as column header text, column size and visibility of some column such as id and ...

            – Vahid Ghadiri
            Nov 18 '11 at 17:43


















          31














          Rebind your DatagridView to the source.



          DataGridView dg1 = new DataGridView();
          dg1.DataSource = src1;

          // Update Data in src1

          dg1.DataSource = null;
          dg1.DataSource = src1;





          share|improve this answer
























          • where I should put this code in the parent form or child ?

            – amer
            Aug 10 '11 at 10:24











          • This is just an example, you only want the bottom two rows, and you want them after an update. So imagine on the parent, on the child closing event.

            – Ian
            Aug 10 '11 at 10:45











          • This is how I used to do it and it does work fine but I found having a 'BindingSource' with that one 'ResetBindings' method (as per my answer) superb!

            – Coops
            Aug 16 '11 at 14:38











          • @Ian: it might be worth updating your example and mentioning that if one nulls out the DataSource property, then they lose the schema (columns). It's usually better to set it to typeof(ElementType), where ElementType is the type of bound rows.

            – Alan
            Oct 9 '11 at 8:44











          • although it works but you have to resetting some properties of your datagridview such as column header text, column size and visibility of some column such as id and ...

            – Vahid Ghadiri
            Nov 18 '11 at 17:43
















          31












          31








          31







          Rebind your DatagridView to the source.



          DataGridView dg1 = new DataGridView();
          dg1.DataSource = src1;

          // Update Data in src1

          dg1.DataSource = null;
          dg1.DataSource = src1;





          share|improve this answer













          Rebind your DatagridView to the source.



          DataGridView dg1 = new DataGridView();
          dg1.DataSource = src1;

          // Update Data in src1

          dg1.DataSource = null;
          dg1.DataSource = src1;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 10 '11 at 10:11









          IanIan

          24.3k1882154




          24.3k1882154













          • where I should put this code in the parent form or child ?

            – amer
            Aug 10 '11 at 10:24











          • This is just an example, you only want the bottom two rows, and you want them after an update. So imagine on the parent, on the child closing event.

            – Ian
            Aug 10 '11 at 10:45











          • This is how I used to do it and it does work fine but I found having a 'BindingSource' with that one 'ResetBindings' method (as per my answer) superb!

            – Coops
            Aug 16 '11 at 14:38











          • @Ian: it might be worth updating your example and mentioning that if one nulls out the DataSource property, then they lose the schema (columns). It's usually better to set it to typeof(ElementType), where ElementType is the type of bound rows.

            – Alan
            Oct 9 '11 at 8:44











          • although it works but you have to resetting some properties of your datagridview such as column header text, column size and visibility of some column such as id and ...

            – Vahid Ghadiri
            Nov 18 '11 at 17:43





















          • where I should put this code in the parent form or child ?

            – amer
            Aug 10 '11 at 10:24











          • This is just an example, you only want the bottom two rows, and you want them after an update. So imagine on the parent, on the child closing event.

            – Ian
            Aug 10 '11 at 10:45











          • This is how I used to do it and it does work fine but I found having a 'BindingSource' with that one 'ResetBindings' method (as per my answer) superb!

            – Coops
            Aug 16 '11 at 14:38











          • @Ian: it might be worth updating your example and mentioning that if one nulls out the DataSource property, then they lose the schema (columns). It's usually better to set it to typeof(ElementType), where ElementType is the type of bound rows.

            – Alan
            Oct 9 '11 at 8:44











          • although it works but you have to resetting some properties of your datagridview such as column header text, column size and visibility of some column such as id and ...

            – Vahid Ghadiri
            Nov 18 '11 at 17:43



















          where I should put this code in the parent form or child ?

          – amer
          Aug 10 '11 at 10:24





          where I should put this code in the parent form or child ?

          – amer
          Aug 10 '11 at 10:24













          This is just an example, you only want the bottom two rows, and you want them after an update. So imagine on the parent, on the child closing event.

          – Ian
          Aug 10 '11 at 10:45





          This is just an example, you only want the bottom two rows, and you want them after an update. So imagine on the parent, on the child closing event.

          – Ian
          Aug 10 '11 at 10:45













          This is how I used to do it and it does work fine but I found having a 'BindingSource' with that one 'ResetBindings' method (as per my answer) superb!

          – Coops
          Aug 16 '11 at 14:38





          This is how I used to do it and it does work fine but I found having a 'BindingSource' with that one 'ResetBindings' method (as per my answer) superb!

          – Coops
          Aug 16 '11 at 14:38













          @Ian: it might be worth updating your example and mentioning that if one nulls out the DataSource property, then they lose the schema (columns). It's usually better to set it to typeof(ElementType), where ElementType is the type of bound rows.

          – Alan
          Oct 9 '11 at 8:44





          @Ian: it might be worth updating your example and mentioning that if one nulls out the DataSource property, then they lose the schema (columns). It's usually better to set it to typeof(ElementType), where ElementType is the type of bound rows.

          – Alan
          Oct 9 '11 at 8:44













          although it works but you have to resetting some properties of your datagridview such as column header text, column size and visibility of some column such as id and ...

          – Vahid Ghadiri
          Nov 18 '11 at 17:43







          although it works but you have to resetting some properties of your datagridview such as column header text, column size and visibility of some column such as id and ...

          – Vahid Ghadiri
          Nov 18 '11 at 17:43













          0














          I don't know if this has really been solved or not... but by looking at all the other answers, nothing seems quite clear. The best way I found to do this is to put the same code, that was used to populate your datagridview into a method and pass it your form's datagridview, as so:



          public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
          { }


          The code within the method is the exact same as the code used to populate the datagirdview originally, except for the datagridview name changing to whatever you called it in your method.



          Now this method is called in your parent form.



          The child form is launched via a .ShowDialog() then the method is called after so that it is called right after the child for is closed... as so:



          ChildForm.ShowDialog();

          ConnectAndPopulateDataGridView(dataGridView1);





          share|improve this answer






























            0














            I don't know if this has really been solved or not... but by looking at all the other answers, nothing seems quite clear. The best way I found to do this is to put the same code, that was used to populate your datagridview into a method and pass it your form's datagridview, as so:



            public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
            { }


            The code within the method is the exact same as the code used to populate the datagirdview originally, except for the datagridview name changing to whatever you called it in your method.



            Now this method is called in your parent form.



            The child form is launched via a .ShowDialog() then the method is called after so that it is called right after the child for is closed... as so:



            ChildForm.ShowDialog();

            ConnectAndPopulateDataGridView(dataGridView1);





            share|improve this answer




























              0












              0








              0







              I don't know if this has really been solved or not... but by looking at all the other answers, nothing seems quite clear. The best way I found to do this is to put the same code, that was used to populate your datagridview into a method and pass it your form's datagridview, as so:



              public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
              { }


              The code within the method is the exact same as the code used to populate the datagirdview originally, except for the datagridview name changing to whatever you called it in your method.



              Now this method is called in your parent form.



              The child form is launched via a .ShowDialog() then the method is called after so that it is called right after the child for is closed... as so:



              ChildForm.ShowDialog();

              ConnectAndPopulateDataGridView(dataGridView1);





              share|improve this answer















              I don't know if this has really been solved or not... but by looking at all the other answers, nothing seems quite clear. The best way I found to do this is to put the same code, that was used to populate your datagridview into a method and pass it your form's datagridview, as so:



              public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
              { }


              The code within the method is the exact same as the code used to populate the datagirdview originally, except for the datagridview name changing to whatever you called it in your method.



              Now this method is called in your parent form.



              The child form is launched via a .ShowDialog() then the method is called after so that it is called right after the child for is closed... as so:



              ChildForm.ShowDialog();

              ConnectAndPopulateDataGridView(dataGridView1);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 24 '16 at 21:36









              Meiko Rachimow

              3,17021333




              3,17021333










              answered Mar 24 '16 at 20:34









              BragonBragon

              64




              64























                  0














                  You can use the DataGridView refresh method. But... in a lot of cases you have to refresh the DataGridView from methods running on a different thread than the one where the DataGridView is running. In order to do that you should implement the following method and call it rather than directly typing DataGridView.Refresh():



                      private void RefreshGridView()
                  {
                  if (dataGridView1.InvokeRequired)
                  {
                  dataGridView1.Invoke((MethodInvoker)delegate ()
                  {
                  RefreshGridView();
                  });
                  }
                  else
                  dataGridView1.Refresh();
                  }





                  share|improve this answer




























                    0














                    You can use the DataGridView refresh method. But... in a lot of cases you have to refresh the DataGridView from methods running on a different thread than the one where the DataGridView is running. In order to do that you should implement the following method and call it rather than directly typing DataGridView.Refresh():



                        private void RefreshGridView()
                    {
                    if (dataGridView1.InvokeRequired)
                    {
                    dataGridView1.Invoke((MethodInvoker)delegate ()
                    {
                    RefreshGridView();
                    });
                    }
                    else
                    dataGridView1.Refresh();
                    }





                    share|improve this answer


























                      0












                      0








                      0







                      You can use the DataGridView refresh method. But... in a lot of cases you have to refresh the DataGridView from methods running on a different thread than the one where the DataGridView is running. In order to do that you should implement the following method and call it rather than directly typing DataGridView.Refresh():



                          private void RefreshGridView()
                      {
                      if (dataGridView1.InvokeRequired)
                      {
                      dataGridView1.Invoke((MethodInvoker)delegate ()
                      {
                      RefreshGridView();
                      });
                      }
                      else
                      dataGridView1.Refresh();
                      }





                      share|improve this answer













                      You can use the DataGridView refresh method. But... in a lot of cases you have to refresh the DataGridView from methods running on a different thread than the one where the DataGridView is running. In order to do that you should implement the following method and call it rather than directly typing DataGridView.Refresh():



                          private void RefreshGridView()
                      {
                      if (dataGridView1.InvokeRequired)
                      {
                      dataGridView1.Invoke((MethodInvoker)delegate ()
                      {
                      RefreshGridView();
                      });
                      }
                      else
                      dataGridView1.Refresh();
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 22:22









                      Alon AxelradAlon Axelrad

                      11




                      11























                          -1














                          You just need to redefine the DataSource. So if you have for example DataGridView's DataSource that contains a, b, i c:



                          DataGridView.DataSource = a, b, c


                          And suddenly you update the DataSource so you have just a and b, you would need to redefine your DataSource:



                          DataGridView.DataSource = a, b


                          I hope you find this useful.



                          Thank you.






                          share|improve this answer




























                            -1














                            You just need to redefine the DataSource. So if you have for example DataGridView's DataSource that contains a, b, i c:



                            DataGridView.DataSource = a, b, c


                            And suddenly you update the DataSource so you have just a and b, you would need to redefine your DataSource:



                            DataGridView.DataSource = a, b


                            I hope you find this useful.



                            Thank you.






                            share|improve this answer


























                              -1












                              -1








                              -1







                              You just need to redefine the DataSource. So if you have for example DataGridView's DataSource that contains a, b, i c:



                              DataGridView.DataSource = a, b, c


                              And suddenly you update the DataSource so you have just a and b, you would need to redefine your DataSource:



                              DataGridView.DataSource = a, b


                              I hope you find this useful.



                              Thank you.






                              share|improve this answer













                              You just need to redefine the DataSource. So if you have for example DataGridView's DataSource that contains a, b, i c:



                              DataGridView.DataSource = a, b, c


                              And suddenly you update the DataSource so you have just a and b, you would need to redefine your DataSource:



                              DataGridView.DataSource = a, b


                              I hope you find this useful.



                              Thank you.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 16 '14 at 13:14









                              miksiiimiksiii

                              1,8911919




                              1,8911919























                                  -1














                                  You can use SqlDataAdapter to update the DataGridView



                                       using (SqlConnection conn = new SqlConnection(connectionString))
                                  {
                                  using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn))
                                  {
                                  DataTable dt = new DataTable();
                                  ad.Fill(dt);
                                  dataGridView1.DataSource = dt;
                                  }
                                  }





                                  share|improve this answer






























                                    -1














                                    You can use SqlDataAdapter to update the DataGridView



                                         using (SqlConnection conn = new SqlConnection(connectionString))
                                    {
                                    using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn))
                                    {
                                    DataTable dt = new DataTable();
                                    ad.Fill(dt);
                                    dataGridView1.DataSource = dt;
                                    }
                                    }





                                    share|improve this answer




























                                      -1












                                      -1








                                      -1







                                      You can use SqlDataAdapter to update the DataGridView



                                           using (SqlConnection conn = new SqlConnection(connectionString))
                                      {
                                      using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn))
                                      {
                                      DataTable dt = new DataTable();
                                      ad.Fill(dt);
                                      dataGridView1.DataSource = dt;
                                      }
                                      }





                                      share|improve this answer















                                      You can use SqlDataAdapter to update the DataGridView



                                           using (SqlConnection conn = new SqlConnection(connectionString))
                                      {
                                      using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn))
                                      {
                                      DataTable dt = new DataTable();
                                      ad.Fill(dt);
                                      dataGridView1.DataSource = dt;
                                      }
                                      }






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Apr 1 '16 at 11:23

























                                      answered Apr 1 '16 at 11:15









                                      Ilciuc SergiuIlciuc Sergiu

                                      12




                                      12






























                                          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%2f7008361%2fhow-can-i-refresh-c-sharp-datagridview-after-update%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

                                          404 Error Contact Form 7 ajax form submitting

                                          How to know if a Active Directory user can login interactively

                                          TypeError: fit_transform() missing 1 required positional argument: 'X'