DataGridView从列标记获取ColumnName [英] DataGridView Get ColumnName From Column Tag

查看:209
本文介绍了DataGridView从列标记获取ColumnName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题,但经过大量搜索后,我没有找到解决方案。我想通过列标记获取 DataGridView ColumnName 。我已经在具有列名的每列上放置了标签。

It's a simple question but after searching a lot I didn't find a solution for it. I want to get ColumnName of a DataGridView by column tag. I had put tags on each column with column name.

编辑:

我还在标签中存储了一个十进制值。


Also I store a decimal value in tag.

这是我的代码:

DataGridViewColumn dgCol = new DataGridViewColumn();
dgCol.Name = "MyCol";
dgCol.Tag = 10; 
DataGridView1.Columns.Add(dgCol );

标记的类型为 Object 。现在我想设置单元格的值,但我没有使用列标记,也没有名称,也不知道列索引。

While tag is of type Object. Now I want to set value of cell but I had column tag not the name neither I know column index.

DataGridView1.Rows[1].Cells[" _**Dont Have ColumnName**_ "].Value = 100;  

但是我有列标记。那么,从列标记中获取列名有什么办法?

But I had column tag. So is there any way from which I got column name from column tag?

推荐答案

您可以遍历所有

DataGridViewColumn getcol;

foreach(DataGridViewColumn c in DataGridView1.Columns)
{
    if(c.Tag as String == "10")
    {
        getcol = c;
    }
}

现在您可以使用此 getcol

DataGridView1.Rows[1].Cells[getcol.Name].Value = 100;

这篇关于DataGridView从列标记获取ColumnName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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