Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > datagridcomboboxcolumn is not update the bindsource datagridview
 

datagridcomboboxcolumn is not update the bindsource datagridview

Hi

I have a 2 datagridview. 1st one i have a datagridcomboboxcolumn which has some pre loaded values. I have a add button which helps me to add the new row in the datagridview1. i have selected some values in the datagridviewcomboboxcolumn which is need to be added automatically in the datagridview2. but it has added the blank row not theselected value in the datagridview1 datagridviewcomboboxcolumn.

Vasudevan2  Thursday, May 22, 2008 6:30 PM

Hi Vasudevan,

I am not very clear about your problem, did you mean binding the two DataGridViews to the same BindingSource and synch the data between the two DataGridViews? The new row is not actually added to the DataTable until the user navigates to another row in DataGridView. When you go to the new row of a DataGridView, the DataGridView creates a new DataRow to be added to the DataSet. Once the user moves off the row the new row will be added to the data table.

To workaround this, you can manually set the current cell of your DataGridView, please check the following sample:

Code Snippet

public partial class Form27 : Form

{

public Form27()

{

InitializeComponent();

}

BindingSource bs = new BindingSource();

private void Form27_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("aa");

dt.Columns.Add("bb");

for (int i = 0; i < 5; i++)

{

dt.Rows.Add("aa" + i, "bb" + i);

}

dt.AcceptChanges();

bs.DataSource = dt;

this.dataGridView1.DataSource = bs;

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();

col.HeaderText = "aa";

col.DataPropertyName = "aa";

for (int i = 0; i < 5; i++)

{

col.Items.Add("aa" + i);

}

this.dataGridView1.Columns.RemoveAt(0);

this.dataGridView1.Columns.Insert(0, col);

this.dataGridView2.DataSource = bs;

this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

if (cob != null)

{

cob.SelectedIndexChanged -= new EventHandler(cob_SelectedIndexChanged);

}

}

ComboBox cob;

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

if (e.Control is ComboBox)

{

cob = (ComboBox)e.Control;

cob.SelectedIndexChanged += new EventHandler(cob_SelectedIndexChanged);

}

}

void cob_SelectedIndexChanged(object sender, EventArgs e)

{

this.dataGridView1.BeginInvoke(new MethodInvoker(EndEdit));

}

void EndEdit()

{

DataGridViewCell cell = this.dataGridView1.CurrentCell;

this.dataGridView1.CurrentCell = this.dataGridView1[0, 0];

this.dataGridView1.CurrentCell = cell;

}

}

Let me know if this helps. If not, could you please provide more detailed information? Some code would be helpful.

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Thursday, May 29, 2008 9:09 AM

Hi Vasudevan,

I am not very clear about your problem, did you mean binding the two DataGridViews to the same BindingSource and synch the data between the two DataGridViews? The new row is not actually added to the DataTable until the user navigates to another row in DataGridView. When you go to the new row of a DataGridView, the DataGridView creates a new DataRow to be added to the DataSet. Once the user moves off the row the new row will be added to the data table.

To workaround this, you can manually set the current cell of your DataGridView, please check the following sample:

Code Snippet

public partial class Form27 : Form

{

public Form27()

{

InitializeComponent();

}

BindingSource bs = new BindingSource();

private void Form27_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("aa");

dt.Columns.Add("bb");

for (int i = 0; i < 5; i++)

{

dt.Rows.Add("aa" + i, "bb" + i);

}

dt.AcceptChanges();

bs.DataSource = dt;

this.dataGridView1.DataSource = bs;

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();

col.HeaderText = "aa";

col.DataPropertyName = "aa";

for (int i = 0; i < 5; i++)

{

col.Items.Add("aa" + i);

}

this.dataGridView1.Columns.RemoveAt(0);

this.dataGridView1.Columns.Insert(0, col);

this.dataGridView2.DataSource = bs;

this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

if (cob != null)

{

cob.SelectedIndexChanged -= new EventHandler(cob_SelectedIndexChanged);

}

}

ComboBox cob;

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

if (e.Control is ComboBox)

{

cob = (ComboBox)e.Control;

cob.SelectedIndexChanged += new EventHandler(cob_SelectedIndexChanged);

}

}

void cob_SelectedIndexChanged(object sender, EventArgs e)

{

this.dataGridView1.BeginInvoke(new MethodInvoker(EndEdit));

}

void EndEdit()

{

DataGridViewCell cell = this.dataGridView1.CurrentCell;

this.dataGridView1.CurrentCell = this.dataGridView1[0, 0];

this.dataGridView1.CurrentCell = cell;

}

}

Let me know if this helps. If not, could you please provide more detailed information? Some code would be helpful.

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Thursday, May 29, 2008 9:09 AM

You can use google to search for other answers

Custom Search

More Threads

• NullReferenceException
• PDF File Open Issue (File not visible)
• LINQ refresh databings
• deleting records from dataset- DeletedRowInaccessibleException
• Updating a dataset via a binding source
• DBNull type in a DataRow, how to set value?
• How to remove a cell value in datagrid?
• Databind a collection and get parent property
• switch textbox column to checkbox column
• ComboBox once again