Select multiple Rows with UltraGrid - c#

I am new to Infragistics using UltraGrid.
I am trying to select multiple rows using checkbox column (or if there is another idea)
I have a code for DataGridView to select multiple rows and insert it to list, than try to delete or to do code with selected items.
//get the selected item
List<DataGridViewRow> selectedRows = (from row in Detail_shanuDGV.Rows.Cast<DataGridViewRow>()
where Convert.ToBoolean(row.Cells["checkBoxColumn1"].Value) == true
select row).ToList();
But when I try to use this code with UltraGrid like this
List<UltraGrid> selectedRows = (from row in ultraGrid1.Rows.Cast<UltraGrid>()
where Convert.ToBoolean(row.Cells["caption"].Value) == true
select row).ToList();
It gives me this error
'UltraGrid' does not contain a definition for 'Cells' and no extension method 'Cells' accepting a first argument of type 'UltraGrid' could be found
So if theres another idea or how to find solution to solve this error.
By the way I am using hierarchical UltraGrid with checkbox column, in my UltraGrid I have Master/Details data

You should cast to UltraGridRow not to UltraGrid
List<UltraGridRow> selectedRows = (from row in ultraGrid1.Rows.Cast<UltraGridRow>
where Convert.ToBoolean(row.Cells["caption"].Value) == true
select row).ToList();
Also, probably you need another level of filtering on those rows. For example, it is not clear if the checkbox for the column Caption is on the master or in the details pane of the grid. Also if you have a GroupBy display option then you need to add also another condition to filter only the required rows
For example, suppose that you want to apply this logic but only the rows that are in the details pane. In Infragistcs terms this second pane is called Band and every row has a property for the Band to which it belongs. And the Band has a property Index, so you get
List<UltraGridRow> selectedRows = (from row in ultraGrid1.Rows.Cast<UltraGridRow>
where row.Band.Index == 1 &&
Convert.ToBoolean(row.Cells["caption"].Value) == true
select row).ToList();
Notice that you first check for the Band index and only if this row is on second band you check for the cell value (Because If there is no "caption" column in the first band you get an NRE)

Related

How do I select the content in a specified cell from all rows in a Datagrid?

I have a text box search bar in my WPF application that displays records in a datagrid if they match the textbox content. The DataGrid has 2 cells- The first is a string and the second is an Int. I would like to retrieve the int values from the second cell in every row by clicking a separate search button. I unfortunately cannot seem to figure out how to accomplish this.
string ID = (DataGrid.SelectedCells[1].Column.GetCellContent(0) as TextBlock).Text;
About Retrieve The Specific Cell
The way to get the cell by row and colunm info:
https://stackoverflow.com/a/11615729/12949439
You can use the GetCell(int row, int colunm) method which created by #LPL in loops to get all cells in the DataGrid.
After you get all the cells, you can find the specific cell in them, and select it.
About Select The Specific Cell
The DataGrid has a SelectionUnit property, and you can set it into DataGridSelectionUnit.SingleCell then you can just select cell instead of select whole row.
And you can also use the SelectedCells property to get or set the selected cell(s).
And if you want the user can only select one cell, you can set SelectionMode property into DataGridSelectionMode.Single, so the user can not select multi-cells.
And for more infomation, you read the MSDN document about DataGrid, about DataGrid.SelectionUnit, about DataGrid.SelectedCells, and about DataGrid.SelectionMode.

DateGridview with checkbox column

In my datagridview there is checkbox column. Based on the values in the database want to check the checkbox in the datagridview during runtime and display it to the user.
for (int i = 0; i < supName.Count; i++)
{
foreach (DataGridViewRow row in dataGridView2.Rows)
{
int supId = Convert.ToInt32(row.Cells["supplierId"].Value.ToString());
if (supId == supName[i])
{
row.Cells["selectSupplier"].Value = true;
}
}
}
For one Item there can be multiple suppliers. When adding a new Item to the database, all the existing suppliers are displayed in a datagridview. In this datagridview there is a checkbox column which allows user to select relevant suppliers.
When retrieving information about an item I want to check the checkboxes of the suppliers user has selected for that particular item in the, above mentioned datagridview (datagridview with all the existing suppliers).
Above is the code that I have used to check the checkbox but it the checkbox is not selected.
checkbox column name is "selectSupplier".
Thank You
Your nested loop checks a row if supName list contains the row's supplierId. First issue I found was that if you don't have "AllowUserToAddRows" set to false, your inner loop will try to parse a blank row to integer and throw an exception. This stresses the importance of try-catch blocks and proper error handling.
Your method is also quite inefficient, order[n*m]. Ultimately, most of us will recommend using data binding (https://msdn.microsoft.com/en-us/library/fbk67b6z%28v=vs.90%29.aspx). You can "link" your database table directly to your DataGridView and the supplier ID and check come for free. You will likely need a simple LINQ command (https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b) to perform the supplier "exists" verification.
If you don't want to or cannot use data binding, you can perform the same result with just your inner loop and use supName.Contains(supId) for order[m].

Method to find DataGrid Row is selected or not?

I am new to C# and WPF. Issue I have is on datagrid I am displaying datatable data. I need to update data based on selected row. I am able to achieve that.
However, when I do not select any row on datagrid it does select default row as '0' and I do not want that I want result as -1 or error because I have not selected any row there?
You can determine if a row is selected with dataGrid.SelectedIndex; if the value is >= 0, you have a row selected.
To access the selected row:
if (dataGrid.SelectedIndex != -1) {
YourDataType row = (YourDataType)dataGrid.SelectedItem;
// process stuff
}
In the event that you allow multiple selections in your data grid, a very reasonable assumption, you can access the collection with the dataGrid.SelectedItems property.
A similar answer that shows example XAML too can be seen here: Get selected row item in DataGrid WPF
Not very clear about what actually you wanted to do. Just set yourdatagrid.SelectedIndex=-1; in some sort of initialization part of your code.

select multiple rows on Janus GridEX

I want to select multiple rows on Janus GridEX, but selectedItems property cannot be assigned and it's read only.
however I can select a specific row by GridEX.Row and set the current selected row, but I cant add a row or any thing like that to select multiple row!
Can anyone help me
Thanks alot, Shahin
First make sure that your grid allows selecting multiple rows. This can either be set in the designer or in code.
gridEX1.SelectionMode = SelectionMode.MultipleSelection;
Now to select rows, use SelectedItems.Add(position) where position is the index of the row in the grid.
GridEXRow row1 = ...;
GridEXRow row2 = ...;
gridEX1.SelectedItems.Add(row1.Position);
gridEX1.SelectedItems.Add(row2.Position);

LINQ Select certain cell in DataGridView depending on other cell in row

I am brand spanking new to LINQ and am trying to use it in my current hobby project. I have a datagridview where the first cell of each row is a datagridviewcheckbox, and the 4th cell is a string.
If the checkbox is checked, I need to add the 4th cell's value to a list.
At first I tried:
var selectedID = from c in multiContactLookup.SelectedCells.Cast<DataGridViewCell>()
select multiContactLookup.Rows[c.RowIndex].Cells[4].Value;
That didn't work because the checked cells are programatically unselected so c is never a value.
Then I tried:
var sel2 = from r in multiContactLookup.Rows.Cast<DataGridViewRow>()
where r.Cells[0].Value is true select r.Cells[4].Value;
but somehow my syntax is wrong.
Using LINQ, how can I select the rows where the first cell is checked then select the value of the first cell? Do I have to split this into two collections?
Thank you!
I think this should work:
IEnumerable<string> values = multiContactLookup.Rows.Cast<DataGridViewRow>()
.Where(row => (bool)row.Cells[0].Value)
.Select(row => (string)row.Cells[3].Value);
Maybe not the answer you have been looking for, but...
DataGridView (as most win-forms controls) is not the best source to start with LINQ. Most collections do not implement the correct IEnumerable<T> interface. That's the reason why you need the Cast() workaround.
In general, try to move your applications behavior away from controls and into the data. Maybe your grid is connected to a DataTable. In this case register the DataTable's events for changed data and work with the values of the DataRow, instead of the DataGridViewRow and its cells.

Categories