WPF ComboBox GotFocus - c#

I have a DataGrid with a ComboBox column and I need to add items to that combobox only when it get focus. I handle the event GotFocus, but still have a problem. In fact once I select an item from the dropdown the event will fire again. Any workaround on how to fix this issue.
private void CmbxGotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !this.returnedFocus)
{
//dowork
}
}
private void CmbxLostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
this.returnedFocus = cb.IsDropDownOpen;
}
}
}

Related

How to select value in DatagridviewComboboxColumn Cell

I have a datagridview which contain a Combobox Column i want when i select a add value from the combobox it shows a new form. i tried this code but it doesn't work:
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
switch (dataGridView2.Columns[e.ColumnIndex].Name)
{
case "CategorieDataGridViewTextBoxColumn":
if (dataGridView2.Rows[e.RowIndex].Cells["CategorieDataGridViewTextBoxColumn"].Value.ToString() == "Add")
{
Categorie cat = new Categorie();
cat.Show();
}
break;
}
}
So how can i do it??
You should handle the event when a value is changed in a ComboBox in a DataGridView cell. try this code which will fire the event of the selection in the comboBox in the dataGridView:
public Form1()
{
InitializeComponent();
DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
dataGridView2.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView2_EditingControlShowing);
}
private void dataGridView2_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 ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
string item = cb.Text;
if (item == "Add")
{
Categorie cat = new Categorie();
cat.Show();
}
}

Adding ComboBox to Datagridview

