How to maintain selected row of XtraGrid control - c#

I'm developing an application of the xtragridview control in that application when i'll double click on row from the xtragridview that time one popup form opens . then the focus of the parent window changes & focus is assigned to the another form which is popup . and that time my selected row changes it's state & it focus/select the default 1st row from the xtrgridview.
but i want to maintain the focused/selected row as it is if user changes the focus from one form to another pop up form.
Is there any solution on this solution? what properties of xtragridview control should i've to set for this problem?
thanxs.....

Generally, the approach you are using does not require you to write an additional code. The XtraGrid does not reset its FocusedRow if you open a form by doubleclicking a grid row. So, I would suggest that you determine the cause of this behavior. This can be done by using the following approach:
1) handle the GridView's FocusedRowChanged event and set a breakpoint in it.
2) reproduce the issue and check which code forces the gridView to focus the first row.
This should give an idea on why this happens.
Also, I would suggest that you review the How to create the PopupForm for editing rows in the GridView and automatically create editors based on the column editors. example where the required functionality is implemented.
I think I know the cause of this problem. It appears because you are changing the DataView's RowFilter property. I think you want your editors to point to the clicked record. The best solution is to do not filter the DataView but to assign the BindingContext as it is done in the example above. Here is the code from it:
public EditForm(Point location, GridColumnCollection columns, object dataSource, BindingContext context)
: this() {
StartPosition = FormStartPosition.Manual;
Location = location;
BindingContext = context; // <<<<<<
allowTrackValueChanges = false;
this.dataSource = dataSource;
...
}

Mehod 1:
In the double click event handler just mention
return;
after all processes (Opening of another form etc.) are done.
After understanding your question better, I suggest to try method 2 I hope it surely works.
Method 2:
First record the current selected index before it opens another form or dialog ..
int index = datagridview.SelectedRows[0].Index; //or xdatagrid.SelectedRows[0].Index;**
Then after completion of form opening or other procedure add the following line
datagridview.Rows[index].Selected = true; //or xdatagrid.Rows[index].Selected = true;**
**N.B.: I have never used xdatagrid, but suggesting the solutions depending on my datagridview experience.

I use
GridView view = (GridView) sender;
Point pt = view.GridControl.PointToClient(Control.MousePosition);
var info = DoRowDoubleClick(view, pt);
when DoRowDoubleClick is:
private static GridHitInfo DoRowDoubleClick(GridView view, Point pt) {
GridHitInfo info = view.CalcHitInfo(pt);
if (info.InRow || info.InRowCell){
string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();
MessageBox.Show(string.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption));
return info;
}
return null;
}

Related

Creating a new record, then focusing on the new record

So far I have the following code on a button click event:
string AccRef = Microsoft.VisualBasic.Interaction.InputBox("New Customer", "Customer Reference:", "", 0, 0);
if (AccRef != "")
{
DataRow dr = dSSystem.Customers.NewRow();
dr["AccRef"] = AccRef;
dSSystem.Customers.Rows.Add(dr);
frmCustomer frmCust = new frmCustomer();
frmCust.ShowDialog();
this.customersTableAdapter.Fill(this.dSSystem.Customers);
}
This successfully creates a new row as I can see behind the dialogue, however, I have an issue - the dialog that displays is another form that displays all the customer information, this is handled with text boxes having a data binding to the appropriate fields within the database, so naturally they display the information on the selected record within the DB, at the moment however it's showing the information for the previously selected record and not moving to the one created using the code above.
How do I make the system create a new row and then focus on the new row before opening the new form for editing?
Thank you in advance.
Essentially you can pass information to the dialog-form, which that form can use to navigate to the correct record.
The simplest approach is to create a constructor for the second form that accepts an argument.
public frmCustomer(int newID)
{
InitializeComponent();
// use newID (if present) to navigate
// to the correct row
}
frmCustomer frmCust = new frmCustomer(theNewID);
frmCust.ShowDialog();
I don't know the details of your binding, so cannot advise how you would navigate to the record.
There are a number of other approaches, outlined here.
A simplistic approach could be to open the dialog-form at the next new record and navigate back a row but, again, I don't know your set-up enough to offer code for this. If you have a BindingSource you can use its methods MoveLast() or MovePrevious().
If you use the first approach and the 2nd form opens with the passed id, then you can use the Find method of the BindingSource, and then the Position property to move to the found record. See the example here at MSDN.

