Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Issues with DataGridView in VirtualMode

$
0
0

Hi

I created a very simple windows form application in VS 2010. The code is as follow:

using System.Linq;
using System.Windows.Forms;

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.VirtualMode = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<TestClass> t = new List<TestClass> { new TestClass("Some Text"), new TestClass("Another Text") };

            bindindSource.DataSource = t;
        }


        private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            if (e.ColumnIndex == dgvcUnbound.Index)
                e.Value = ((TestClass)dataGridView1.Rows[e.RowIndex].DataBoundItem).Value.ToUpper();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<TestClass> t = (List<TestClass>)bindindSource.DataSource;
            t.First().Value = "Text Changed";
        }
    }

    public class TestClass : INotifyPropertyChanged
    {
        public TestClass(string value)
        {
            Value = value;
        }

        private string _value;
        public string Value { get { return _value; } set { _value = value; OnPropertChanged("Value"); } }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

As  seen, the form contains a DataGridView bound to a generic List of TestClass type. The DataGridView has a column bound to the Value property of TestClass and another unbound column, named dgvcUnbound. The DataGridView is set to VirtualMode and the CellValueNeeded event is handled in order to provide values for the unbound column. A button is provided in order to change some values of the data source at run time.

When the form is loaded, the grid displays values as expected, but, when the button is clicked and the Value property of the first data source element is modified, the grid updates the value of the bound column but the unbound column remains unchanged.

I have checked that the CellValueNeeded event is fired and  a value for the unbound cell is properly assigned in the event handler, but the DataGridView does not updates the cell value.

What am I missing, please?

Regards


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>