I found a way to add a combobox to DataGridview (Winform) cell, but I have not found an event like ItemDataBound of DataGridView to set a value to comboBox. And do not know how to set a selected value of a comboBox to DataItem property of a current row (of a DataGridView) :(
Please give me some clues to do this task
Thanks you so much
You can use below method to add data to a combobox in gridview. If you dont have a list you can add items to the combobox as:
cmbdgv.Items.Add("Test");
private void bindDataToDataGridViewCombo() {
DataGridViewComboBoxColumn cmbdgv = new DataGridViewComboBoxColumn();
List<String> itemCodeList = new List<String>();
cmbdgv.DataSource = itemCodeList;
cmbdgv.HeaderText = "Test";
cmbdgv.Name = "Test";
cmbdgv.Width = 270;
cmbdgv.Columns.Add(dgvCmbForums);
cmbdgv.Columns["Test"].DisplayIndex = 0;
}
After adding if you want to capture the combobox selection change you can use below event in the datagridview.
ComboBox cbm;
DataGridViewCell currentCell;
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
cbm = (ComboBox)e.Control;
if (cbm != null)
{
cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
}
currentCell = this.dataGridView1.CurrentCell;
}
}
void cbm_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new MethodInvoker(EndEdit));
}
void EndEdit()
{
if (cbm != null)
{
string SelectedItem=cbm.SelectedItem.ToString();
int i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows[i].Cells["Test"].Value = SelectedItem;
}
}
If you are trying to set the value to a Combobox in a DataGridView, see if this answer will help.
To get the selected item of the Combobox (example):
comboBox.SelectedIndexChanged += new EventHandler(comboBox_ComboSelectionChanged);
private void comboBox_ComboSelectionChanged(object sender, EventArgs e)
{
if (myDGV.CurrentCell.ColumnIndex == 5)
{
int selectedIndex;
string selectedItem;
selectedIndex = ((ComboBox)sender).SelectedIndex; // handle an error here.
// get the selected item from the combobox
var combo = sender as ComboBox;
if (selectedIndex == -1)
{
MessageBox.Show("No value has been selected");
}
else
{
// note that SelectedItem may be null
selectedItem = combo.SelectedItem.ToString();
if (selectedItem != null)
{
// Your code

Control stuck inside an infinite loop because of checkbox events in Datagrid in WPF

I have a datagrid in WPF with first column as a checkbox. I have now added a select all checkbox which appears on the header of the column that it is bound to.
The handlers for the select all checkbox and individual row check boxes are defined as:
private void SelectAllCheckBox_Checked(object sender, RoutedEventArgs e)
{
foreach (BaseDataItem objItem in BaseReleaseList)
{
objItem.Select = true;
}
BaseReleaseDataGridView.Items.Refresh();
}
private void SelectAllCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
foreach (BaseDataItem objItem in BaseReleaseList)
{
objItem.Select = false;
}
BaseReleaseDataGridView.Items.Refresh();
}
private void RowCheckBoxUnchecked(object sender, RoutedEventArgs e)
{
SelectAllCheckBox.IsChecked = false;
}
private void RowCheckBoxChecked(object sender, RoutedEventArgs e)
{
if (AreAllCheckBoxesChecked())
SelectAllCheckBox.IsChecked = true;
else
SelectAllCheckBox.IsChecked = false;
BaseReleaseDataGridView.Items.Refresh();
}
private bool AreAllCheckBoxesChecked()
{
foreach (BaseDataItem objItem in BaseReleaseList)
{
if (!objItem.Select)
return false;
}
return true;
}
Now the problem is that whenever i click on the select all checkbox or the row check box, control gets stuck inside an infinite loop. Reason is that whenever i set, select to true or false, it again fires an event. How can this be handled.
Instead of tapping two events Checked and UnChecked, register to Click event. Click event will be raised on when user clicks on the checkbox unlike Checked/UnChecked which get called even when you check/uncheck your checkbox programatically.
<CheckBox Name="SelectAllCheckBox" Click="SelectAll_OnClick"></CheckBox>
in handler
private void SelectAll_OnClick(object sender, RoutedEventArgs e)
{
bool? isChecked = SelectAllCheckBox.IsChecked;
if (isChecked.HasValue)
{
foreach (BaseDataItem objItem in BaseReleaseList)
{
objItem.Select = isChecked;
}
BaseReleaseDataGridView.Items.Refresh();
}
}

ButtonClick to get an Object on SelectionChanged event

I have a SelectionChanged event and works perfectly, but I want to figure out how to "catch" this selected item at the click of a button they need to pass it as parameter to another page and edit this Item. Here's the current code and button SelectionChanged I still implemented because this is what I need.
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
//pega o Carro que foi selecionado
Carro sCar = (Carro)listBox.SelectedItem;
btnEditCar.IsEnabled = true;
btnDeleteCar.IsEnabled = true;
}
else
{
btnEditCar.IsEnabled = false;
btnDeleteCar.IsEnabled = false;
}
}
I need to edit the selectedItem on this button:
private void btnEditCar_Click(object sender, EventArgs e)
{
//Here I need access to the selectedItem on SelectionChanged event.
}
If you could also tell me how to pass the object as parameter would be perfect.
You can do this with binding also
1.Bind ListBoxItem(Carro Object) to the tag of "btnEditCar" in xaml.
Xaml should be like this
<Button Name="btnEditCar" OnClick="btnEditCar_Click" Tag="{Binding}"/>
and now in
private void btnEditCar_Click(object sender, EventArgs e)
{
Carro sCar=(Carro)((sender as FrameworkElement).Tag)
}
This is the good practice,creating a class variable only for temporary purpose is hack
To give a better idea on my comments. Creating a class level variable is like this:
Notice that sCar is declared outside the method, but within the class.
Carro sCar = new Carro();
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
sCar = (Carro)listBox.SelectedItem;
...
private void btnEditCar_Click(object sender, EventArgs e)
{
sCar.ProperyYouWantToChange = "Stuff I want to change"
}

Devexpress DataGrid contextmenu disappear

I have a strange problem with DataGrid context menu. I load item details from server after click on a item in datagrid. While loading the details the application shows a waitscreen:
private void gridViewOrders_MouseDown (object sender, MouseEventArgs e)
{
GridView gv = sender as GridView;
if (gv != null)
{
ShowWaitScreen (message);
GridHitInfo ghi = gv.CalcHitInfo (e.Location);
...
CloseWaitScreen ( );
}
}
When the user click the right mouse button, it should shows a context menu:
private void gridViewOrders_PopupMenuShowing (object sender, PopupMenuShowingEventArgs e)
{
if (e.MenuType == GridMenuType.Row)
{
DXMenuItem item = new DXMenuItem ("Delete", OnBtnDeleteOrder_Click);
e.Menu.Items.Add (item);
}
}
But the menu disappear at once. When I remove the waitscreen, the context menu is shown and the user can select the "Delete" menuitem. Any hints, how I can fix this problem? Thank you!
A good solution for my problem is to do the following:
private void gridViewOrders_MouseDown (object sender, MouseEventArgs e)
{
GridView gv = sender as GridView;
if (gv != null)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
ShowWaitScreen (message);
...
CloseWaitScreen ( )
}
}
}
Thanks to the DevExpress team for the hint!
I dont really thisnk you need to handle anything in MouseDown event.
This code works for GridView:
private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
GridView view = sender as GridView;
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row)
{
int rowHandle = e.HitInfo.RowHandle;
e.Menu.Items.Clear();
DXMenuItem zaznaczItem = new DXMenuItem("Zaznacz wszystkie", new EventHandler(zaznacz_Click));
DXMenuItem odznaczItem = new DXMenuItem("Odznacz wszystkie", new EventHandler(odznacz_Click));
e.Menu.Items.Add(zaznaczItem);
e.Menu.Items.Add(odznaczItem);
}
}
void zaznacz_Click(object sender, EventArgs e)
{
foreach (DataRow dr in (gcKontrahent.DataSource as DataTable).Rows)
{
dr["checkbox"] = true;
}
}
Handler zaznacz_Click is just example of handler for selected menu item. odznacz_Click is similar so I didnt post it. I dont have example for DataGrid so excuise me if it's not good solution. Just take it as example for acomplishing context menu handling in GridView.

Categories

Resources