SelectedItem in gridview combo box - winforms

I have populated combobox from database in my RadGridView and I want that when it is in add mode my combobox shows its first item as default value. I used following code but I have Error in debuging on commented line in code
private void radGridView_CellValueChanged(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
if (e.Column.Name == "Productcolumn")
{
//ERROR on debuging following line
RadDropDownListEditor ed = this.radGridView.Columns["UnitPrice"] as RadDropDownListEditor;
if (ed != null)
{
// the default selected Price that will be shown will be the first price, if no price is selected for the current cell
RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)ed.EditorElement;
editorElement.SelectedIndex = 0;
}
}
}

Related

Selecting same item in 2 different Grid-Views

I have 2 RadGridView.
GridView 1: short list has items from GridView2.
GridView 2: Long list has telerik:RadGridView.GroupDescriptors for group the items.
I want if I select one row in first GridView takes me to the same Row in the second GridView.
So, I put in Xaml
//XAML
SelectedItem="{Binding SelectedDP}" For GridView1
SelectedItem="{Binding SelectedDP1}" For GridView2
//CS
private DataPermission mSelectedDP = new DataPermission();
public DataPermission SelectedDP
{
get { return mSelectedDP; }
set
{
mSelectedDP = value;
foreach (SecurityDataPermissions m in DisplayedDataPermissionsList)
{
if (m.DataPermission.DataPointName == SelectedDP.DataPointName)
SelectedDP1 = m;
}
OnPropertyChanged("SelectedDP");
}
}
private SecurityDataPermissions mSelectedDP1 = new SecurityDataPermissions();
public SecurityDataPermissions SelectedDP1
{
get { return mSelectedDP1; }
set
{
this.mSelectedDP1 = value;
OnPropertyChanged("SelectedDP1");
}
}
The row in GridView2 is getting selected but it doesn't scroll to it. I mean if manually scrolled to the item I can see it gray (selected but not focused) but If not, I can't recognize which row is selected.
What I want is when I select row in GridView1 takes me to the same row in GridView2
You could handle the SelectionChanged event and call the ScrollIntoViewAsync method in the event handler as suggested in the official documentation.
private void gridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
{
gridView.ScrollIntoViewAsync(gridView.SelectedItem, radgridView.Columns[0]);
}
You may of course wrap this functionality in an attached behaviour if you want to: https://www.telerik.com/forums/auto-scroll-to-the-selected-item

Vaadin-Unable to update grid container on combobox value change

