[.NET 2 / DataGridView] What method to call for refreshing display when DataSource property was upda

  • Thread starter newsgroup.poster
  • Start date
N

newsgroup.poster

Hello, sorry to ask one more question about the topic 'refreshing
displayed data in a datagridview'.

I'm using DataGridView.DataSource property to bind with a custom
collection, at some place I modify the bound collection and want to
perform an update of the displayed datas in the grid.

Here is a simple c# sample:

//----->8----->8----->8----->8----->8----->8----->8-----
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication3 {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Form form = new Form();
DataGridView grdView = new DataGridView();
Button btnAdd = new Button();
TableLayoutPanel tblLayout = new TableLayoutPanel();
tblLayout.Dock = grdView.Dock = btnAdd.Dock = DockStyle.Fill;
btnAdd.Text = "ADD";
form.Controls.Add(tblLayout);
tblLayout.Controls.Add(grdView, 0, 0);
tblLayout.Controls.Add(btnAdd, 0, 1);

IList<KeyValuePair<Guid, string>> datas = new
List<KeyValuePair<Guid, string>>();
// populate datas
{

for (int i = 0; i < 4; ++i) {
datas.Add(new KeyValuePair<Guid, string>(Guid.NewGuid(), "a
row"));
}
grdView.DataSource = datas;
}

// handle add button click
{
btnAdd.Click += delegate {
datas.Add(new KeyValuePair<Guid, string>(Guid.NewGuid(), "added
row"));
};
}

Application.Run(form);
}
}
}
//----->8----->8----->8----->8----->8----->8----->8-----

My question is: how to refresh displayed data in the datagridview when
click on btnAdd is performed?

I tryied Refresh or some other tricks (found via various web search)
but nothing happen, the only trick that performed somewhat what I
expect was setting the DataSource property to null and back to the
value wich is not acceptable (will lost selected cells, generate index
exception...).

Does anyone can help me with this issue?

Thanks in advance and best regards,

Gauthier Segay
 
G

Guest

Use BindingList<> instead of List<>.
If you what to programmatically change data on an object that is already in
the list, make sure you implement INotifyPropertyChanged in that object.
 
N

newsgroup.poster

So nobody to handle such a question or provide alternative ways?

What is wrong with the question, is it posted in wrong group?

Thanks.

(e-mail address removed) a écrit :
 
N

newsgroup.poster

Hi all,

I've finally found an option by replacing my 'datas' variable such way:

IList<KeyValuePair<Guid, string>> datas = new List<KeyValuePair<Guid,
string>>();

becomes

System.ComponentModel.BindingList<KeyValuePair<Guid, string>> datas =
new System.ComponentModel.BindingList<KeyValuePair<Guid, string>>();

stand-in solution for the problem!

Gauthier Segay

So nobody to handle such a question or provide alternative ways?

What is wrong with the question, is it posted in wrong group?

Thanks.

(e-mail address removed) a écrit :
Hello, sorry to ask one more question about the topic 'refreshing
displayed data in a datagridview'.

I'm using DataGridView.DataSource property to bind with a custom
collection, at some place I modify the bound collection and want to
perform an update of the displayed datas in the grid.

Here is a simple c# sample:

//----->8----->8----->8----->8----->8----->8----->8-----
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication3 {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Form form = new Form();
DataGridView grdView = new DataGridView();
Button btnAdd = new Button();
TableLayoutPanel tblLayout = new TableLayoutPanel();
tblLayout.Dock = grdView.Dock = btnAdd.Dock = DockStyle.Fill;
btnAdd.Text = "ADD";
form.Controls.Add(tblLayout);
tblLayout.Controls.Add(grdView, 0, 0);
tblLayout.Controls.Add(btnAdd, 0, 1);

IList<KeyValuePair<Guid, string>> datas = new
List<KeyValuePair<Guid, string>>();
// populate datas
{

for (int i = 0; i < 4; ++i) {
datas.Add(new KeyValuePair<Guid, string>(Guid.NewGuid(), "a
row"));
}
grdView.DataSource = datas;
}

// handle add button click
{
btnAdd.Click += delegate {
datas.Add(new KeyValuePair<Guid, string>(Guid.NewGuid(), "added
row"));
};
}

Application.Run(form);
}
}
}
//----->8----->8----->8----->8----->8----->8----->8-----

My question is: how to refresh displayed data in the datagridview when
click on btnAdd is performed?

I tryied Refresh or some other tricks (found via various web search)
but nothing happen, the only trick that performed somewhat what I
expect was setting the DataSource property to null and back to the
value wich is not acceptable (will lost selected cells, generate index
exception...).

Does anyone can help me with this issue?

Thanks in advance and best regards,

Gauthier Segay
 
Top