简体   繁体   English

.NET 框架。 如何从datagridview的不可见列中获取文本值?

[英].NET Framework. How to get the text value from invisible column of datagridview?

My issue is very simple and it would be easy if I write my program on ASP.NET. But I apply .NET Framework 4.8 and I have no idea how to get the value from that datagridview if the column is hidden.我的问题非常简单,如果我在 ASP.NET 上编写我的程序会很容易。但是我应用 .NET Framework 4.8,如果列被隐藏,我不知道如何从该 datagridview 中获取值。

for (int i2 = 0; i2 < RowsAmount; i2++)
        {
            int er = Convert.ToInt32(dataGridView1.Rows[i2].Cells["id_contact"].Value.ToString());
            MessageBox.Show($"{er}");
            // to do smth with that value.
        }

The column named as id_contact is hidden as you can guess.您可以猜到,名为 id_contact 的列是隐藏的。 What should I do if the error is "object reference does not point to an instance of an object "?报错“对象引用没有指向一个object的实例”怎么办?

Recommend adding columns to your DataGridView, set DataPropertyName to the source whatever that may be (perhaps a DataTable or List).建议向您的 DataGridView 添加列,将DataPropertyName设置为源,无论它是什么(可能是 DataTable 或 List)。 Set Visible property to false for in your case id_contact .在您的情况下,将 Visible 属性设置为 false id_contact

Setup a BindingList , BindingSource to your data source where the BindingSource becomes the data source of the DataGridView.BindingListBindingSource设置为您的data source ,其中 BindingSource 成为 DataGridView 的数据源。 To get an the invisible column access via the BindingList indexed by the BindingSource Position property.通过由 BindingSource Position 属性索引的 BindingList 获取不可见列访问。

Here the first column IdColumn (defined in the DataGridView designer) is hidden.这里隐藏了第一列 IdColumn(在 DataGridView 设计器中定义)。

namespace DataGridViewSamples
{
    public partial class SampleForm : Form
    {
        public SampleForm() { InitializeComponent(); }
        private readonly BindingSource _bindingSource = new BindingSource();
        private BindingList<Item> _bindingList;

        private void SampleForm_Load(object sender, EventArgs e)
        {
            List<Item> items = new List<Item>()
            {
                new Item(){ Id = 1, Process = false, Product = "Phone 2"},
                new Item(){ Id = 2, Process = true, Product = "Phone 3"},
                new Item(){ Id = 3, Process = false, Product = "Phone 4"},,
            };

            _bindingList = new BindingList<Item>(items);
            _bindingSource.DataSource = _bindingList;
            dataGridView1.DataSource = _bindingSource;
        }
        private void CurrentIdButton_Click(object sender, EventArgs e)
        {
            if (_bindingSource.Current != null)
            {
                MessageBox.Show($"{_bindingList[_bindingSource.Position].Id}");
            }
        }
    }    
    public class Item
    {
        public int Id { get; set; }
        public bool Process { get; set; }
        public string Product { get; set; }
    }
}

The decision turned out very simple.这个决定原来很简单。 I have just forgotten about so-called datatype DataRow .我刚刚忘记了所谓的数据类型DataRow With aid of it and the cycle for I could of solve it faster.借助它和循环我可以更快地解决它。 I've decided to record the data into datagridview in this way:我决定以这种方式将数据记录到 datagridview 中:

if (dTable.Rows.Count > 0)
            {
                DataRow row;
                for (int i = 0; i < dTable.Rows.Count; i++)
                {
                    row = dTable.Rows[i];
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[i].Cells["id"].Value =
                        row["id"];
                    dataGridView1.Rows[i].Cells["Brief_name"].Value = 
                        row["brief_name"];
                    dataGridView1.Rows[i].Cells["FullName"].Value =
                        row["full_official_name"];
                    dataGridView1.Rows[i].Cells["Adress"].Value =
                        row["ju_adress"];
                    dataGridView1.Rows[i].Cells["Code"].Value =
                        row["EDRPOU"];
                    dataGridView1.Rows[i].Cells["Branch"].Value =
                        row["branch_economy"];
                    dataGridView1.Rows[i].Cells["id_contact"].Value =
                        row["id_contact_person"];
                    RowsAmount++;
                }
            }

Some of these columns are invisible and the previous command int er = Convert.ToInt32(dataGridView1.Rows[i2].Cells["id_contact"].Value.ToString());其中一些列是不可见的,之前的命令int er = Convert.ToInt32(dataGridView1.Rows[i2].Cells["id_contact"].Value.ToString()); works out.解决了。

Just I haven't willed to apply BindingSource , because the essential part of my program works without it.只是我不愿意申请BindingSource ,因为我的程序的基本部分没有它就可以工作。 But anyway thank you for attention.但无论如何谢谢你的关注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM