日期:2014-05-18  浏览次数:20963 次

这段代码为什么不行?
简单说明一下,DataGridView的数据设置为一个BingList<A> 类型,A中两个字段,我希望第二个字段从下拉列表中选择,
请看下面代码:
C# code


namespace TestWinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindingList<A> datasource = new BindingList<A>();

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = datasource;
            DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
            dataGridView1.Columns.Add(c);
            c.DataPropertyName = "FieldA";
            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            listBox1.DataSource = dataGridView1.DataSource;
            combo.Items.AddRange(
                new Pack[] { 
                   new Pack(){
                        Value= new B(){Field1="F10",Field2="F20"}
                  
                   },
                   new Pack(){
                        Value=  new B(){Field1="F11",Field2="F21"}
                   }

                });
            combo.DisplayMember = "Name";
            combo.ValueMember = "Value";
            combo.ValueType = typeof(B);
            //如果注释掉下面这行,不会报错,但是值也当然不会设置到数据源中去
            combo.DataPropertyName = "B";
            dataGridView1.Columns.Add(combo);
            dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                DataGridViewTextBoxCell tc = dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewTextBoxCell;
                Console.WriteLine(tc.Value.ToString());
            }
            else if (e.ColumnIndex == 1)
            {
                DataGridViewComboBoxCell bc = dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewComboBoxCell;
                Console.WriteLine(bc.Value.ToString());
            
            }
            Console.WriteLine();
            Console.WriteLine("==dataGridView1.DataSource==Begin==");
            BindingList<A> list = dataGridView1.DataSource as BindingList<A>;
            foreach (A a in list)
            {
                Console.WriteLine(a);
            }
            Console.WriteLine("==dataGridView1.DataSource==End===");

        }
    }
    public class A
    {
        public A()
        {
            B = new B() { Field1 = "default1", Field2 = "default2" };
        }
        public String FieldA { get; set; }
        public B B { get; set; }
        public override string ToString()
        {
            return FieldA + " " + B.ToString();
        }
    }
    public class B
    {
        public String Field1 { get; set; }
        public String Field2 { get; set; }
        public override string ToString()
        {
            return Field1 + " " + Field2;
        }
    }
    public class Pack
    {
        public String Name { get { return Value.Field1; } }
        public B Value { get; set; }
    }
}




------解决方案--------------------
代码太长,你要把遇到的问题写出来,才好分析