转换DataGridView ColumnType运行时 [英] Convert DataGridView ColumnType runtime

查看:203
本文介绍了转换DataGridView ColumnType运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


谁能告诉我如何在运行时转换DataGridView ColumnType.


我需要将DataGridViewLinkColumn转换为DataGridViewComboboxColumn.



谢谢..

Hi,
Can anybody tell me how to convert DataGridView ColumnType at runtime.


I need to convert a DataGridViewLinkColumn into a DataGridViewComboboxColumn.



Thank You..

推荐答案

无法将datagridviewtextbox单元格放入datagridviewcomboboxcolumn中.您可以做的是在datagridviewtextbox列中有一个datagridviewcombobox单元.

代码段

DataGridViewComboBoxCell cc =新的DataGridViewComboBoxCell();
//您可以设置组合框的数据源,而不用添加项目
cc.Items.Add("1");
cc.Items.Add("2");
cc.Items.Add("3");

dataGridView1 [0,3] = cc;



您可以执行以下操作:

代码段

公共局部类Form1:Form

{

公共Form1()

{

InitializeComponent();

}

私有void Form1_Load(对象发送者,EventArgs e)

{

DataTable dt =新的DataTable("b");

dt.Columns.Add("col");

dt.Rows.Add("bb1");

dt.Rows.Add("bb2");

dt.Rows.Add("bb3");



this.dataGridView1.DataSource = dt;



DataGridViewComboBoxColumn cmb =新的DataGridViewComboBoxColumn();

cmb.Items.Add("111");

cmb.Items.Add("222");

cmb.Items.Add("333");

this.dataGridView1.Columns.Add(cmb);

}

private void button1_Click(对象发送者,EventArgs e)

{

DataGridViewTextBoxCell单元格=新的DataGridViewTextBoxCell();

cell.Value ="bb1";

this.dataGridView1 [1,1] =单元格;

}

}
It is not possible to put a datagridviewtextboxcell in datagridviewcomboboxcolumn. What you can do is have a datagridviewcomboboxcell in a datagridviewtextboxcolumn.

Code Snippet

DataGridViewComboBoxCell cc = new DataGridViewComboBoxCell();
// you could set the combobox''s datasource instead of adding items
cc.Items.Add("1");
cc.Items.Add("2");
cc.Items.Add("3");

dataGridView1[0,3]= cc;



You can do something like this:

Code Snippet

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable("b");

dt.Columns.Add("col");

dt.Rows.Add("bb1");

dt.Rows.Add("bb2");

dt.Rows.Add("bb3");



this.dataGridView1.DataSource = dt;



DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();

cmb.Items.Add("111");

cmb.Items.Add("222");

cmb.Items.Add("333");

this.dataGridView1.Columns.Add(cmb);

}

private void button1_Click(object sender, EventArgs e)

{

DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();

cell.Value = "bb1";

this.dataGridView1[1, 1] = cell;

}

}


谢谢..
实际上,我希望以一种不同的方式...
就是你做的另一种方式...

其将按钮单击事件中的datagridviewComboboxCell转换为dataGridViewTextBoxCell.

我想要双方的转换...
还可以在编辑文本框时保留组合框的项目...

尽管我现在可以做到一半了,然后再返回组合框.
谢谢你.但无法将所选数据保留在组合框上.
Thank You..
Actually, i wanted it in a different way...
just the other way you did it...

its converting datagridviewComboboxCell to dataGridViewTextBoxCell on button click event.

I wanted the conversion from both sides...
also to retain the items of combobox on editing the textbox...

Although I am now able to do it half way... And back to Combobox.
Thanks to you. but unable to retain selected data on the combobox.


这篇关于转换DataGridView ColumnType运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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