2

Let's say I got a DataTable with columns A,B,C,D,E.

I got a DataGridView that works in virtual mode because I had to add a column that is unbound with DataTable. This additional column is a combobox that combines columns A,B,D values into one, and after user selects an item, than I'd like to read this selected item value for further processing. In method DataGridView1_ValueChanged I try to read that value by:

DataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString()

and compiler shows exception that I should use operator new for initiating the object. So, seems like I see new row is added visually but I can't access it.

I got stucked in here :(

4

1 回答 1

0

我想你在问为什么你不能编译这样的东西:

using System.Data;
using System.Windows.Forms;

namespace ZZZ
{
    class Program
    {
        private DataTable _table;
        private readonly DataGridView _view = new DataGridView();

        public void Setup()
        {
            CreateTable();
            _view.CellValueChanged += CellValueChanged;
        }

        private void CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            _view[e.ColumnIndex, e.RowIndex].Value.ToString();
        }

        private void CreateTable()
        {
            _table = new DataTable("Table");
            _table.Columns.Add("A");
            _table.Columns.Add("B");
            _table.Columns.Add("C");
            _table.Columns.Add("D");
            _table.Columns.Add("E");
            _table.Columns.Add("Combined");
        }

        static void Main(string[] args)
        {
            var p = new Program();
            p.Setup();
        }
    }
}

这看起来很奇怪。你应该能够编译它。

于 2011-02-28T20:09:18.993 回答