我用B接口继承了A接口,用类T实现了子接口B,在form中用datagridview通过T访问接口B设置数据源,但是只能访问到B的接口属性,A的接口属性访问不到,如果在一个命名空间是可以访问的,但在不同的空间就不行。

解决方案 »

  1.   

    你把那个命名空间using进来不行吗
      

  2.   

    A和B是在同一个命名空间中,用using引人过了。T在另一个空间中,显示的控件又在一个空间中,都引入过了,不知道为什么就是不行。
      

  3.   

    datagridview,combox加数据源时都是这样的,只能显示到所定义的当前类/接口的属性,其他的看不到,你可以用cellformating再进行调整,或建立一个新类,定义你要显示的所有属性,再把数据源设成新类。
      

  4.   

    7楼,请问cellformating怎么设置?
      

  5.   


        public partial class Form1 : Form
        {        public Form1()
            {
                InitializeComponent();            dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
                List<A2 > zlist = new List<A2 >();  //如果使用List<Z>则可以全部显示, 使用List<A2>不能显示x列,需要在cellformating中调整
                zlist.Add (new Z (1,"a"));
                zlist.Add(new Z(2, "b"));
                zlist.Add(new Z(3, "c"));
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = zlist;
                dataGridView1.Columns["Dx"].DataPropertyName = "X";
                dataGridView1.Columns["DY"].DataPropertyName = "Y";        }        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {            if (e.ColumnIndex == Dx.Index&& e.RowIndex >=0)
                {
                    A2 u = dataGridView1.Rows[e.RowIndex].DataBoundItem as A2;
                    e.Value = u.X;                 
                }
            }
        }
        interface A1
        {
            int X { get; }
        }
        interface A2 : A1
        {
            string Y { get; }
        }    class Z : A2
        {
            int _x;
            string _y;        public Z(int x, string y)
            {
                _x = x;
                _y = y;
            }        public string Y
            {
                get { return _y; }
            }        public int X
            {
                get { return _x; }
            }
        }