Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Type into a DataGridView combo box does not change selected Value

$
0
0

I create a datagrid and populate it with dummy data like this

DataGridView dg = new DataGridView();
dg.AutoGenerateColumns = true;

//Populate the combo box datasource
DataTable dtComboData = new DataTable();
dtComboData.Columns.Add("code",typeof(string));
dtComboData.Columns.Add("desc",typeof(string));

DataRow dr = dtComboData.NewRow();
dr["code"] = "IN";
dr["desc"] = "IN";
dtComboData.Rows.Add(dr);

DataRow dr2 = dtComboData.NewRow();
dr2["code"] = "IY20";
dr2["desc"] = "IY20";
dtComboData.Rows.Add(dr2);

DataRow dr3 = dtComboData.NewRow();
dr3["code"] = "R";
dr3["desc"] = "R";
dtComboData.Rows.Add(dr3);

//Create a the combo column
DataGridViewComboBoxColumn colCombo = new DataGridViewComboBoxColumn();
colCombo.Name = "MyColumn";
colCombo.HeaderText = "MyColumn";
colCombo.DataPropertyName = "MyColumn";
colCombo.ValueMember = "code";
colCombo.DisplayMember = "desc";
colCombo.DataSource = dtComboData;
colCombo.ValueType = typeof(string);
dg.Columns.Add(colCombo);

DataTable dtGridData = new DataTable();
dtGridData.Columns.Add("MyColumn",typeof(string));
//Populate the datagrid with dummy data
for(int i=0;i<=12;i++)
{
	DataRow newRow = dtGridData.NewRow();
	if(i<=4)
	{
		newRow["MyColumn"] = "IN";
		dtGridData.Rows.Add(newRow);
	}
	else if(i>4 && i <=8)
	{
		newRow["MyColumn"] = "IY20";
		dtGridData.Rows.Add(newRow);
	}
	else if(i>8 && i <=12)
	{
		newRow["MyColumn"] = "R";
		dtGridData.Rows.Add(newRow);
	}
}

dg.DataSource = dtGridData;
//Set other properties on the datagrid
dg.Name = "theDataGrid";
EnquiryForm.GetControl("Frame1").Controls.Add(dg);
dg.Dock = DockStyle.Fill;
dg.AllowUserToAddRows = false;
dg.AllowUserToDeleteRows = false;
dg.AllowUserToOrderColumns = false;
dg.AllowUserToResizeRows = false;
dg.EditMode = DataGridViewEditMode.EditOnEnter;

//Set some properties on the column
DataGridViewColumn col = dg.Columns["MyColumn"];
col.DisplayIndex = 0;
col.HeaderText = "MyColumn";
// Prevent sorting
col.SortMode = DataGridViewColumnSortMode.Programmatic;
dg.AutoResizeColumn(col.Index);
//Events
dg.EditingControlShowing += 


I set the combo to a allow users to type into it. Users should be able to type or paste into the combo to change the value. The values are restricted to the List.

private void dg_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
	    {
			if(e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
			{
				ComboBox combo = e.Control as ComboBox;
		        if (combo != null)
		        {
					combo.DropDownStyle = ComboBoxStyle.DropDown;
					combo.AutoCompleteSource = AutoCompleteSource.ListItems;
					combo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

		        }
			}
	    }

For some reason, when I click into the First ROW and type a valid value ("R"), and press enter, the R value of the combo does not change.

If I try this on the next row, it works fine. 

Any ideas why this is happening and what a potential fix would be? If you need further info let me know

thanks


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>