I am using vaadin 7.7.7
In a grid i have a combobox as an edited item in one of the columns
as
grid.addColumn("columnProperty").setEditorField(combobox);
I need to update a property/cell in same row based on the combobox selection change
My issue is , the selection change event triggers twice, once when the combobox in clicked and second when the selection value is changed. But the updated value in next cell gets reflected on UI only first time.
Below is the code written . Any solutions?
Combobox.addValueChangeListener(new ValueChangeListener()
#Override
public void valueChange(ValueChangeEvent event) {
// below line works only first time when the combobox is clicked,but i want
//it when the item in the combobox is changed
gridContainer.getContainerProperty(editedRow,"editedColumProperty").setValue("ValueTobeUpdated");}
});
Need to update the unit column on combobox change in edited mode(before saving)
Refer below link for image
example image
You will get value change events even when the field gets value that it should show to the user. In order to get event that indicates that the user has accepted the input you should use field group (setEditorFieldGroup).
From Book of Vaadin example for grid editing:
grid.getColumn("name").setEditorField(nameEditor);
FieldGroup fieldGroup = new FieldGroup();
grid.setEditorFieldGroup(fieldGroup);
fieldGroup.addCommitHandler(new CommitHandler() {
private static final long serialVersionUID = -8378742499490422335L;
#Override
public void preCommit(CommitEvent commitEvent)
throws CommitException {
}
#Override
public void postCommit(CommitEvent commitEvent)
throws CommitException {
Notification.show("Saved successfully");
}
});
Edit
I assume that you want to connect Parameter and Unit comboboxes. I would do that with this kind of value change lister
BeanItemContainer container = new BeanItemContainer<>(
Measurement.class,
measurements);
Grid grid = new Grid(container);
grid.setEditorEnabled(true);
ComboBox parameterComboBox = new ComboBox();
ComboBox unitComboBox = new ComboBox();
parameterComboBox.addItems(Parameter.Pressure, Parameter.Temperature, Parameter.Time);
parameterComboBox.addValueChangeListener(v -> setUnits(parameterComboBox, unitComboBox));
grid.getColumn("parameter").setEditorField(parameterComboBox);
grid.getColumn("unit").setEditorField(unitComboBox);
Units could be updated like this. I think you need to preserve current value and set it back if you replace available items in the combobox.
private void setUnits(ComboBox parameterComboBox, ComboBox unitComboBox) {
Object currentValue = unitComboBox.getValue();
List<String> units = unitsForParameter(parameterComboBox.getValue());
unitComboBox.removeAllItems();
unitComboBox.addItems(units);
if (units.contains(currentValue)) {
unitComboBox.setValue(currentValue);
} else {
unitComboBox.setValue(null);
}
}
private List<String> unitsForParameter(Object value) {
if (value == null) {
return Collections.emptyList();
} else if (value == Parameter.Pressure) {
return asList("Pascal", "Bar");
} else if (value == Parameter.Temperature) {
return asList("Celcius", "Kelvin");
} else if (value == Parameter.Time) {
return asList("Second", "Minute");
} else {
throw new IllegalArgumentException("Unhandled value: " + value);
}
}

SelectionForeColor not working for link cells in DataGridViewLinkColumn of DataGridView

In my Winform 4.5 app, I have a DataGridView with first column as a link column. I would like to have the link color of the selected link cell to be white. Since by default the background color of a selected row (or a cell) is blue and the ForeColor of all the links are also blue, when user selects a row (or a link cell) the text of the link is not readable. I tried writing the following code but it does not change the link color of selected link cell at all.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewLinkCell cell in ((DataGridView)sender).SelectedCells)
{
if (cell.ColumnIndex == 0)
{
if (cell.Selected)
{
cell.Style = new DataGridViewCellStyle()
{
SelectionForeColor = SystemColors.HighlightText
};
}
}
}
}
I then modified the above code as follows. But it changes the link color of all the links to white - that makes non-selected link cells to be not readable since the backcolor of those links is also white:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewLinkCell cell in ((DataGridView)sender).SelectedCells)
{
if (cell.ColumnIndex == 0)
{
if (cell.Selected)
{
cell.LinkColor = SystemColors.HighlightText;
}
}
}
}
I tested both the codes by setting a breakpoint inside the foreach loop and selecting a link cell. I noticed that the code does go through exactly one iteration of the foreach loop correctly. Moreover, I have made no change to the default settings of the DataGridViewLinkColumn
Edit
By default the DataGridView looks like this on a row selection. Notice that the cell in the second column changes its ForeColor to white but not the cell in the first column:
I want it to looks like this on a row selection:
Edit The CellLeave event will always occur when an attempt is made to navigate away from a cell.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewLinkCell cell in
((DataGridView) sender).SelectedCells.OfType<DataGridViewLinkCell>())
{
if (cell.Selected)
{
cell.LinkColor = SystemColors.HighlightText;
}
}
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewLinkCell cell in
((DataGridView) sender).Rows[e.RowIndex].Cells.OfType<DataGridViewLinkCell>())
{
cell.LinkColor = cell.LinkVisited ? Color.Purple : Color.Blue;
}
}
I've experienced the same issue and I got it working using the CellFormatting event. Below you'll find a generic solution for this:
void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
SetGridLinkColor(sender as DataGridView, e.RowIndex, e.ColumnIndex, Color.White);
}
public static void SetGridLinkColor(DataGridView grd, int rowIndex, int columnIndex, Color selectedColor)
{
if (grd == null || !(grd.Columns[columnIndex] is DataGridViewLinkColumn))
return;
if (grd.Rows[rowIndex].Selected)
{
((DataGridViewLinkCell)grd.Rows[rowIndex].Cells[columnIndex]).LinkColor = selectedColor;
((DataGridViewLinkColumn)grd.Columns[columnIndex]).VisitedLinkColor = selectedColor;
}
else
{
Color color = ((DataGridViewLinkColumn)grd.Columns[columnIndex]).LinkColor;
((DataGridViewLinkCell)grd.Rows[rowIndex].Cells[columnIndex]).LinkColor = color;
((DataGridViewLinkColumn)grd.Columns[columnIndex]).VisitedLinkColor = color;
}
}

