I have a DataGrid and one of my columns is a DataGridTextColumn. When the user presses enter, the DataGrid takes them to the next row, in which the user can immediately start typing, and the DataGridTextColumn takes the input just fine, yay!
The problem is, after they fill out one row, and hit enter to goto the next to fill it out, THEY MUST TYPE to activate the column. It's very important that the user can just paste with CTRL+V right after they are brought to the new column, and that the pasted info will automatically activate the text column without them having to type.
How can I go about this?
Idea is similar to Dimitris Batsougiannis suggested, but check Ctrl instead.
<DataGrid KeyDown="DataGrid_KeyDown"...
private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
{
var dataGrid = (DataGrid)sender;
if (dataGrid.CurrentCell != null)
{
dataGrid.BeginEdit();
}
}
}
Related
I have search TextBox, and I want to when I press Arrow Down to navigate through the ListView, and if I press Left/Right Arrow on ListView I want to navigate with Caret through the text, and if I press down again I navigate through the ListView again. Just regular process like Chrome Search suggestions.
I just want to get Focus like when pressing TAB button. With TAB all works great.
On TextBox I use PreviewKeyDown event and detect Key.Down and I set my ListView.Focus() - That works great. ListView is focused and I can Navigate through items.
On ListView I use PreviewKeyDown event and detect any key that is not Key.Down or Key.Up and set focus on TextBox and set selection to the end of text. - That works great. Textbox is focused and I can Navigate through the text with Left/Right arrow.
Problem is now if I press Down arrow. Nothing happens. I can't get focus on ListView again. Don't know why.
I used Keyboard.Focus(), Keyboard.ClearFocus(), FocusManager... Nothing helps, focus remain on TextBox.
I used ItemContainerGenerator with SelectedIndex and that works but Selects second item in ListView. I think it is because I select first index and when I press down ListView gets focus and pass that Arrow Down event. And I ending with second row in ListView.
TextBox on PreviewKeyDown:
private void SearchTermTextBox_OnKeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Down) {
SuggestionsListView.Focus();
}
else if (e.Key == Key.Escape)
{
SearchTermTextBox.Clear();
}
}
ListView on PreviewKeyDown:
private void SuggestionsListView_OnPreviewKeyDown(object sender, KeyEventArgs e) {
if (e.Key != Key.Down && e.Key != Key.Up) {
SearchTermTextBox.Focus();
SearchTermTextBox.Select(SearchTermTextBox.Text.Length, 0);
}
}
I used this code with ItemContainerGenerator. But, like I said, it's selecting second row from some reason.
SuggestionsListView.SelectedItem = SuggestionsListView.Items[0];
ListViewItem item = SuggestionsListView.ItemContainerGenerator.ContainerFromItem(SuggestionsListView.SelectedItem) as ListViewItem;
item.Focus();
Pressing Tab key on Button Refresh is setting focus on the dropdown list but I need to set focus on Checkbox column and first row of grid when the grid datasource is not null else the next control, however it is selecting the given cell only. I have set tabIndex property in sequence, please tell me where i am wrong, here is my code:
private void btnRefresh_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
if (grid.DataSource != null)
{
grid.Focus();
grid.CurrentCell = this.grid[1, 0];
grid.CurrentCell.Selected = true;
grid.BeginEdit(false);
}
else
{
btnCancel.Focus();
}
}
}
Have you seen this post?
Seems like your use of the index is of Grid[x,y].
Try
grid.Rows[1].Cells[0]
However, this wil select only the cell (first cell, second row by the way).
If you want to select the entire row, try
grid.Rows.First().Selected = True
Hope it helps.
I have a datagrid. In this datagrid new rows are added programatically.
When a new row is added, the first cell of new row is focused.
Now, if user clicks on any other cell without entering data in the first cell, then focus should not move from first cell. I mean I want the user to enter data in first cell. If he don't want to enter data in first cell, then he/she should press Enter. If he wants to edit any other cell from any other row, then he should be able to do it. But when he again comes to the new row, then he should be forced to focus the first cell
Since you don't want focus to move to any other column in the new row, you can disable all the elements of that row.
Now whenever user press an Enter or he/she input some text to the first column of the new row, enable all the elements of that row.
For example, consider the two TextBox. Initially second textbox will be disabled. When user press enter or any data in first TextBox, second TextBox will get enabled.
XAML code:
<TextBox Name="txtBox" Grid.Row="0" KeyDown="txtBox_KeyDown" TextChanged="txtBox_TextChanged"></TextBox>
<TextBox Name="txtBox2" Grid.Row="1" IsEnabled="False"></TextBox>
C# Code:
private void txtBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
txtBox2.IsEnabled = true;
txtBox2.Focus();
}
}
private void txtBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!txtBox.Text.Equals(""))
{
txtBox2.IsEnabled = true;
}
else
{
txtBox2.IsEnabled = false;
}
}
You can use the same concept to solve your issue.
I have a datagridview with five columns and context menu strip which have items and sub items. When I right click on last column I want to open context menu.
I tried this code, but it's open context menu strip without sub items.
dataGrid.Columns[dataGrid.Columns.Count].HeaderCell.ContextMenuStrip = contextMenuStrip1;
It looks like you want to open your ContextMenuStrip if your user right clicks the header of your DataGridView's last column. I would use the DataGridView MouseDown event and in that event check for these conditions and if they're met call the Show method of your ContextMenuStrip.
Like this:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
var ht = dataGridView1.HitTest(e.X, e.Y);
// See if the user right-clicked over the header of the last column.
if (( ht.ColumnIndex == dataGridView1.Columns.Count - 1)
&& (ht.Type == DataGridViewHitTestType.ColumnHeader)) {
// This positions the menu at the mouse's location.
contextMenuStrip1.Show(MousePosition);
}
}
}
If you mean you want to attach context menu to header of your last column, your direction is probably right. But index of last column is dataGrid.Columns.Count - 1. So, this code works fine for me:
dataGrid.Columns[dataGrid.Columns.Count - 1].HeaderCell.ContextMenuStrip = contextMenuStrip1; ?
Subitems are in place.
How can i avoid the double click on a DropDownButton used within a DataGridView? Right now I am able to view the drop down items within the DataGridView by clicking two or more times. First time it selects the cell and second time when I click on the DropDownButton arrow, it shows the list. How can I achieve the same in a single click?
Set EditMode property of the DataGridView to EditOnEnter: link
DataGridView.EditMode - Gets or sets a value indicating how to begin editing a cell.
EditOnEnter - Editing begins when the cell receives focus.
You can achieve this by subscribing for the EditingControlShowing event of the grid and there for control of type ComboBox
ComboBox ctl = e.Control as ComboBox;
ctl.Enter -= new EventHandler(ctl_Enter);
ctl.Enter += new EventHandler(ctl_Enter);
And in the Enter event, use the property
void ctl_Enter(object sender, EventArgs e)
{
(sender as ComboBox).DroppedDown = true;
}
DroppedDown indicates as the name suggests whether the dropdown area is shown or not, so whenever the control is entered this will set it to true and display the items without the need of further clicks.
The "set EditMode property of the DataGridView to EditOnEnter" worked for me, but I found another problem: user can't delete a row by just selecting and pressing DEL key. So, a google search gave me another way to do it. Just catch the event CellEnter and check if the cell is the appropriated type to perform appropriated action like this sample code:
private void Form_OnLoad(object sender, EventArgs e){
dgvArmazem.CellEnter += new DataGridViewCellEventHandler(dgvArmazem_CellEnter);
}
void dgvArmazem_CellEnter(object sender, DataGridViewCellEventArgs e)
{
DataGridView dg = (DataGridView)sender;
if (dg.CurrentCell.EditType == typeof(DataGridViewComboBoxEditingControl))
{
SendKeys.Send("{F4}");
}
}
Now the ComboBox drops down faster and the user still delete a row by selecting a row and pressing DEL key.
That's it.