使用 Bindinglist 在 datagridview 中显示右侧数据成员
目标:
显示播放器的封装字段
问题:
想要仅使用 bindingList
在类 mainform
中显示 datamember_id
、_name
和 _bust
是否应该在封装字段上方使用语法 []
?
Class MainForm
dataGridViewPlayers.AutoGenerateColumns=true;
dataGridViewPlayers.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;
bindingSourcePlayers.Clear();
bindingSourcePlayers.DataSource = _myGameManager.Players;
Class GameManager:
public BindingList<Player> Players
{
get
{
for (int i = 0; i < _myPlayerGUI_list.Count; i++)
{
_player.Add(new Player(_myPlayerGUI_list[i].Player));
}
return _player;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardGameClassLibrary;
namespace CardGameLib
{
public class Player
{
private int _id;
private string _name;
private Hand _myHand;
private int _win;
private int _lost;
private bool _madeMove = false;
private bool _bust = false;
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public Hand MyHand
{
get { return _myHand; }
set { _myHand = value; }
}
public int Win
{
get { return _win; }
set { _win = value; }
}
public int Lost
{
get { return _lost; }
set { _lost = value; }
}
public bool MadeMove
{
get { return _madeMove; }
set { _madeMove = value; }
}
public bool Bust
{
get { return _bust; }
set { _bust = value; }
}
public Player(int pId)
{
_id = pId;
_myHand = new Hand();
}
public Player(Player pPlayer)
{
_id = pPlayer.Id;
//_name = pPlayer.Name;
_name = "adsf";
}
public Player()
{
}
}
}
Goal:
Display Encapsulate field from Player
Problem:
Want to display datamember_id
, _name
and _bust
in class mainform
only by using bindingList
Was it suppose to use syntax []
above the encapsulate field?
Class MainForm
dataGridViewPlayers.AutoGenerateColumns=true;
dataGridViewPlayers.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;
bindingSourcePlayers.Clear();
bindingSourcePlayers.DataSource = _myGameManager.Players;
Class GameManager:
public BindingList<Player> Players
{
get
{
for (int i = 0; i < _myPlayerGUI_list.Count; i++)
{
_player.Add(new Player(_myPlayerGUI_list[i].Player));
}
return _player;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardGameClassLibrary;
namespace CardGameLib
{
public class Player
{
private int _id;
private string _name;
private Hand _myHand;
private int _win;
private int _lost;
private bool _madeMove = false;
private bool _bust = false;
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public Hand MyHand
{
get { return _myHand; }
set { _myHand = value; }
}
public int Win
{
get { return _win; }
set { _win = value; }
}
public int Lost
{
get { return _lost; }
set { _lost = value; }
}
public bool MadeMove
{
get { return _madeMove; }
set { _madeMove = value; }
}
public bool Bust
{
get { return _bust; }
set { _bust = value; }
}
public Player(int pId)
{
_id = pId;
_myHand = new Hand();
}
public Player(Player pPlayer)
{
_id = pPlayer.Id;
//_name = pPlayer.Name;
_name = "adsf";
}
public Player()
{
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Browsable()< /a> 属性,以防止在使用
BindingList
时在DataGridView
中显示特定属性。示例:如果您想隐藏
MadeMove
:You can use Browsable() attribute to prevent specific properties from being shown in the
DataGridView
when usinBindingList
.Example: if you want to hide
MadeMove
: