更改datagridview中的单元格类型 [英] change the cell type in a datagridview

查看:687
本文介绍了更改datagridview中的单元格类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时更改datagridview列中的单元格类型(textbox to button)而不更改列类型?  如果是这样怎么样?  我已经尝试过使用RemoveAt然后添加一个新的列类型,但它不起作用,值没有出现在单元格中的

Is it possible to change the cell type in a datagridview column at run time (textbox to button) without changing the column type?  If so how?  I have tried using RemoveAt then adding a new column type, but its not working, value not appearing in cell.

推荐答案

你好,

>>是否可以在运行时更改datagridview列中的单元格类型(文本框到按钮)而不更改列类型?

>> Is it possible to change the cell type in a datagridview column at run time (textbox to button) without changing the column type?

是的,这可以是done.you可以重新分配单元格类型。

Yes, This can be done.you can re-assign the cell type.

请尝试以下代码。

 private void Form_Load(object sender, EventArgs e)
        {
            DataGridViewTextBoxColumn Column1 = new DataGridViewTextBoxColumn();
            Column1.HeaderText = "Column1";
            dataGridView1.Columns.Add(Column1);
            DataGridViewTextBoxColumn Column2 = new DataGridViewTextBoxColumn();
            Column2.HeaderText = "Column2";
            dataGridView1.Columns.Add(Column2);
            dataGridView1.Rows.Add(5);
            for(int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    dataGridView1.Rows[i].Cells[j].Value = i.ToString();
                }
            }
            DataGridViewButtonCell cell = new DataGridViewButtonCell();
            cell.Value = dataGridView1.Rows[1].Cells[1].Value;
            dataGridView1.Rows[1].Cells[1] = cell;
        }

最好的问候,

Bob


这篇关于更改datagridview中的单元格类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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