DataBind DataGridViewRow.Tag到列表中的项目 [英] DataBind DataGridViewRow.Tag to an item in a List

查看:125
本文介绍了DataBind DataGridViewRow.Tag到列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

让我们假设此代码

Hi experts,

let''s assume this code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Test_DataBinding
{
    public partial class Form1 : Form
    {
        private FooContainer _foos = new FooContainer();

        public Form1()
        {
            InitializeComponent(); // Including Multiselect = false

            foreach (Foo foo in _foos.Items)
            {
                int rowIndex = dgvFoo.Rows.Add();
                dgvFoo.Rows[rowIndex].Cells["ObjectName"].Value = foo.Name;
                dgvFoo.Rows[rowIndex].Tag = foo;
            }

            //dgvFoo.DataBindings.Add(dgvFoo.selectedrow
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    }


    public class FooContainer
    {
        private List<foo> _fooList = new List<foo>();
        private Foo _selected = null;


        public FooContainer()
        {
            _fooList.Add(new Foo("First"));
            _fooList.Add(new Foo("Second"));
            _fooList.Add(new Foo("Third"));
        }


        public List<foo> Items
        {
            get { return (_fooList); }
        }


        public Foo SelectedItem
        {
            get { return (_selected); }
            set { _selected = value; }
        }
    }


    public class Foo
    {
        private string _name = string.Empty;


        public Foo(string name)
        {
            _name = name;
        }


        public string Name
        {
            get { return (_name); }
        }


        public override string ToString()
        {
            string name = _name;
            if(name==string.Empty)
                name = "NoName";

            return (name + "(" + this.GetType().ToString() + ")");
        }
    }
}
</foo></foo></foo>

我在注释掉的行中要执行的操作是对属性FooContainer.SelectedItem和DataGridView.SelectedRows进行数据绑定.

用户应该能够通过更改DataGridView中的选择来更改FooContainer中的所选项目.
FooContainer.SelectedItem的程序性更改应在DataGridView中显示.

FooContainer.SelectedItem的类型为Foo.
DataGridView.SelectedRows是DataGridViewRows的列表,Foos作为标签附加.
你怎么会被数据绑定?

预先感谢,


luker

What I''m trying to do in the line that is commented out is to databind the properties FooContainer.SelectedItem and DataGridView.SelectedRows.

The user should be able to change the selected item in FooContainer by changing the selection in the DataGridView.
And a programatical change in FooContainer.SelectedItem shall be shown in DataGridView.

FooContainer.SelectedItem is of type Foo.
DataGridView.SelectedRows is a list of DataGridViewRows with Foos attached as Tags.
How can thy be databound?

Thanks in advance,


luker

推荐答案


我专注于用户应该能够通过更改DataGridView中的选择来更改FooContainer中的所选项目."

以下解决方案使用户可以通过单击单元格来选择项目,并且以下事件将绑定数据集合中的相应项目设置为您的FooContainer类中的SelectedItem.
您应该问自己,是否真的需要FooContainer类.

请记住要投票和发表评论!

Hi
I''m focusing on "The user should be able to change the selected item in FooContainer by changing the selection in the DataGridView."

The following solution lets the user select items by clicking the cell, and the following event sets the according item in the bound data collection to the SelectedItem in your FooContainer-class.

Your should ask you self if the FooContainer-class really is needed.

Please remember to vote and comment!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Test_DataBinding
{
    public partial class Form1 : Form
    {
        private FooContainer _foos = new FooContainer();

        public Form1()
        {
            InitializeComponent(); // Including Multiselect = false

            /*foreach (Foo foo in _foos.Items)
            {
                int rowIndex = dgvFoo.Rows.Add();
                dgvFoo.Rows[rowIndex].Cells["ObjectName"].Value = foo.Name;
                dgvFoo.Rows[rowIndex].Tag = foo;
            }*/

            //dgvFoo.DataBindings.Add(dgvFoo.selectedrow

            dgvFoo.DataSource = _foos.Items;
            dgvFoo.CellClick += new DataGridViewCellEventHandler(dgvFoo_CellClick);
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    }

    void dgvFoo_CellClick(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dgvFoo.SelectedRows)
        {
            Foo myFoo = row.DataBoundItem as Foo;
            if (myFoo != null)
            {
                // Assuming only one selected
                _foos.SelectedItem = myFoo;
            }
        }
    }


    public class FooContainer
    {
        private BindingList<foo> _fooList = new BindingList<foo>();
        private Foo _selected = null;


        public FooContainer()
        {
            _fooList.Add(new Foo("First"));
            _fooList.Add(new Foo("Second"));
            _fooList.Add(new Foo("Third"));
        }


        public BindingList<foo> Items
        {
            get { return (_fooList); }
        }


        public Foo SelectedItem
        {
            get { return (_selected); }
            set { _selected = value; }
        }
    }


    public class Foo
    {
        private string _name = string.Empty;


        public Foo(string name)
        {
            _name = name;
        }


        public string Name
        {
            get { return (_name); }
        }


        public override string ToString()
        {
            string name = _name;
            if(name==string.Empty)
                name = "NoName";

            return (name + "(" + this.GetType().ToString() + ")");
        }
    }
}</foo></foo></foo>


这篇关于DataBind DataGridViewRow.Tag到列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