Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Can anybody tell me how to convert DataGridView ColumnType at runtime.


I need to convert a DataGridViewLinkColumn into a DataGridViewComboboxColumn.


Thank You..
Posted

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;

}

}
 
Share this answer
 
Comments
Ragi Gopi 30-Jun-11 3:49am    
@meBalkrishna

I have doubt about datagridviewcell.
i have created textboxcolumn in datagridview. when ever i click the particular cell the cell should change to datagridviewcomboboxcell. Also i need to add the item in combobox collection.
when i go to another cell then, the new cell get datagridviewcombobox cell and existing cell should change to datagridviewtextbox cell...
Ragi Gopi 30-Jun-11 3:50am    
Can u plz help me....
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.
 
Share this answer
 
Comments
agent_kruger 13-May-14 7:06am    
sir, you can leave comment on others answer but never write your comment in the answer tab.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900