I have a customization in which I have two grids. I want to customize this so that if I select multiple rows on the right grid, it will make the same Invoices in the left grid active.
I have the following code that works if I click each row one at a time manually, however I want to be able to shift and click multiple rows at the same time. When I shift click, It will only make the last selected Invoice Number be selected on the grid on the left.
I think you would have to do a foreach loop on the whole grid and if row is selected (not active) then use that value to make the row in the other grid selected.
If I am understanding correctly â you want to activated and select rows on the list grid when selected on the GS grid. One thing to note, there is only one active row per grid, so the last row selected will be the active row the way you have this coded. Plus I am assuming the message boxes will be removed after testing. If you donât now I suggest learn how to use Visual Studio to debug. You can remove the message boxes and then step through the code to see what is happening at each step.
I did not test this but you can try it
âââ //check to see if any rows are selected
if(this.gridGSInvoices.Selected.Rows.Count > 0)
{
MessageBox.Show(âRow is Selectedâ);
//loop through selected rows on GS Invoices Grid
foreach(var gsRow in this.gridGSInvoices.Selected.Rows)
{
//get GS Invoice Num for comparision on List grid
var gsInvoiceNum = int.Parse(gsRow.GetCellValue("InvoiceNum").ToString());
//loop through all rows on List grid
foreach (var listRow in this.grdList.Rows) //no need for to enumerate down
{
if (gsInvoiceNum == int.Parse(listRow.GetCellValue("InvoiceNum").ToString()))
{
MessageBox.Show("Row Selected = Invoice Num in grid");
row.Selected = true;
row.Activated = true;
}
else
{
MessageBox.Show("Invoice Number doesn't match");
}
}
}
The AfterSelectChange event gets hit on both Select and Deselect of a row so it should be running thorough your code for both. Put in your else statement row.Selected
= false; to deselect the row in the other grid
I thought that too, however, when I changed the code to have row.Selected = false; in the else section it deselected every row but the last selected one. It isnât working properly. I need it to keep all selected rows on the left grid to be selected on the right and only deselect the ones on the left that I deselect on the right.
You are looping through and acting only if there are selected rows. If you need to find un-selected rows, youâll have to do a loop to un-select on un-selected rows. Or do a clear of all selected rows and re-select.
Looked at what you are trying to do closer. First I would change the AfterSelectChange event to the List grid so when a row is selected or de-selected on the
list grid it is at that point the event is triggered.
What you need to do is first create a list of invoices from the list grid and then use that list to as it is looping through the GS Invoices grid to see if the
row in the GS Invoices grid matches any invoice in the invoice list created from the list grid.
Try this
Right Grid => List Grid
Left Grid => GS Invoices
private void grdList_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs args)
{
//check to see if any rows are selected
if(this.grdList.Selected.Rows.Count > 0)
{
List<string> invoices = new List<string>();
//loop through list grid and create an list of invoice numbers in selected rows
foreach(var listRow in this.grdList.Selected.Rows)
{
invoices.Add(int.Parse(listRow.GetCellValue("InvoiceNum").ToString());
}
//loop through all rows on GS Invoices grid and set Selected
foreach (var gsRow in this.gridGSInvoices.Rows)
{
//set row selected value if invoice number is in the invoices List
gsRow.Selected = invoices.Contains(int.Parse(listRow.GetCellValue("InvoiceNum").ToString()));
}
}
}