Pass value form one window form to another Windows form

I have a DataGridView on my first windows Form. where a user click then it open a popup where is second DataGridView, click on row of DataGridView that will display on first page DataGridView.
The problem is that when i click on Form one DataGridView it open popup of second Form, where user select any row. I take my required data from that DataGridView and send these data on First page that is previously open on my window. From this line of code i pass the value from Second Form to First Form .
FrmSetting _frmSetting = new FrmSetting(string _val,string code)
I think because of new keyword it open a new page or something like that . How can i send on First Form please suggest me .
Thanks
Create property on Second form and set the selected value to that property. Then you can get it from first form.
UPDATE:
//this should be in second form
string _myProperty;
public string MyProperty
{
get{return _myProperty;}
set{_myProperty=value; if(MyPropertyChanged!=null)MyPropertyChanged(this,null);}
}
public event EventHandler MyPropertyChanged;
//assign value to MyProperty when your dataGrid changes or whatever
Then from your first form you can say:
string valueFromSecondForm = secondForm.MyProperty;
//subscribe to MyPropertyChanged event if you need to know when it is changed
if you need some info from first form you can do the same thing.
Pass the first form as a parameter to the constructor for the second form. Make the DataGrid that you want to update in the first form public and update it from the second form:
Form1.DataGrid1.CaptionText = "Caption 1";

Get the SourceControl of my ContextMenuStrip when I use the shortcut key

I have single ContextMenuStrip attached to two controls (DataGridView).
In the ToolStripMenuItem click event, I manage to get the original caller (the DataGridView) with this code :
var menu = (ToolStripDropDownItem)sender;
var strip = (ContextMenuStrip)menu.Owner;
var dgv = (DataGridView)strip.SourceControl;
It works pretty good when I click on my ToolStripMenuItem.
But when I use the sortcut key linked to the ToolStripMenuItem, the strip.SourceControl return null.
Does anyone know why ?
The SourceControl property shows the control that caused the ContextMenuStrip to open. Since in this case the ContextMenuStrip doesn't display there is no control used to open it, and thus the property is null.
This property is better used in the context of the opening event. See ContextMenuStrip.SourceControl.
Update: One method to figure out which DataGridView was the intended receiver of the ToolStripMenuItem click would be to see which one has the focus:
var dgv = this.ActiveControl as DataGridView;
if ( dgv != null) // make sure to check for null before trying to use this var
//...

Infragistics UltraGrid : Unable to enter edit mode for focused column

Info:
I am working with a C#.NET 3.5 winforms application and using Infragistics ultrgrid 8.2 in it. The grid has 5 columns, of which first three are readonly and 4th and 5th are valuelisted dropdowns. The 4th column is called "From".
What do I want to achieve:
When form launches, I want to set focus to first row cell for "From" column and also have it in edit mode.
What have I coded :
I have written following function
private void SetFocus()
{
_grid.Focus();
_grid.ActiveCell = _grid.Rows[0].Cells["From"];
_grid.PerformAction(UltraGridAction.EnterEditMode);
_grid.DisplayLayout.Bands[0].Columns["From"].Editor.Focus();
}
Which when called through form's paint event, works fine. But it is irritating to see the control get focus on each paint. Calling this on load, gets the focus right; but does not set the control in edit mode.
Thanks already
Jyotsna
I am not sure but I did a little Googling and maybe,
_grid.Rows[0].Cells["From"].Activate();
instead of calling
_grid.ActiveCell = _grid.Rows[0].Cells["From"];
Can't be sure if it will make a difference but give it a try.
Solved here: link
var cell = ... // Get the cell
if (cell != null)
{
BeginInvoke(new MethodInvoker(() =>
{
cell.Activate();
bugGrid.PerformAction(UltraGridAction.EnterEditMode);
}));
}

Set selected WPF DataGridCell to focused and editable upon event

