使用 Bindinglist 在 datagridview 中显示右侧数据成员

发布于 2024-11-06 10:35:07 字数 2378 浏览 3 评论 0原文

目标:

显示播放器的封装字段

问题:
想要仅使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み青杉依旧 2024-11-13 10:35:07

您可以使用 Browsable()< /a> 属性,以防止在使用 BindingList 时在 DataGridView 中显示特定属性。

示例:如果您想隐藏 MadeMove

[Browsable(false)]
public bool MadeMove
{
    get { return _madeMove; }
    set { _madeMove = value; }
}

You can use Browsable() attribute to prevent specific properties from being shown in the DataGridView when usin BindingList.

Example: if you want to hide MadeMove:

[Browsable(false)]
public bool MadeMove
{
    get { return _madeMove; }
    set { _madeMove = value; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文