Solved

How to refresh a DataGridView?

Posted on 2007-11-13
5
12,529 Views
Last Modified: 2013-11-26
All,
i have a datagridview, the data source of it is a List. what i want to do are:
1. Add new data to the list
2. bind the list to dataGridView
3. refresh the dataGridView
example:
lstGrpMaster.Add(Master1);
dataGridViewMaster.DataSource = _lstGrpMaster;
dataGridViewMaster.Refresh();

the above code does not work. Do you have any idea?
thanks a lot
viola
0
Comment
Question by:viola123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20277760
Hi viola,

Instead of dataGridViewMaster.Refresh(), use dataGridViewMaster.Databind().

Regards,

Wayne
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 20277767
the direct answer to that is to set the DataSource to null before setting it to the actual datasource.
This will force the datagrid to "refresh" itself.

BUT...
If the datagrid have the DataSource property set, changing the datasource will automatically show on the grid.

Try the example bellow:
Drag a DataGridView and a button into an empty form.
Paste the code bellow into the form.

Run the project and click the button, you'll see the rows appearing and I'm not running any refresh code.

            DataTable dt = new DataTable();
            private void Form1_Load(object sender, EventArgs e)
            {
                  
                  dt.Columns.Add("Column1", typeof(int));

                  DataRow nr = dt.NewRow();
                  nr["Column1"] = 1;
                  dt.Rows.Add(nr);

                  nr = dt.NewRow();
                  nr["Column1"] = 2;
                  dt.Rows.Add(nr);

                  this.dataGridView1.DataSource = dt;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                  DataRow nr = dt.NewRow();
                  nr["Column1"] = 1;
                  dt.Rows.Add(nr);
            }
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 20277773
Forgot to ask, is this windows or web?

I assumed it's windows because windows grid is called dataGridView and the web GridView have no Refresh method.
0
 
LVL 6

Author Comment

by:viola123
ID: 20278003
All,
thanks. i figured it out, you have to set the datasource to Null first and then assign the datasource again.

AlexCode: it is windows application.

0
 

Expert Comment

by:Samooramad
ID: 23248163
nevermind, stupid question. I had the dataset size set incorreclty. Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question