Multi Line Grid

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.

	private void gridGSInvoices_Click(object sender, System.EventArgs args)
	{
		UltraGridRow activeRow = gridGSInvoices.ActiveRow;
		neInvoiceNumberfromMemos.Value = activeRow.Cells["InvcHead_InvoiceNum"].Value.ToString();
		foreach (UltraGridRow row in this.grdList.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
		{

		if (Int32.Parse(row.GetCellValue("InvoiceNum").ToString()) == Int32.Parse(neInvoiceNumberfromMemos.Value.ToString()))
			{
				MessageBox.Show("Click Row");
				row.Selected = true;
				row.Activated = true;		
			}
			else
			{			
			//MessageBox.Show("Invoice Number doesn't match");
			}
		}

	}

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.

Shannon,

Try the AfterSelectChange Event for the grid. Here is an example from Infragistics.

private void ultraGrid1_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs e)

{

if(this.ultraGrid1.Selected.Rows.Count > 0)

{

this.ultraTextEditor1.Text = “Band, Row \n”;

foreach(UltraGridRow rowSelected in this.ultraGrid1.Selected.Rows)

{

this.ultraTextEditor1.Text += rowSelected.Band.Index.ToString() +

" , " + rowSelected.Index.ToString() + “\n”;

}

}

}

Scott

I have this so far but I’m getting an error. I need to tell it what the [selected] is and I don’t know how to do that.

	private void gridGSInvoices_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs args)
	{
		if(this.gridGSInvoices.Selected.Rows.Count > 0)
			{
				foreach (UltraGridRow row in this.grdList.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
				{
					if (Int32.Parse(row.GetCellValue("InvoiceNum").ToString()) == Int32.Parse(selected.GetCellValue("InvcHead_InvoiceNum").ToString()))
						{
							//MessageBox.Show("Row Selected = Invoice Num in grid");
							row.Selected = true;
							row.Activated = true;	
						}
					else
						{			
							//MessageBox.Show("Invoice Number doesn't match");
							//row.Selected = false;
							//row.Activated = false;
						}
				}
			}
	}

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");
		}
	}
	
}

}’’’

Scott

I’ve got it working with this. Now I just have to figure out the opposite which is when it is deselected, deselect the row.

private void gridGSInvoices_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs args)
	{
		//foreach(var selected in gridGSInvoices.Selected.Rows)
		if(this.gridGSInvoices.Selected.Rows.Count > 0)
			{
				foreach (UltraGridRow row in this.grdList.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
				{
					foreach(var selected in gridGSInvoices.Selected.Rows)
					if (Int32.Parse(row.GetCellValue("InvoiceNum").ToString()) == Int32.Parse(selected.GetCellValue("InvcHead_InvoiceNum").ToString()))
						{
							//MessageBox.Show("Row Selected = Invoice Num in grid");
							row.Selected = true;
							row.Activated = true;	
						}
					else
						{			

						}
				}
			}
	}

Any ideas on how I would change this to if the row is deselected it deselects the corresponding row in the other grid?

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

Scott

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.

That code is here:

	private void gridGSInvoices_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs args)
	{
		//foreach(var selected in gridGSInvoices.Selected.Rows)
		if(this.gridGSInvoices.Selected.Rows.Count > 0)
			{
				foreach (UltraGridRow row in this.grdList.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
				{
					foreach(var selected in gridGSInvoices.Selected.Rows)
					if (Int32.Parse(row.GetCellValue("InvoiceNum").ToString()) == Int32.Parse(selected.GetCellValue("InvcHead_InvoiceNum").ToString()))
						{
							row.Selected = true;
						}
					else
						{			
							row.Selected = false;
						}
				}
			}
	}

Picture of example:

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.

Okay, that makes sense, now any suggestions for how I might go about doing that?

Okay, that makes sense, now any suggestions for how I might go about doing that?

https://www.infragistics.com/community/forums/f/ultimate-ui-for-windows-forms/95316/deselect-rows-when-ultra-grid-loads

I looked thru that but I don’t understand how to say if a row gets unselected on the right to then perform an action. I don’t want to clear all.

just clear everything before start the loop, then re-select everything in the loop again.

2 Likes

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()));

}

}

}

Hope that makes sense.

Scott

Ive got these errors. Any ideas?

You can either

  • Make the List a List

or

  • Change

invoices.Add(int.Parse(listRow.GetCellValue(“InvoiceNum”).ToString());

to

invoices.Add(listRow.GetCellValue(“InvoiceNum”).ToString());

and

Change gsRow.Selected = invoices.Contains(int.Parse(listRow.GetCellValue(“InvoiceNum”).ToString()));

To

gsRow.Selected = invoices.Contains(listRow.GetCellValue(“InvoiceNum”).ToString());

the second one doesn’t require the value to be converted to a integer, does the comparison as a string.

Scott

Make List<string> to List<int>

For the last error you need to change the name of the row instance

gsRow.Selected = invoices.Contains(int.Parse( gsRow .GetCellValue(“InvoiceNum”).ToString()));