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
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