加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#Datagridview中以编程方式在指定位置添加新列

发布时间:2020-12-16 02:01:47 所属栏目:百科 来源:网络整理
导读:我有一个5(0-5)列的datagridview.我从创建的哈希表中检索的所有行值. 现在我设置了一个条件,表明第4列是否包含哈希表中的空值,然后在第4列旁边添加新列,这使得新添加的列索引位于第5位,之前第5列的哈希表值更改为第7列. 我做这样的代码: int number = dataG
我有一个5(0-5)列的datagridview.我从创建的哈希表中检索的所有行值.
现在我设置了一个条件,表明第4列是否包含哈希表中的空值,然后在第4列旁边添加新列,这使得新添加的列索引位于第5位,之前第5列的哈希表值更改为第7列.

我做这样的代码:

int number = dataGridView1.Rows.Add();
    dataGridView1.Rows[number].Cells[0].Value = result;                                 //id       
    dataGridView1.Rows[number].Cells[1].Value = newAddress;                             //ip    
    dataGridView1.Rows[number].Cells[2].Value = (string)((Hashtable)ht[1])["value"];    //name
    dataGridView1.Rows[number].Cells[3].Value = (string)((Hashtable)ht[2])["value"];    //description
    if (!ht.ContainsValue(3))
        {
          // Create a Save button column
          DataGridViewImageButtonSaveColumn columnSave = new DataGridViewImageButtonSaveColumn();

          // Set column values
          columnSave.Name = "SaveButton";
          columnSave.HeaderText = "";

          //Add the columns to the grid
          dataGridView1.Rows[number].Cells[4].ReadOnly = false;
          dataGridView1.Columns[5].Add(columnSave);    //im not sure about this codes
          dataGridView1.Rows[number].Cells[6].Value = (string)((Hashtable)ht[4])["value"];    //count

        } 
        else
        {
          dataGridView1.Rows[number].Cells[4].Value = (string)((Hashtable)ht[3])["value"];    //location
          dataGridView1.Rows[number].Cells[5].Value = (string)((Hashtable)ht[4])["value"];    //count
        }

但是,我不确定我是否正确行事,因为我在评论的行中收到错误

dataGridView1.Columns[5].Add(columnSave);    //im not sure about this codes

好像这个代码错了.任何人都可以建议吗?

解决方法

试试dataGridView1.Columns.Insert(5,columnSave);代替.

MSDN参考:DataGridViewColumnCollection.Insert Method

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读