TheSwamp
Code Red => .NET => Topic started by: latour_g on December 09, 2020, 04:55:44 PM
-
Hi,
I have a datagridview link to a datatable.
It contains three DataColumn and one DataGridViewComboBoxColumn. I thought I was doing the right thing when adding data to my datagridview :
System.Data.DataRow row = null;
row = lyrTable.NewRow();
row["Nom"] = "test1";
row["Visibilité"] = "Visi1";
row["Quantité"] = "1";
lyrTable.Rows.Add(row);
DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)dgvBlocs.Rows[nRow].Cells["Visibilité New"];
dgvcbc.Items.Clear();
dgvcbc.Items.Add("visi1");
dgvcbc.Items.Add("visi2");
dgvcbc.Items.Add("visi3");
The DataGridViewComboBoxColumn is giving me problem. At first, I look like it's working, all the data are there. But if for example I reorder column "Nom", the comboboxcell loose the data that I put in.
I think I understand why, I'm not adding those data into my datatable. So my question is How do I add data into my DataGridViewComboBoxCell throught my datatable ?
Thank you !
-
Maybe You could try with DataGridViewComboBoxColumn.Items instead of DataGridViewComboBoxCell.Items?
-
Thanks, I will check that but the content is different from cell to cell so I don't think I can edit throught DataGridViewComboBoxColumn.
-
Yup, in this scenario it does not make much sense :/
-
In the code below I've created the combobox in specific columns, and populated with elements on cell click event.
I've used a string list and string array to populate the combobox dropdown list which may be different to your requirements.
I haven't included this in the code, but on form load I use an external XML file to populate a datatable which then used as the datagridview source. I then have a save button which overwrites the original XML file by capturing the datagridview and saving it back to XML.
Using this method, when the row header is clicked to filter/change the order of the dgv, the cell combobox selection and dropdown list elements works fine.
Hopefully there is something there that will set you on the right path.
public partial class RuleWindow : Form
{
private object item;
private DataGridViewComboBoxCell combo;
//set item variable to combobox selection text upon selection
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cbx = (ComboBox)sender;
item = cbx.Text;
}
//add comboboxes to relevant columns. Populate with relevant elements
private void datagridview_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == datagridview.Columns[0].Index && e.RowIndex > -1) || (e.ColumnIndex == datagridview.Columns[1].Index && e.RowIndex > -1))
{
if (combo == null)
{
DataGridViewComboBoxCell combo = new DataGridViewComboBoxCell();
//example of adding string array to comboBox
string[] operators = { ">", "<", "≤", "≥", "=" };
combo.Items.AddRange(operators);
this.datagridview[e.ColumnIndex, e.RowIndex] = combo;
}
}
else if (e.ColumnIndex == datagridview.Columns[2].Index && e.RowIndex > -1)
{
if (combo == null)
{
DataGridViewComboBoxCell combo = new DataGridViewComboBoxCell();
//example of adding list to comboBox (uses external extension method not in this code - replace as required)
List<string> layerList = ExtensionMethods.SortedAcadLayerList();
combo.Items.AddRange(layerList.ToArray());
this.datagridview[e.ColumnIndex, e.RowIndex] = combo;
}
}
}
private void datagridview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void datagridview_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == datagridview.Columns[0].Index && e.RowIndex > -1) ||
(e.ColumnIndex == datagridview.Columns[1].Index && e.RowIndex > -1) ||
(e.ColumnIndex == datagridview.Columns[2].Index && e.RowIndex > -1))
{
combo = null;
datagridview[e.ColumnIndex, e.RowIndex] = new DataGridViewTextBoxCell();
datagridview[e.ColumnIndex, e.RowIndex].Value = item;
}
}
}
-
Hi,
Sorry I didn't get back to you. I figure out the problem but thanks anyway for your example. The problem was that after adding my items in the DataGridViewComboBoxto the combobox, I was binding again my datagridview to the datatable, so it was erasing (or overwriting I should say) my items in the DataGridViewComboBoxColumn.
Thanks again !