Juni1

Hi,

I am trying to mix some ComboBox cells and TextBox cells together in DataGridViewColumn. But I don't have succeeded. Is this even possible

Also, if I am trying to modify dynamically Datagridview it throws an exception:
"Collection already belongs to a DataGridView control. This operation is no longer valid."

Codeline is following when using C#:
this.Rows[2].Cells.RemoveAt(index);

Thanks forehand for answering.

T. Juni



Re: Windows Forms Data Controls and Databinding Datagridviewcolumn with ComboBox and TextBox cells together

Karthikeya Pavan Kumar .B

Try looking at this post

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=260387&SiteID=1






Re: Windows Forms Data Controls and Databinding Datagridviewcolumn with ComboBox and TextBox cells together

Juni1

Actually I don't mean merging cells together. I mean Column, that contains both types of cell. (ComboBoxes and TextBoxes). Is that possible and how

If I make new column, all the cells in that column is just type of newCell.

DataGridViewColumn newCol = new DataGridViewColumn(newCell);






Re: Windows Forms Data Controls and Databinding Datagridviewcolumn with ComboBox and TextBox cells together

Martin Xie - MSFT

Move it from C# General forum to Windows Form forum for better response.

Thank you for your understanding!






Re: Windows Forms Data Controls and Databinding Datagridviewcolumn with ComboBox and TextBox cells together

Rong-Chun Zhang - MSFT

Hi juni1,

Yes, you can, you can change the type of a DataGridViewCell dynamicly. See my sample below. It provides you a column with TextBoxCell and ComboBoxCell, and the cell's type depends on the CheckBoxCell in the same row.

Code Snippet

namespace NewDGV

{

public partial class DGVTXTandCBO : Form

{

public DGVTXTandCBO()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private void DGVTXTandCBO_Load(object sender, EventArgs e)

{

dt.Columns.Add("aa", typeof(bool));

dt.Columns.Add("bb");

dt.Columns[1].DefaultValue = "False";

for (int i = 1; i < 20; i++)

{

dt.Rows.Add((i % 2 == 0), Convert.ToString(i % 2 == 0));

}

this.dataGridView1.DataSource = dt;

this.dataGridView1.Columns.RemoveAt(1);

DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();

cbc.HeaderText = "bb";

cbc.Items.AddRange(new object[] { "True", "False" });

cbc.DataPropertyName = "bb";

this.dataGridView1.Columns.Add(cbc);

foreach (DataGridViewRow dr in this.dataGridView1.Rows)

{

if (dr.Cells[0].Value != null)

{

if ((bool)dr.Cells[0].Value)

{

DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();

dr.Cells[1] = txtcell;

}

}

}

this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

this.dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);

}

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

{

if (e.ColumnIndex == 1)

{

DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;

if (!cm.Items.Contains(e.FormattedValue))

cm.Items.Add(e.FormattedValue);

}

}

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

{

if (e.ColumnIndex == 0)

{

if (this.dataGridView1[0, e.RowIndex].Value != null)

{

if ((bool)this.dataGridView1[0, e.RowIndex].Value)

{

DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();

this.dataGridView1.Rows[e.RowIndex].Cells[1] = txtcell;

}

else

{

DataGridViewComboBoxCell cbocell = new DataGridViewComboBoxCell();

DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;

cbocell.Items.AddRange(cm.Items);

this.dataGridView1.Rows[e.RowIndex].Cells[1] = cbocell;

}

}

}

}

}

}

Hope this helps.

Regards






Re: Windows Forms Data Controls and Databinding Datagridviewcolumn with ComboBox and TextBox cells together

Juni1

Thanks very much. That was just what I needed. Good and simple example.

Regards
Juni