在C# Winform中 我定义了一个实体 格式如下:
public class User
{
string _UserName, _Pass;
IList<string> _Other = new List<string>();
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
public string Pass
{
get { return _Pass; }
set { _Pass = value; }
}
public IList<string> Other
{
get { return _Other; }
set { _Other = value; }
}
}假设Other中有两个值 "男" "1985-1-1"现在我要把它邦定到DataGridView上并添加UserName、Password、Sex、Brithday列,UserName和Password列DataPropertyName分别为UserName和Password Sex我设置了Other["Sex"] 结果Sex和Brithday两列都没有显示 请问我怎么设置呢?
public class User
{
string _UserName, _Pass;
IList<string> _Other = new List<string>();
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
public string Pass
{
get { return _Pass; }
set { _Pass = value; }
}
public IList<string> Other
{
get { return _Other; }
set { _Other = value; }
}
}假设Other中有两个值 "男" "1985-1-1"现在我要把它邦定到DataGridView上并添加UserName、Password、Sex、Brithday列,UserName和Password列DataPropertyName分别为UserName和Password Sex我设置了Other["Sex"] 结果Sex和Brithday两列都没有显示 请问我怎么设置呢?
看看 我的 ..我不想多说了
===
==============================================================================
private List<Gro> groceryList = new List<Gro>();
private void frmMain_Load(object sender, EventArgs e)
{
Gro[] items = new Gro[]{
new Gro("Yogurt",Convert.ToDecimal(2.95)),
new Gro("Carrots",Convert.ToDecimal(1.55)),
new Gro("Celery",Convert.ToDecimal(0.99)),
new Gro("Rice",Convert.ToDecimal(5.39)),
new Gro("Bread",Convert.ToDecimal(3.95)),
new Gro("Milk",Convert.ToDecimal(3.95)),
new Gro("Soda",Convert.ToDecimal(1.99)),
new Gro("Cheese",Convert.ToDecimal(5.99)),
new Gro("Paper Towels",Convert.ToDecimal(4.55))
}; groceryList.AddRange(items); UpdateGrid(groceryList);
}
private void UpdateGrid(List<Gro> list)
{
int c=1;
decimal total=0;
dataGridView1.Rows.Clear();
list.ForEach(delegate(Gro item)
{
dataGridView1.Rows.Add(new object[] { c++, item.ITEM, String.Format("{0:c}",item.PRICE)});
total += item.PRICE;
});
lblTotal.Text = String.Format("{0:c}",total);
}
==
class Gro
{
private string _item = null;
private decimal _price = 0; public Gro(){}
public Gro(string item, decimal price)
{
_item = item;
_price = price;
} public string ITEM
{
get { return this._item; }
} public decimal PRICE
{
get { return this._price; }
}
}
((IList)Eval("Other"))[0]
看看这样行不行
或者你在建个datatable
把属性和Other拿出来当作列
然后把datatable绑定到datagrid
{
public string Sex;
public string Birthday;
...
}
那就只能重新创建一个DataTable
根据Other去动态的创建列,在和原来的正常属性一起绑定到DataGrid
2个尖括号代表什么?这是什么方法?
可以采用的方法,再建2个属性Sex和Birthday:
public class User
{
string _UserName, _Pass;
IList<string> _Other = new List<string>();
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
public string Pass
{
get { return _Pass; }
set { _Pass = value; }
}
public IList<string> Other
{
get { return _Other; }
set { _Other = value; }
}
public string Sex{
get { return Other[0];}
set { Other[0] = value;}
}
public string Birthday{
get { return Other[1];}
set { Other[1] = value;}
}
}我还想问一下,lz说的webform下能做到是怎么做到的?