I am working with a DataGridView and have a column of type DataGridViewComboBox and I have stumbled across a problem. Basically, depending on the value of the cell, i would like to set the .Items to a certain set of strings, but when the value is changed, i would like to change the .Items list. But with this, I will occasionally remove a string from the list that is currently being occupied by another DataGridViewCell, this changes all the values that were equal to the removed item, until i re-add it back to the list (after i have finished editing).
So basically, I am wondering if there is a way of hiding some of the items from the combo box Drop Down list, so that when a certain cell is selected, they can't choose an item that isn't allowed.
Thanks,
Lloyd
Instead of databinding the entire column, databind each row's DataGridViewComboBoxCell individually. That way, you don't have .items from one row affecting .items from another row.
Related
I have a Datagridview and 18 comboboxes.Each combobox represents a column of the Datagridview.
The point is: when I select a combobox item in the dropdownlist, I'd like the other comboboxes to select the corresponding item. I believe that to do that I have to make the comboboxes remember the row of the item.
At first I tried to make a specific class for it but I'm having some trouble.
Then thought that I could store the row information into the combobox by putting it into value property. But I'm still having troubles.
Keeping in mind that trouble won't ever completely leave me, someone has any tips?
Thank you all so much.
I would try this: use the SelectIndexChanged on the first combobox and get the line index, after that, change the others combobox index by setting them with the index you found. This is the dumbest approach, then you can improve it.
I'm looking to get the index of the selected item in the combobox that corresponds to a cell in the grid view.
The following is what I used:
var cb = (DataGridViewComboBoxCell)row.Cells[1];
fieldIndex = cb.Items.IndexOf((string)cb.Value);
However, this doesn't taken into account the fact that the selected value may match another value in the combo box. How can I get the exact index that is selected for the current row?
The combobox doesn't actually exist unless the cell is selected. It is overlayed when the cell is active. You can access it in the EditingControlShowing event. For this reason you can't get the index unless you loop through the data source and check the value, but as you mentioned, you don't know which one you want since values can appear multiple times.
Generally you don't care about the index of the selection, you just care about the value. Are you sure you need the index? If you are properly using databinding against your grid, the backing collection will have its fields in sync with the grid and you have all the data you should care about.
I have a weird problem with ComboBox Column in DataGridView in WinForm.
When I select the item from ComboBox, the selected value would not be rendered by the ComboBox. I need to click the ComboBox so that the selected value of the ComboBox would be rendered or displayed. If not, the ComboBox would just display empty.
The weird thing is this only happen in my first ComboBox Column in DataGridView.
In other words, this issue is not happening with my Second, Third or etc ComboBox Column in DataGridView.
Any ideas? Could it be a DataGridView bugs?
Thanks for your attention.
Thanks for your attention.
I manage to find the solution for the question that I have asked.
Overview:
I populate rows in DataGridView manually by adding row into DataGridView.
For DataGridViewComboBoxCell object, I use DataSource to populate the Items instead of adding the Items manually to the
ComboBoxCell.
The Problem:
Whenever you select the item in ComboBox, it is unable to render the selected value in the ComboBox. It would only show the value if you click it.
Solution:
It turns out in the code somewhere after I set the DataSource of the DataGridViewComboBoxCell, I called its method, DataGridViewComboBoxCell.Items.Count. This is the source of the weird behaviour.
Somehow, if you have set the ComboBoxCell DataSource, and you call Items.Count method, it would show that weird behaviour.
I should not use Items.Count since I am using DataSource. The Items.Count would always be zero because the DataBinding it's not happened instantly the moment you set the DataSource.
Hopefully this post would help someone else in the future. Thanks.
I'm using a WPF datagrid in which multiple records can be selected & it can be sorted (by clicking the column headers).
I want to be able to retain the selected rows after the sort.
Please note I can retain the selected row, the problem arises when the rows to retain are more than 1.
Say for example: I select rows with indices 2 & 3, and then sort on a particular column. After the sort functionality I want to be able to retain the selected rows (old indices-2,3) & new indices - 4,5. Thus, after sorting, the 4th & 5th rows must be selected.
The items source for my datagrid is of type ListCollectionView to which I'm adding sort descriptions when reqd to sort.
Things I have tried:
Tried setting the SelectedItems property. Doesn't work since it is a Read Only property.
Tried setting the IsSelected property of the DataGridRow(s), doesn't work. Though I can see the IsSelected property for the selected rows is set to true, on the screen I do not see the records highlighted.
This one may be vague but I set the selected Index multiple times but this only highlights the record for the last selected index.
Each time after sorting, the last selected record is highlighted. Is there a way by which I can retain the selected rows?
EDIT:
I'm now able to set the selected items of the datagrid using Datagrid.SelectedItems.Add(dataGridRow) or setting the IsSelected property on the datagrid row.
The issue is though the selected items are present, they are not highlighted on the screen. How can I get the rows to be highlighted?
For ex: If 3 records were selected & highlighted before sort, after sort, all records are selected (put breakpoints & checked in Code) but only one record is highlighted.
One may set the selected items of a datagrid by setting the IsSelected property of the row to true.
Or use the SelectedItems.Add() method to add selected items.
I was doing the same but still couldn't see the selected items since, there was another place in the code where the selected items were being modified.
The above solution is tested & works.
I am working on a master/detail gridview and detailsview in asp.net web forms using an objectdatasource. The details view is displaying extra information about the selected row from the gridview. When I sort the grid, I want the currently selected row, before sort occurs, to remain in the details view after the sort is completed. Instead, the gridview is auto selecting the new first row whenever I sort.
I found a partial solution to this problem. If I set WhateverGridview.SelectedIndex= -1 onsort and the value becomes null. This makes it deselect any rows after a sort. This leaves the details view blank. However, what I want to do is maintain the selected row not nullify it.
So, does anyone have a good way to retain the selected value or prevent the details view from displaying after sort event fires.
Here you need to use the GridView.EnablePersistedSelection property. Set this property to true.
Setting this property to true means that GridView will make sure that the selection of a row is based on data-key values.
By default GridView makes row selections based on index. This is the reason why when you sort, gridview is selecting row based on index and you lose your actual selected row.