Woot, first Stack Overflow post! I've been asked to work on a desktop application to improve an inventory process for my company. I dabbled with WPF in school and I figured I'd start there. After researching some, I learned about MVVM, put a design together, and forged ahead. Finally, I'm stuck and looking for some help and also a sanity check to see if I'm on the right path.
I have single-column DataGrid bound to an observable collection. Users of the application use a scan gun to enter values in. One potential value that I catch in my "Cell" model object is a "MoveNextColumn" value. This raises a custom event in my model that is handled in the View Model. The handler is supposed to simulate blank entries for all remaining rows in that column, set focus on the last row, and wait for input before moving on. So here is what I have so far:
private void dummyCell_MoveToNextColumn(object sender, RoutedEventArgs e) {
e.Handled = true;
// Cell is the model object containing the parsing rules and raising events
var lSender = sender as Cell;
var gridItems = ViewGridReference.Items;
var lastItem = gridItems[gridItems.Count - 1];
if (lSender == lastItem) {
// We are at the bottom of the column
// Move the program on to the next column
CurrentColumn++;
OnPropertyChanged("ItemPositions");
} else {
// Simulate "empty position" input for this cell and all cells down the column
// Cells are validating themselves as the simulation progresses
foreach (Cell item in ViewGridReference.Items) {
item.ActualItemCode = string.Empty;
}
// ViewGridReference is a reference to my DataGrid set from the view
ViewGridReference.Focus();
ViewGridReference.SelectedIndex = gridItems.Count - 1;
ViewGridReference.CurrentCell = new DataGridCellInfo(lastItem, ViewGridReference.Columns[0]);
((DataGridCell)ViewGridReference.SelectedItem).Focus();
}
}
All of this seems to be working as expected: all rows receive blank input and are validated (I use color properties in the cell to which the view binds to signify the validity of the entry).
Unfortunately, though the focus is on the last row as desired, it is not editable and the user cannot submit another "MoveNextColumn" value which would move the program on. The goal here is to minimize any keyboard interaction. Everything should be done with scan guns and barcodes.
Any ideas on how to make the selected cell editable after this code executes?
Any "hey, your design sucks" feedback would be cool too. This is new to me and I'm open to constructive criticism.
I have made some progress with this. The entire grid was left at an uneditable state in the code above. This now leaves focus on the last cell in my column and allows me to submit input with the scan gun.
This seems to work, but I'd still appreciate some feedback on whether there is a better way.
private void dummyCell_MoveToNextColumn(object sender, RoutedEventArgs e) {
e.Handled = true;
// Cell is the model object containing the parsing rules and raising events
var lSender = sender as Cell;
var gridItems = ViewGridReference.Items;
var lastItem = gridItems[gridItems.Count - 1];
if (lSender == lastItem) {
// We are at the bottom of the column
// Move the program on to the next column
CurrentColumn++;
OnPropertyChanged("ItemPositions");
} else {
// Simulate "empty position" input for this cell and all cells down the column
// Cells are validating themselves as the simulation progresses
foreach (Cell item in ViewGridReference.Items) {
item.ActualItemCode = string.Empty;
}
ViewGridReference.SelectedIndex = gridItems.Count - 1;
ViewGridReference.CurrentCell = new DataGridCellInfo(lastItem, ViewGridReference.Columns[0]);
(ViewGridReference.ItemsSource as ListCollectionView).EditItem(ViewGridReference.SelectedItem);
((DataGridCell)ViewGridReference.SelectedItem).Focus();
}
}
Updated 12/2/2010
Hey, there is an important update to this. The first thing to note is that text entry is being done with a scan gun in my scenario, so 'Enter' keys are sent down with each pull of the trigger. It shoots down each character followed by the Enter key all at once.
WPF sees this enter and wants to set the focus to the DataGridCell directly beneath the cell in which the Enter key input was received. The code above sets the focus to the last cell, but then the Enter key event still fires and is handled by DataGrid after this code is run. The effect is that the focus is reset back to the subsequent cell, not the last cell like I want.
So I need to either figure out how to eat the Enter key for just that scan, or I need to break how WPF handles Enter keys. The last line up there actually throws an exception. We are trying to use a Model class (Class.cs) as a DataGridCell, and there is nothing to handle that cast. Because of that, the Focus() method tries to operate on a null object and we get a NullReferenceException. This was really confusing me because Visual Studio 2010 would sometimes break to alert me about this, but sometimes it wouldn't. However, if I run the executable outside of Visual Studio, it works just fine. That's because unhandled, non-fatal exceptions are ignored and the Enter key behavior fails to operate as normal.
So it works, but in a pretty gross way. I either need to figure out how to do one-time handling of the Enter key and override the default WPF handler, or just leave it like it is and grimace.

Categories