Hello I need a help about datagrid in wpf. Actually I have C# experiences but new in WPF. I can answer my question in c# but don't know how to do this on WPF. I am using this code to solve datagridview cell autocomplete problem in c# and it works very good.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(dataGridView1.CurrentCell.ColumnIndex == 7)
{
AutoCompleteStringCollection acBusIDSorce = new AutoCompleteStringCollection();
acBusIDSorce.Add("Autocomplete Value 1");
acBusIDSorce.Add("Autocomplete Value 2");
acBusIDSorce.Add("Autocomplete Value 3");
TextBox txtBusID = e.Control as TextBox;
if (txtBusID != null)
{
txtBusID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtBusID.AutoCompleteCustomSource = acBusIDSorce;
txtBusID.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
}
But I can't see EditingControlShowing Event in Wpf. How can i make this solution in WPF ? I searched many platforms but couldn't find anything
edit: I am populating my dataGrid with codebehind with this code
dataGrid1.ItemsSource=dt.DefaultView;
Related
I have a TextBox (SearchBox - TabIndex=0) and a populated DataGrid TabIndex=1.
I want after input of the keyword in the SearchBox by pressing Enter to select the first row in the DataGrid.
So far i have just this:
private void txtSearchBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
System.Windows.Controls.TextBox txtb = sender as System.Windows.Controls.TextBox;
txtb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
It selects the whole DataGrid and not the row.
Thank you for your help.
EDIT:
I´ve added this to code above and the problem was solved :-)
dataGrid.SelectedItem = dataGrid.Items[0];
I´ve added the following code to the Searchbox_KeyDown
dataGrid.SelectedItem = dataGrid.Items[0];
And this solved my problem!
i have an combobox cell and i wanna convert to textbox cell after select item from the same combobox cell and convert to textbox but the problem when using
[1, DataGridView1.CurrentRow.Index] = new DataGridViewTextBoxCell()`
the cell not change until click on another cell although i'm using Datagridview1.refresh()
the first picture (when select from combobox then it should be changed )
the second picture ( after click an another cell )
DataGridViewComboBoxCells are notoriously obtuse when it comes to such a simple thing as simply and fully accepting a selected value..
This seems to work for me:
ComboBox editComboBox = null;
private void DataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox && e.ColumnIndex == 1)
{
editComboBox = (ComboBox)e.Control;
editComboBox.SelectionChangeCommitted -= editComboBox_SelectionChangeCommitted;
editComboBox.SelectionChangeCommitted += editComboBox_SelectionChangeCommitted;
}
}
void editComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
DataGridView1[1, dataGridView5.CurrentRow.Index] = new DataGridViewTextBoxCell();
DataGridView1[1, dataGridView5.CurrentRow.Index].Value =
editComboBox.SelectedItem.ToString();
DataGridView1.EndEdit();
DataGridView1[1, dataGridView5.CurrentRow.Index]cell1.Selected = false;
editComboBox = null;
}
Note that calling EndEdit allegedly can raise an exception if no error handler is found. I don't have one and I still don't get an error. Not sure what the docs mean; maybe that it may raise an error if the data are in fact invalid..
Also note that with this design the user can not easily correct an edit, unless you provide a way to restore the dropdown..
I'm working on my dataGridView and I'm trying to make one of my cell to be auto Complete, but its not working.
C# code:
private void dataGridRequest_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox prodCode = e.Control as TextBox;
if (prodCode != null)
{
prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
prodCode.AutoCompleteCustomSource = itemList;
prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
Check 2 things:
Check if itemList is empty
Disable the multiline option in the textbox (here MSDN mentions it doesn't work on multiline TextBox Controls)
i am using custom auto source to the text box. But the problem is, when i am entering key , if the suggestion list is high then the textbox flickers before showing suggestion.
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (txtSearch.Text != "")
{
string templateSearchTxt = txtSearch.Text;
foreach (String template in templateName) // templateName contains list of string
{
if (template.ToUpper().StartsWith(templateSearchTxt.ToUpper()))
{
suggestion.Add(template);
}
}
}
}
I have declared following code on form load event
suggestion = new AutoCompleteStringCollection();
txtSearch.AutoCompleteCustomSource = suggestion;
txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
I will seriously encourage you to use a Combobox with its AutoCompleteMode set to Suggest and attach the autocomplete list to it (as its AutoCompleteSource). It'll performt way better than your textchanged event listener.
I am very new to C#, and I really am stuck. I will try to be concise.
I have some ComboBox (in the same column) in DataGridView, and I want users to be able to type in the ComboBox. I got it to semi-work...
*Problem 1:
When I type in it, even though what I typed is added to the list of selections in the drop down, the ComboBox goes blank and I have to re-select the value I just entered. How can I make it so the value I type stays as the selection and not go blank.
*Problem 2:
Is there a way I can make certain ComboBox not editable in the same column? My code seems to make all of my ComboBox user editable. How can I make some ComboBox exceptions?
Thanks in advance, so much, if you could help!
Here are the codes:
private void dm_dgview_add_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
DataGridViewComboBoxEditingControl combo = e.Control as DataGridViewComboBoxEditingControl;
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.TextChanged += new EventHandler(combo_TextChanged);
}
}
void combo_TextChanged(object sender, EventArgs e)
{
dm_dgview_add.NotifyCurrentCellDirty(true);
}
private void dm_dgview_add_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewComboBoxCell cell = dm_dgview_add.CurrentCell as DataGridViewComboBoxCell;
if (cell != null && e.ColumnIndex == dm_dgview_add.Columns[1].Index)
{
if (!cell.Items.Contains(e.FormattedValue) && e.FormattedValue != null)
{
cell.Items.Add(e.FormattedValue);
}
}
}
Please help, I greatly appreciate this!
Do you need to set cell.Items.SelectedItem = e.FormattedValue in the cell validating event?