A self-learning C# DataGridView Combobox? - c#

I would like to have a column of ComboBoxes in a DataGridView, which allows the user to freely input some text, which is collected in the dropdown menus, so that entering the same text in the next box is faster. I'd prefer not to use DataGridViewComboBoxColumn, unless I really have to.
The following code nearly does the job but has these issues:
After entering some new text and hitting return, the newly entered text is immediately replaced with the old value
But the new text is successfully added to the dropdown menus of all of the comboboxes
when I select this newly added text in one of the boxes, I get DataGridView-Exceptions complaining about an invalid value.
It seems the boxes somehow have for validation purposes a copy of the datasource which doesn't get updated?
public partial class Form1 : Form
{
List<string> data = new List<string>(); // shared data source for all ComboBoxes
private void checkData(string s) // check wether s in the list, add it if not, keep things sorted
{
if (data.Contains(s))
return;
data.Add(s);
data.Sort();
}
private void addCell(string s) // add a new cell to the grid
{
checkData(s);
DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
c.DataSource = data;
c.Value = s;
int i = theGrid.Rows.Add();
theGrid.Rows[i].Cells[0] = c;
}
public Form1()
{
InitializeComponent();
theGrid.ColumnCount = 1;
addCell("Foo");
addCell("Bar");
}
// handler to enable the user to enter free text
private void theGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
cb.DropDownStyle = ComboBoxStyle.DropDown;
}
// handler which adds the entered text to the data source
private void theGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
return;
checkData(e.FormattedValue.ToString());
}
}

After some tests, I am guessing that the individual combo boxes are not getting updated as you think they are. It appears that in the checkData method, that the data is updated with a new s. This will visually update the combo box cell, however the DataSource for each of the combos needs to be updated. Hence the DataError exception when the new added value is selected.
Considering that each combo box cell is “independent” and not part of a DataGridViewComboBoxColumn… then a loop through all the rows is necessary to update each combo box cell. I am not sure why a DataGridViewComboBoxColumn would not be used here.
private void checkData(string s) // check wether s in the list, add it if not, keep things sorted
{
if (data.Contains(s))
return;
data.Add(s);
data.Sort();
// now because each cell is independent... we have to update each data source!
UpdateCombos();
}
private void UpdateCombos() {
foreach (DataGridViewRow row in theGrid.Rows) {
if ((!row.IsNewRow) && (row.Cells[0].Value != null)) {
string currentValue = row.Cells[0].Value.ToString();
DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
c.Value = currentValue;
c.DataSource = data;
row.Cells[0] = c;
}
}
}
Using the posted code, A call to UpdateCombos is added to the checkData method. This method as expected loops through all the rows in the grid and replaces each combo box with the updated data. I will not disagree that it may be prudent to replace the data source, however I would use a combo box column, which the code below does. With this change, the UpdateCombos is not needed and simply update the combo box column.
The DataGridViewComboBoxColumn is exposed since the data source is updated frequently.
private List<string> comboData;
private DataGridViewComboBoxColumn comboColumn;
private void Form2_Load(object sender, EventArgs e) {
comboData = new List<string>();
comboData.Add("Foo");
comboData.Add("Bar");
comboColumn = new DataGridViewComboBoxColumn();
comboColumn.DataSource = comboData;
theGrid2.Columns.Add(comboColumn);
theGrid2.RowCount = 3;
}
private void checkData2(string s) {
if (!comboData.Contains(s)) {
comboData.Add(s);
comboData.Sort();
comboColumn.DataSource = null;
comboColumn.DataSource = comboData;
}
}
Hope that helps

Related

c# Populate datagridview based on ComboBox Item Selected

