C# DatagridviewComboBox - c#

In a Winform application, how can I manage this case :
I have a datagridview, with 2 columns, each contains a datagridviewcombobox
In the first I would list the constructor of some vehicles (BMW, FORD, etc)
In the second I would like that the content of the combobox is refreshed depending what I selected in the first combo, for example to display the model of the car
Thank you for any help
Best Regards
Austin

You can Handle the event handle SelectedIndexChanged so whenever the index is changed refresh the data example :
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
your_refresh_method_here();//
}
}
Please note that e is the item selected by the user
For any additional data please refer to:
Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged

Related

C# datgrid view combobox cell binding

I need two combo box cell in my data grid view that when we select item in first one the second binding source should be change to something
for example :
we select a bank name from the first combo box the second combo box item should be the items that belong to that bank
My binding combo box is properly working but in any event when I try to handle the changing the second datasource it have given me error like image below,
datagridview error datagridviewcomboboxcell not validate how can it be?
error datagrid view image
[1]: https://i.stack.imgur.com/u9yMO.png
DataGridView's default error handling seems to work on assumption that it cannot assume anything. So, normally it's quite safe to just override it.
myGrid.DataError += myGrid_DataError;
private void myGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
// ignore
}
Also gives you a place where to write actual error handling, should it become necessary.

Colorcoding based on ComboBox item

I'm using c# and Visual Studio 2010. I have a tablelayoutpanel with 8 rows and 8 columns. In one column the user will pick an item from a dropdown list in a combobox. Based on their choice, the row should assume a certain color. Any help would greatly be appreciated.
Add an event handler to your Combo Box.
public void MyComboBox_OnSelectedItemChanged(object sender, EventArgs e)
{
//change the color here
}
Make sure you add the event handler to the ComboBox properties.

accessing dynamically created control variables from event handler c#

I have a program that I am trying to write that
one row of the comboboxes and text boxes are created dynamically when you click the plus button that is on the form
and then you can keep clicking the plus and it keeps adding a row but moves the new row down
I have the row organized like this:
there is a comboBox for buildings, then a comboBox for rooms. And that is one row
The building comboboxes have lists of buildings in them, and when I choose an item from the list, I need to populate the room combobox with the list of room numbers that correspond with the building.
I am having an extremely hard time doing this.
This is what Ive tried.
my row class is what I use to create the new rows.
When I press the button, a new row is created with new comboboxes etc.
Here is my event handler
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
row.roomComboBox.Items.Add(x);
}
}
Now my problem is I need to somehow add the corresponding room number data to the roomComboBox associated with the current row, and I dont know how and its driving me nuts.
The sender comboBox is associated with the current row, so is there a way to use the sender to reference the roomComboBox that is a member of the same row??
any help would be great. Ive searched lots of threads on here and cant seem to find an answer.
edit:
Is there a way to reference the object that a variable belongs to.
If I could somehow reference the row object of the sender comboBox then i might could use that to reference the room comboBox of the same row object...maybe? somehow? please?
When you create your comboboxes for each row, you can set the Building combobox's Tag property to the Room Numbers combobox. That will let you retrieve the associated combobox for that row. i.e...
// create combobox for row N...
ComboBox cmbBuilding = new ComboBox();
ComboBox cmbRooms = new ComboBox();
// store rooms combobox in building combobox
cmbBuilding.Tag = cmbRooms;
// ...
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
// get the room combobox
ComboBox cmbRooms = comboBox.Tag as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
cmbRooms.Items.Add(x);
}
}
Hope this little snippet will help
(sender as ComboBox).Parent.Controls.Find("cmbRooms",true)
This code will get the control with id "cmbRooms" with reference to the ComboBox sender
Use a DataRepeater Control for Windows Forms. It is included in the Visual Basic Power Packs but can be used in C# projects as well.
Another option is to use a DataGridView.

How to select a WinForm DataGridView value, for a specific column, when anywhere along the entire row is clicked?

I've got a simple WinForm app which has a DataGridView on it. When the user clicks -anywhere- along the row results, I'm hoping to retrieve the value of the first column (i'll hard code that) which happens to be the ID of that row.
Anyone have any suggestions to how I can do this?
Lets assume the ID of the gridview is DataGridView1 <-- Bonus points for using the autoegenerated Id's :P
private void DataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
var value= DataGridView1[ID_ColumnName.Index,e.RowIndex].Value
}
You can also use CellMouseClick event handler and use the same code.
I hope you know how to get the event handlers.

Datagridview execute code on change of the column combo box value

I am trying to reload the datagridview on change of the combobox value in on of that datagridview columns. I know how to do the reload, but I am having trouble triggering the action. Should I be looking for value change in this particular cell or is there a change in comobobox action?
I typically use ComboBox_TextChanged events and you can use those within a datagridview as well, as you add them:
comboBox1.TextChanged += delegate(object sender, EventArgs e)
{
// Do Whatever
}
Do a DataGridView1.Refresh() on the combobox change event.

Categories