数据源访问继承接口问题 我用B接口继承了A接口,用类T实现了子接口B,在form中用datagridview通过T访问接口B设置数据源,但是只能访问到B的接口属性,A的接口属性访问不到,如果在一个命名空间是可以访问的,但在不同的空间就不行。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你把那个命名空间using进来不行吗 A和B是在同一个命名空间中,用using引人过了。T在另一个空间中,显示的控件又在一个空间中,都引入过了,不知道为什么就是不行。 datagridview,combox加数据源时都是这样的,只能显示到所定义的当前类/接口的属性,其他的看不到,你可以用cellformating再进行调整,或建立一个新类,定义你要显示的所有属性,再把数据源设成新类。 7楼,请问cellformating怎么设置? 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; } } } 新手求教! 在C#里面利用委托做连个页面直接传值? 正则表达式取数据? 一个小问题。 实在没有分了!!望大家帮忙呀!谢谢了!如何用代码实现关闭已打开的多个窗口呢????? 操作活动目录的问题!!!!!! ADO.NET数据库多表关联的删除 请求各位推荐几本书吧。 密码单向加密 datagrid,xml请教 初级问题:怎么把字符串转换成数值? [C#]如何在一个函数里调用一个控件的事件过程 点击按钮变色?
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; }
}
}