I have this datagrid where Combobox is populated from Db.
What I'm trying to achieve is that when I select something in the column "Esercizio", the cell of "Video" column auto populate with respective value from the "link_video" column of Db.
So if I select "kickback", I need to see the link video of kickback from db in the textbox cell.
Here's the code that i use to populate the combobox on form load:
private void Myform_Load(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=(LocalDB)\\etc");
cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM Esercizi";
dr = cmd.ExecuteReader();
while (dr.Read())
{
//populate Column1 combobox with "nome" column from Esercizi db table
Column1.Items.Add(dr["nome"]);
}
con.Close();
}
datagridview
EDIT
I've figured out with 2 new problems.
I'm trying to load a saved workout from db but when I do this, no video link populate the dgv as the grid event doesn't fire.
What I've tried is to add a foreach loop to a new selectionindexchanged function and to fire it at the end of the Load Button code like this:
private void curCombo_LoadedValues(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (curCombo != null && curCombo.SelectedValue != null)
{
ExerciseAndVideo selectedExercise = (ExerciseAndVideo)curCombo.SelectedItem;
dataGridView1.CurrentRow.Cells["Video"].Value = selectedExercise.Video;
}
}
}
}
private void button9_Click(object sender, EventArgs e){
string connectionString = "Data Source=(LocalDB)\\etc";
string sql = "SELECT * FROM Schede WHERE Id = 6 AND dgv = 'dataGridView1'";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);using (DataTable dt = new DataTable())
{
dataadapter.Fill(dt);
//Set AutoGenerateColumns False
dataGridView1.AutoGenerateColumns = false;
//Set Columns Count
dataGridView1.ColumnCount = 6;
//Add Columns
dataGridView1.Columns[0].Name = "Esercizio";
dataGridView1.Columns[0].HeaderText = "Esercizio";
dataGridView1.Columns[0].DataPropertyName = "Esercizio";
dataGridView1.Columns[1].Name = "Serie";
dataGridView1.Columns[1].HeaderText = "Serie";
dataGridView1.Columns[1].DataPropertyName = "Serie";
dataGridView1.Columns[2].HeaderText = "Ripetizioni";
dataGridView1.Columns[2].Name = "Ripetizioni";
dataGridView1.Columns[2].DataPropertyName = "Ripetizioni";
dataGridView1.Columns[3].Name = "Recupero";
dataGridView1.Columns[3].HeaderText = "Recupero";
dataGridView1.Columns[3].DataPropertyName = "Recupero";
dataGridView1.Columns[4].Name = "Time Under Tension";
dataGridView1.Columns[4].HeaderText = "Time Under Tension";
dataGridView1.Columns[4].DataPropertyName = "Time_Under_Tension";
dataGridView1.DataSource = dt;
connection.Close();
}
curCombo_LoadedValues();
}
But I get this error "the are no arguments for the obligatory parameter sender...
How I can call it correctly?
The second Issue is that when I populate some dgv columns like this, combos stops working correctly and I get an error exception on the combobox :
dataGridView1.Rows.Add(7);
Random rnd = new Random();
dataGridView1.Rows[0].Cells[1].Value = 3;
dataGridView1.Rows[0].Cells[2].Value = rnd.Next(1, 13);
dataGridView1.Rows[0].Cells[3].Value = 1;
dataGridView1.Rows[0].Cells[4].Value = 201;
dataGridView1.Rows[1].Cells[1].Value = 2;
dataGridView1.Rows[1].Cells[2].Value = rnd.Next(1, 13);
dataGridView1.Rows[1].Cells[3].Value = 1;
dataGridView1.Rows[1].Cells[4].Value = 201;
dataGridView1.Rows[2].Cells[1].Value = 3;
dataGridView1.Rows[2].Cells[2].Value = rnd.Next(1, 13);
dataGridView1.Rows[2].Cells[3].Value = 1;
dataGridView1.Rows[2].Cells[4].Value = 201;
dataGridView1.Rows[3].Cells[1].Value = 4;
dataGridView1.Rows[3].Cells[2].Value = rnd.Next(1, 13);
dataGridView1.Rows[3].Cells[3].Value = 1;
dataGridView1.Rows[3].Cells[4].Value = 201;
dataGridView1.Rows[4].Cells[1].Value = 5;
dataGridView1.Rows[4].Cells[2].Value = rnd.Next(1, 13);
dataGridView1.Rows[4].Cells[3].Value = 1;
dataGridView1.Rows[4].Cells[4].Value = 201;
dataGridView1.Rows[5].Cells[1].Value = 6;
dataGridView1.Rows[5].Cells[2].Value = rnd.Next(1, 13);
dataGridView1.Rows[5].Cells[3].Value = 1;
dataGridView1.Rows[5].Cells[4].Value = 201;
dataGridView1.Rows[6].Cells[1].Value = 7;
dataGridView1.Rows[6].Cells[2].Value = rnd.Next(1, 13);
dataGridView1.Rows[6].Cells[3].Value = 1;
dataGridView1.Rows[6].Cells[4].Value = 201;
This is the look of the dgv now:
dgv
And this is the error the i get after combos stop working correctly (I click and no dropdown appear or if I click 2-3 times, a random item get selected but no video link appear in the other column):
error
In the posted code, it shows a query to a DB to get the exercise names and adds those names as the items to display in “each” combo box cell in that column. This is fine, however, there is zero (0) information about “which video” belongs to each of the items in the combo boxes list of items. I will assume the “relation” to which video is related to which exercise would involve another query to the DB.
If this is the case, then it is easy to see that when a combo box in the grid is changed, that you could simply query the DB for which video to use for the selected exercise. This would certainly work; however, it creates a redundancy issue. Example, let’s say the user selected “kicks” in the combo box in row 1 in the gird. The code queries the DB and gets the location of the “kicks” video and sets the “video” cell to this videos path. Then later, the user selects “kicks” again for some other row. This will re-query the DB for the SAME data we previously got. You want to avoid querying the DB unnecessarily.
So given this, it appears a better approach to avoid re-querying the DB unnecessarily, is that we somehow “combine” the exercise with the particular video that the exercise uses. You could do this ONCE when the form loads. Once you have the exercises, then loop though each exercise and query the DB for that exercises video and combine this with the exercise. Once we have the video link, we “save” this info. With this approach, you will not have to re-query the DB for any given exercise since we have saved that info for all exercises.
There are a myriad number of ways to “combine” the exercise with the video. One possible solution is to create a Class that has these two properties. Let’s call this Class ExerciseAndVideo… it has two properties, a string Exercise that is the exercise text and a string Video that defines the path to the video for that Exercise. This simple class may look something like…
public class ExerciseAndVideo {
public string Exercise { get; set; }
public string Video { get; set; }
}
This is one approach to “combine” an Exercise with a particular Video. We could then make a list of ExerciseAndVideo objects like…
List<ExerciseAndVideo> ExerciseList;
Then we cannot only use this list as a DataSource to the combo box column, but we could also use this list as a way to easily tell “which” video belongs to “which” exercise. The example below uses this strategy.
How to implement this in the DataGridView…
One thing to keep in mind is that a DataGridViewComboBoxCell is “different monster” than a “regular” ComboBox. Example; for a regular combo box, there is an event called… SelectedIndexChanged event. This event fires when the user “changes” the combo boxes currently selected index. On the other hand, a DataGridViewComboBoxCell does NOT have a SelectedIndexChanged event, which I assume you may be aware of. The grid’s absence of this event makes sense, because the grid’s combo box “column” may have MANY combo boxes in it.
Fortunately, even though the DataGridViewComboBoxCell does not have a SelectedIndexChanged event, we CAN cast an “individual” combo box cell to a ComboBox (in this case the combo box cell being edited), THEN we CAN subscribe to that ComboBoxes SelectedIndexChanged event. Then we could simply wait until the SelectedIndexChanged event fires then update the video cell data with the appropriate video link.
The DataGridView provides a special event for this called… EditingControlShowing. This event will fire when the user starts to “edit” a cell in the grid. In this particular case, this event would fire when the user clicks into a combo box cell and “starts” to change the cells value. Is what we want to do in this event is simply cast that combo box cell to a regular ComboBox…, THEN, subscribe to that ComboBoxes SelectedIndexChanged event which we will implement below.
The strategy goes like this… we will make a “global” ComboBox variable we will call curCombo. When the grids EditingControlShowing event fires and we see that the edited cell is an “Exercise” cell… Then we will cast THAT combo box cell in the grid to the global curCombo variable. Then we will subscribe to that combo boxes SelectedIndexChanged event. When the user “leaves” the cell we will unsubscribe from the global variables curCombo SelectedIndexChanged event to keep things clean.
Therefore, given this, the grids EditingControlShowing event may look something like below...
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name == "Exercise") {
curCombo = e.Control as ComboBox;
if (curCombo != null) {
curCombo.SelectedIndexChanged -= new EventHandler(curCombo_SelectedIndexChanged);
curCombo.SelectedIndexChanged += new EventHandler(curCombo_SelectedIndexChanged);
}
}
}
Obviously we need to implement the event handler curCombo_SelectedIndexChanged. In this event, we would know that previously, the user selected a combo box cell and has changed the value in that cell to some other value. Since the Exercise changed, we know we need to change the “Video” cell.
Again there are numerous ways you could do this, however, if we set the grid’s “Exercise” combo box column’s DataSource as a List of ExerciseAndVideo objects, then we should be able to get that particular ExerciseAndVideo object directly from the global ComboBox curCombo. This will tell us “which” video to place in the “Video” cell. This may look something like…
private void curCombo_SelectedIndexChanged(object sender, EventArgs e) {
if (curCombo != null && curCombo.SelectedValue != null) {
ExerciseAndVideo selectedExercise = (ExerciseAndVideo)curCombo.SelectedItem;
dataGridView1.CurrentRow.Cells["Video"].Value = selectedExercise.Video;
}
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "Exercise") {
if (curCombo != null) {
curCombo.SelectedIndexChanged -= new EventHandler(curCombo_SelectedIndexChanged);
}
}
}
To complete the example and putting all this together may produce something like below…
List<ExerciseAndVideo> ExerciseList;
ComboBox curCombo;
public Form2() {
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e) {
ExerciseList = GetExerciseVideoComboBoxListFromDB();
dataGridView1.Columns.Add(GetExcerciseComboBoxColumn(ExerciseList));
dataGridView1.Columns.Add(GetLinkColumn());
dataGridView1.CellLeave += new DataGridViewCellEventHandler(dataGridView1_CellLeave);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
private DataGridViewComboBoxColumn GetExcerciseComboBoxColumn(List<ExerciseAndVideo> exerciseData) {
DataGridViewComboBoxColumn cbCol = new DataGridViewComboBoxColumn();
cbCol.HeaderText = "Exercise";
cbCol.Name = "Exercise";
cbCol.DisplayMember = "Exercise";
cbCol.DataSource = exerciseData;
return cbCol;
}
private DataGridViewLinkColumn GetLinkColumn() {
DataGridViewLinkColumn col = new DataGridViewLinkColumn();
col.HeaderText = "Video";
col.Name = "Video";
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
return col;
}
private List<ExerciseAndVideo> GetExerciseVideoComboBoxListFromDB() {
List<ExerciseAndVideo> exerciseList = new List<ExerciseAndVideo>();
exerciseList.Add(new ExerciseAndVideo { Exercise = "Crosses", Video = #"C:/somepath/Crosses.htm/" });
exerciseList.Add(new ExerciseAndVideo { Exercise = "Kickback", Video = #"C:/somepath/Kickback.htm" });
exerciseList.Add(new ExerciseAndVideo { Exercise = "Leg Extensions", Video = #"C:/somepath/LegExtensions.htm" });
exerciseList.Add(new ExerciseAndVideo { Exercise = "Crunches", Video = #"C:/somepath/Crunches.htm" });
exerciseList.Add(new ExerciseAndVideo { Exercise = "Pushups", Video = #"C:/somepath/Pushups.htm" });
return exerciseList;
}
What if the grid has a data source?
This works as expected when the grid has no data source. However, if the grid has a data source and one of the columns/properties in the data source is bound to our “Exercise” combo box column, then, is what will happen… is that after the data is loaded, the combo boxes should display the proper exercise, however, all the video cells will remain empty. This is obviously because the grid events are not fired when the data is loaded.
So, in that case, you will want a method that loops through all the rows in the grid, checks what the exercise value is for that row, then set the video cell value to the correct video link. Since you do not say if the grid does or does not have a data source, I will assume this is all you need. If there is a data source, I recommend you check each “Exercise” to make sure the “Exercises” in the data are in the combo boxes list of items, if one or more “Exercises” are in the data that are not in the combo box columns list of items, then you will get the grids DataError when you attempt to set the grids data source.
I hope this makes sense.
Edit... An example of setting the video cell after the data has been loaded into the grid.
private void SetVideoCellsAfterDataLoad() {
foreach (DataGridViewRow row in dataGridView1.Rows) {
if (!row.IsNewRow && row.Cells["Exercise"].Value != null) {
foreach (ExerciseAndVideo eav in ExerciseList) {
if (row.Cells["Exercise"].Value.ToString() == eav.Exercise) {
row.Cells["Video"].Value = eav.Video;
break;
}
}
}
}
}
I mixed ComboBox with DatagridViewComboboxColumn.
It's partly your fault :).
Here you have a form with events. Since CellValueChanged fires on cell exit, I added a Dirty StateEvent to update the Video column.
From the designer, just put the datagrid in the form and make sure the name is the same.
IMHO, these 3 events are crucial
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Init();
}
private void Init()
{
var list = new List<Exercise>() {
new Exercise (){Name="Name1", Link= "Link1" },
new Exercise (){Name="Name3", Link= "Link3" },
new Exercise (){Name="Name4", Link= "Link4" },
};
var comboColumn = new DataGridViewComboBoxColumn() { Name = "ExerciseName", CellTemplate = new DataGridViewComboBoxCell() };
comboColumn.DisplayMember = nameof(Exercise.Name);
comboColumn.ValueMember = nameof(Exercise.Link);
comboColumn.DataSource = list;
dataGridView1.Columns.Add(comboColumn);
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Video" });
dataGridView1.CellContentClick += DataGridView1_CellContentClick;
dataGridView1.CellValueChanged += DataGridView1_CellValueChanged;
dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_CurrentCellDirtyStateChanged;
}
private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
var currentCell = (sender as DataGridView).CurrentCell;
if(currentCell.ColumnIndex == 0)
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 0)
return;
var comboCell = (dataGridView1.Rows[e.RowIndex].Cells[0] as DataGridViewComboBoxCell);
var value = comboCell.Value;
dataGridView1.Rows[e.RowIndex].Cells["Video"].Value = value;
}
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
public class Exercise
{
public string Name { get; set; }
public string Link { get; set; }
}

Not getting the typed text in editable combobox

In my datagridview, i have a textboxcolumn and an editable combobox column in winforms.But while typing the new value in combobox text and pressing the enter key, i am not getting the typed value as the corresponding cell value.Could anyone please help with this.
private void dgv_customAttributes_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = dgv_customAttributes.CurrentRow;
if (row.Cells[1].Value.ToString() != null)
{
//Here the selectedVal is giving the old value instead of the new typed text
string SelectedVal = row.Cells[1].Value.ToString();
foreach (CustomAttribute attribute in customAttributes)
{
if (row.Cells[0].Value.ToString() == attribute.AttributeName)
{
attribute.AttributeValue = SelectedVal;
break;
}
}
}
}
You need to find out the combo boxes as they are being shown, and attached an event handler to them for when the selected index changes (since it is not possible to obtain that information from the column or the cell themselves).
This means that, unfortunately, capturing the event CellEndEdit is useless.
In the following example, a text box is filled with the chosen option, but you can do anything else, such as selecting a specific value in your enumerated variable or whatever.
void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
{
if ( e.Control is ComboBox comboEdited ) {
// Can also be set in the column, globally for all combo boxes
comboEdited.DataSource = ListBoxItems;
comboEdited.AutoCompleteMode = AutoCompleteMode.Append;
comboEdited.AutoCompleteSource = AutoCompleteSource.ListItems;
// Attach event handler
comboEdited.SelectedValueChanged +=
(sender, evt) => this.OnComboSelectedValueChanged( sender );
}
return;
}
void OnComboSelectedValueChanged(object sender)
{
string selectedValue;
ComboBox comboBox = (ComboBox) sender;
int selectedIndex = comboBox.SelectedIndex;
if ( selectedIndex >= 0 ) {
selectedValue = ListBoxItems[ selectedIndex ];
} else {
selectedValue = comboBox.Text;
}
this.Form.EdSelected.Text = selectedValue;
}
Find the complete source code for the table in which a column is a combobox in GitHub.
Hope this helps.

Updating the existing DataGridViewComboBoxColumn Items collection

We have DataGridViewComboBoxColumn in which we have four values which are fixed. During run-time when the event dataGridView1_EditingControlShowing occurs we are trying to append new items to DataGridViewComboBoxColumn.
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.DropDown += new System.EventHandler(ComboBox1_DropDown);
}
}
private void ComboBox1_DropDown(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (comboBox.Items != null)
{
List<String> elementname = Elements();
foreach (string s in elementname)
{
if (!comboBox.Items.Contains(s))
{
comboBox.Items.Add(s);
}
}
}
}
I am getting this exception :
Can you please suggest how to add the values to the existing DataGridViewComboBoxColumn in Items collection.
You are adding into the Items of editing control but not into the Items of the ComboBoxColumn, which will eventually be used for validation.
To get easy access to the hosting DGV we use the special type DataGridViewComboBoxEditingControl.
Now we will add the new choices to both Items collections now in the ComboBox1_DropDown event: :
DataGridViewComboBoxEditingControl comboBox = (DataGridViewComboBoxEditingControl)sender;
DataGridView dgv = comboBox.EditingControlDataGridView;
if (comboBox.Items != null && dgv != null)
{
DataGridViewComboBoxColumn dcbc =
(DataGridViewComboBoxColumn) dgv.Columns[dgv .CurrentCell.ColumnIndex];
List<String> elementname = Elements.ToList();
foreach (string s in elementname)
{
if (!comboBox.Items.Contains(s))
{
comboBox.Items.Add(s);
dcbc.Items.Add(s);
}
}
}
Note:
The new Items will not be persistet unless you code for it.
So if you have set fields to new values and saved them, you must also save and reload those new values before re-loading the DGV or else the values will not be in the Column's list of values and throw the DataError again!
The usual place to store them would be a DataTable in your DBMS, but any other external storage may also be used like an XML file or maybe dynamic resources etc.. But the DBMS is the most natural choice imo.

C# DataGridView DataGridViewTextCell to DataGridViewComboBoxCell editable text

I have a DataGridView with a TextBoxColumn. I would like to be able to click on the cell to enter edit mode, and when I do a drop down will appear with options for the user to pick, or if the user does not want one of those options, they are able to edit the cell (as if there was no drop down). Then when the user leaves the cell, the value (either what they typed, or what they chose) will be saved.
There are lots of answers to adding the user typed choice to the drop down list, but this is not what I want. I just want to have some common options for the user to consider before they go off and make their own choice.
I do not want to have a button to popup another input dialog. I do not want to chanbe the column to a ComboBoxColumn. I do not care if the dropdown arrow is shown at all times or not.
I have tried to change the TextBoxCell to a ComboBoxCell on EditingContolShowing, but this is proving to be a pointless effort.
Are there any suggestions for this?
One option you could use is AutoComplete. This can mimic most of the desired behavior on a DataGridViewTextCell, with exception of displaying all options on entering the textcell, and without the need of converting the cell type to a ComboBox.
This could be handled in the DataGridView EditingControlShowing event:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
TextBox box = e.Control as TextBox;
box.AutoCompleteCustomSource = new AutoCompleteStringCollection();
box.AutoCompleteCustomSource.AddRange(new string[] { "Foo", "Bar", "Baz" });
box.AutoCompleteMode = AutoCompleteMode.Suggest;
box.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
Given, the user must enter text for any option to show. If the desired behavior requires all options to show upon entering the textbox, this would not be your best option. But if that is secondary to all other required behaviors (suggested options, accepts non-option entries, doesn't always have to show, etc), this is a viable solution.
Edit
This worked in all the following situations:
DataGridView is data bound.
Binding the DataSource:
public BindingList<Example> Examples { get; set; }
this.Examples = new BindingList<Example>();
dataGridView1.DataSource = this.Examples;
Where Example is a simple class:
public class Example
{
public string First { get; set; }
public string Last { get; set; }
public string Test { get; set; }
}
Manually adding column(s).
Just an empty column:
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = "Extra";
col.HeaderText = "Extra";
this.dataGridView1.Columns.Add(col);
Both 1 and 2 combined.
Just set the cells you want to be a ComboBox to the DataGridViewComboBoxCell type:
var cb1 = new DataGridViewComboBoxCell();
cb1.Items.Add("Yes");
cb1.Items.Add("No");
dgv.Rows[1].Cells[1] = cb1;
var cb2 = new DataGridViewComboBoxCell();
cb2.Items.Add("Blue");
cb2.Items.Add("Red");
dgv.Rows[3].Cells[1] = cb2;
Then use the EditingControlShowing event to change the style of the DropDown to allow editing:
void dgv_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e) {
ComboBox cb = e.Control as ComboBox;
if (cb != null) {
cb.DropDownStyle = ComboBoxStyle.DropDown;
}
}
Use the CellValidating event to add any typed items into the list that aren't already there:
void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
var cb = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
if (cb != null && !cb.Items.Contains(e.FormattedValue)) {
cb.Items.Add(e.FormattedValue);
if (dgv.IsCurrentCellDirty) {
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = e.FormattedValue;
}
}

Datagridview: How to set a cell in editing mode?

I need to programmatically set a cell in editing mode. I know that setting that cell as CurrentCell and then call the method BeginEdit(bool), it should happen, but in my case, it doesn't.
I really want that, with my DGV with several columns, the user can ONLY select and also edit the first two. The other columns are already read-only, but the user can select them, and that is what I don't want.
So I was thinking, tell the user to TAB everytime it has finished writing on the cell, then select the second cell, then tab again and it select and begin edit the next row's first cell...
How can I do this?
Setting the CurrentCell and then calling BeginEdit(true) works well for me.
The following code shows an eventHandler for the KeyDown event that sets a cell to be editable.
My example only implements one of the required key press overrides but in theory the others should work the same. (and I'm always setting the [0][0] cell to be editable but any other cell should work)
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && dataGridView1.CurrentCell.ColumnIndex == 1)
{
e.Handled = true;
DataGridViewCell cell = dataGridView1.Rows[0].Cells[0];
dataGridView1.CurrentCell = cell;
dataGridView1.BeginEdit(true);
}
}
If you haven't found it previously, the DataGridView FAQ is a great resource, written by the program manager for the DataGridView control, which covers most of what you could want to do with the control.
private void DgvRoomInformation_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (DgvRoomInformation.CurrentCell.ColumnIndex == 4) //example-'Column index=4'
{
DgvRoomInformation.BeginEdit(true);
}
}
Well, I would check if any of your columns are set as ReadOnly. I have never had to use BeginEdit, but maybe there is some legitimate use. Once you have done dataGridView1.Columns[".."].ReadOnly = False;, the fields that are not ReadOnly should be editable. You can use the DataGridView CellEnter event to determine what cell was entered and then turn on editing on those cells after you have passed editing from the first two columns to the next set of columns and turn off editing on the last two columns.
I know this question is pretty old, but figured I'd share some demo code this question helped me with.
Create a Form with a Button and a DataGridView
Register a Click event for button1
Register a CellClick event for DataGridView1
Set DataGridView1's property EditMode to EditProgrammatically
Paste the following code into Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable m_dataTable;
DataTable table { get { return m_dataTable; } set { m_dataTable = value; } }
private const string m_nameCol = "Name";
private const string m_choiceCol = "Choice";
public Form1()
{
InitializeComponent();
}
class Options
{
public int m_Index { get; set; }
public string m_Text { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
table = new DataTable();
table.Columns.Add(m_nameCol);
table.Rows.Add(new object[] { "Foo" });
table.Rows.Add(new object[] { "Bob" });
table.Rows.Add(new object[] { "Timn" });
table.Rows.Add(new object[] { "Fred" });
dataGridView1.DataSource = table;
if (!dataGridView1.Columns.Contains(m_choiceCol))
{
DataGridViewTextBoxColumn txtCol = new DataGridViewTextBoxColumn();
txtCol.Name = m_choiceCol;
dataGridView1.Columns.Add(txtCol);
}
List<Options> oList = new List<Options>();
oList.Add(new Options() { m_Index = 0, m_Text = "None" });
for (int i = 1; i < 10; i++)
{
oList.Add(new Options() { m_Index = i, m_Text = "Op" + i });
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i += 2)
{
DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
//Setup A
c.DataSource = oList;
c.Value = oList[0].m_Text;
c.ValueMember = "m_Text";
c.DisplayMember = "m_Text";
c.ValueType = typeof(string);
////Setup B
//c.DataSource = oList;
//c.Value = 0;
//c.ValueMember = "m_Index";
//c.DisplayMember = "m_Text";
//c.ValueType = typeof(int);
//Result is the same A or B
dataGridView1[m_choiceCol, i] = c;
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns.IndexOf(dataGridView1.Columns[m_choiceCol]))
{
DataGridViewCell cell = dataGridView1[m_choiceCol, e.RowIndex];
dataGridView1.CurrentCell = cell;
dataGridView1.BeginEdit(true);
}
}
}
}
}
Note that the column index numbers can change from multiple button presses of button one, so I always refer to the columns by name not index value. I needed to incorporate David Hall's answer into my demo that already had ComboBoxes so his answer worked really well.
The question is old, but the proposed solutions did not help in my situation.
It was necessary to select the cell when loading the form.
This option did not work:
private void FOperations_Load(object sender, EventArgs e)
{
dgvOperations.CurrentCell = dgvOperations[nameof(Operation.DisplayName), 0];
dgvOperations.Select();
}
If you make a cell selection in the "Layout" event, then everything is successful:
private void dgvOperation_Layout(object sender, LayoutEventArgs e)
{
dgvOperations.CurrentCell = dgvOperations[nameof(Operation.DisplayName), 0];
dgvOperations.Select();
}
I know this is an old question, but none of the answers worked for me, because I wanted to reliably (always be able to) set the cell into edit mode when possibly executing other events like Toolbar Button clicks, menu selections, etc. that may affect the default focus after those events return. I ended up needing a timer and invoke. The following code is in a new component derived from DataGridView. This code allows me to simply make a call to myXDataGridView.CurrentRow_SelectCellFocus(myDataPropertyName); anytime I want to arbitrarily set a databound cell to edit mode (assuming the cell is Not in ReadOnly mode).
// If the DGV does not have Focus prior to a toolbar button Click,
// then the toolbar button will have focus after its Click event handler returns.
// To reliably set focus to the DGV, we need to time it to happen After event handler procedure returns.
private string m_SelectCellFocus_DataPropertyName = "";
private System.Timers.Timer timer_CellFocus = null;
public void CurrentRow_SelectCellFocus(string sDataPropertyName)
{
// This procedure is called by a Toolbar Button's Click Event to select and set focus to a Cell in the DGV's Current Row.
m_SelectCellFocus_DataPropertyName = sDataPropertyName;
timer_CellFocus = new System.Timers.Timer(10);
timer_CellFocus.Elapsed += TimerElapsed_CurrentRowSelectCellFocus;
timer_CellFocus.Start();
}
void TimerElapsed_CurrentRowSelectCellFocus(object sender, System.Timers.ElapsedEventArgs e)
{
timer_CellFocus.Stop();
timer_CellFocus.Elapsed -= TimerElapsed_CurrentRowSelectCellFocus;
timer_CellFocus.Dispose();
// We have to Invoke the method to avoid raising a threading error
this.Invoke((MethodInvoker)delegate
{
Select_Cell(m_SelectCellFocus_DataPropertyName);
});
}
private void Select_Cell(string sDataPropertyName)
{
/// When the Edit Mode is Enabled, set the initial cell to the Description
foreach (DataGridViewCell dgvc in this.SelectedCells)
{
// Clear previously selected cells
dgvc.Selected = false;
}
foreach (DataGridViewCell dgvc in this.CurrentRow.Cells)
{
// Select the Cell by its DataPropertyName
if (dgvc.OwningColumn.DataPropertyName == sDataPropertyName)
{
this.CurrentCell = dgvc;
dgvc.Selected = true;
this.Focus();
return;
}
}
}
I finally found an answer to this. In my case, I wanted to select a specific index or item after adding a new row, but this should apply to other situations.
The cell doesnt hold the combobox controls per say. The DGV does, it holds the controls of the current cell. So you have to make the current cell the combo cell, then go in edit mode, then cast the dgv controls as ComboBox, then you will have access to the selectedIndex and selectedItem methods
Dim rowIndex = myDgv.Rows.Add()
myDgv.ClearSelection()
myDgv.CurrentCell = myDgv.Rows(rowIndex).Cells("colName")
myDgv.BeginEdit(True)
Dim myCombo as ComboBox = CType(myDgv.EditingControl, ComboBox)
myCombo.SelectedIndex = 3

Categories

Resources