Capturing Checkbox click event in Windows Forms DataGridview

I have a Winforms DataGridView in my application.
I've two checkbox columns along with 5 other columns from the database. These two checkbox columns are added usng DataGridViewCheckBoxColumn.
When the user clicks on the 2nd checkbox, I need to show a message to the user if the first checkbox is not checked for that row.
How do I go about this?
I tried this,but the cell value is coming as null.
What am i doing wrong?
private void dgTest_CellClick(System.Object sender, DataGridViewCellEventArgs e)
{
DataGridViewCheckBoxCell officialCbCell = row.Cells[1] as DataGridViewCheckBoxCell;
DataGridViewCheckBoxCell includeCbCell = row.Cells[0] as DataGridViewCheckBoxCell;
if (officialCbCell != null)
{
if (officialCbCell.Value != null && (bool)officialCbCell.Value == true)
{
if (includeCbCell != null && (bool)includeCbCell.Value == false)
{
MessageBox.Show("INVALID");
}
}
}
}
Thanks.
You can try using the CellValueChanged event of the grid
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
bool isChecked = (Boolean) dataGridView1[0, e.RowIndex].FormattedValue;
if (isChecked)
dataGridView1[1, e.RowIndex].Value = true;
}
}
if checked then you can set the other column as also checked or any other validation
CellContentClick event and cell.EditingCellFormattedValue property are also useful if you just un/clicked the cell.

MVVMLight Multiple Listbox Selection

I have an ItemsContol bound to a Country model - which look like this.
Country
--int Id
--string Name
--List Counties
In the DataTemplate of the ItemsControl there's a Listbox - which is bound to the Counties property.
So what I want is only one item in any of the listboxes be selected at any one time.
For example:
I have an item selected in the first listbox and I click an item in the second listbox, then the first listbox shouldn't have any selected items.
Any ideas ??
Add a SelectedCounty property to your Country object. Then you can bind the SelectedItem on your ListBox to that property. Then in code manually set all others to null. Something like so
Country.OnPropertyChanged += (s,e) =>
{
if(e.PropertyName == "SelectedCounty")
{
foreach(Country country in MyCountries)
if(country != sender)
country.SelectedCounty = null;
}
}
Just for reference here's the solution I'm using - it resides in the CountryViewModel
private CountyModel _selectedcounty;
public CountyModel SelectedCounty
{
get { return _selectedcounty; }
set
{
_selectedcounty = value;
RaisePropertyChanged("SelectedCounty");
if (value != null)
{
if (CountySelectedEvent != null)
CountySelectedEvent(value, EventArgs.Empty);
Messenger.Default.Send<CountyModel>(value, "SelectedCounty");
}
}
}
public CountryViewModel()
{
Counties = new ObservableCollection<CountyModel>();
Messenger.Default.Register<CountyModel>(this, "SelectedCounty",
msg =>
{
if(msg != this.SelectedCounty && msg != null)
this.SelectedCounty = null;
});
}
Hope it helps someone :)

Resources