Anyone here knows how to accomplish this kind of rows using DevExpress GridView on WinForms?
I suggest you to go through documentation for the topic: Customizing Appearances of Individual Rows and Cells.
You can do this using various ways:
Customizing Appearances
Using the GridView.CustomDrawCell event
The GridView.RowStyle event can be handled to customize the appearance
of individual rows in GridViews. To customize a specific cell's
appearance, handle the GridView.RowCellStyle event instead. The
GridView.RowStyle event fires before the GridView.RowCellStyle event.
Example:
using DevExpress.XtraGrid.Views.Grid;
private void gridView1_RowStyle(object sender,
DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
GridView View = sender as GridView;
if(e.RowHandle >= 0) {
string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
if(category == "Beverages") {
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
}
}
}
References:
Changing Row Colours on DevExpress GridView
Hope this help..
You click on GridView and then click on Theme, you can choose from this.
Here is how you would do in a DataGridView control in forms. Should be similar I assume, it has been a while since I last used DevExpress. But you should go through the DevExpress' documentation, since all of the components are very well documented.
foreach (DataGridViewRow row in dgInformation.Rows)
{
if (some criteria here == 1234)
{
row.DefaultCellStyle.BackColor = Color.Goldenrod;
}
}
Related
To take advantage of DevExpress Winforms XtraGrid incremental search, the user must first click on a row before typing.
How to set focus on the first row programmatically, to avoid that step?
In the control's DataSourceChanged event, I've tried setting focus to the control itself
gridControl1.Focus();
or to its default view
gridControl1.DefaultView.Focus();
but neither had the desired effect.
Also have tried setting the FocusedRowHandle:
gridControl1.DataSource = T;
this.gridView1.FocusedRowHandle = 0;
Try this:
private void gridControl1_DataSourceChanged(object sender, EventArgs e)
{
this.ActiveControl = this.gridControl1;
this.gridView1.FocusedRowHandle = 0;
}
The DataGridViewComboBoxColumn implements a DropDownList functionality, which validates the data against the values in the list.
I want to implement a custom column which behaves like a ComboBox with DropDown functionality, which does not validate the data against the list.
Is there a way to create a custom column which:
inherits from DataGridViewComboBoxColumn
changes the DropDownStyle property of editing control to ComboBoxStyle.DropDown
disables cell validation against the items in the list
Does anybody have sample code with this behavior?
I have tried several solutions and settled on using a standard text box column and enabling the AutoComplete feature. I am not using a custom column.
During initialization, I create an AutoCompleteStringCollection containing the elements for the dropdown list
private AutoCompleteStringCollection _SerialNumbers ;
I attach to the EditingControlShowing event, check that it is the right column and enable AutoComplete.
private void dgvChannels_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ( dgvChannels.CurrentCell.OwningColumn == SerialNumber )
{
TextBox c = e.Control as TextBox;
if (c != null)
{
c.AutoCompleteMode = AutoCompleteMode.Suggest;
c.AutoCompleteCustomSource = _SerialNumbers;
c.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
}
Functionally, a text box with AutoComplete is very similar to a Combobox with DropDown style. In my case, the functionality is actually better.
Weirdly, you can also use AutoComplete on a ComboBox, which can result in there being two drop lists at the same time. That's some user experience!
Is there anyone who has experience with the Devexpress GridControl.
I have a class which has a List of Objects in it. (This class is binded to the Grid).
This grid has a few columns to display the class.
I want a row to have another color when the list of objects.count is > 1
I've tried to like make an In-Place Editor Repository LookUpEdit item so I have the List of objects to set into a column.
DevExpress Family: Winforms
There are few methods that suit your needs. you can use the Appearance specific events that are more flexible.
Check the Customizing Appearances of Individual Rows and Cells on
devExpress documentation.
Check this how can you change the appearance conditionally on the basis of some column value:
using DevExpress.XtraGrid.Views.Grid;
private void gridView1_RowStyle(object sender,
DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
GridView View = sender as GridView;
if(e.RowHandle >= 0) {
string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
if(category == "Beverages") {
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
}
}
}
I'm using a DataGrid in my silverlight application to display some data that's refreshed on a timer. My problem is that when this happens the vertical scrollbar in the grid resets to the top, whereas I want it to stay in the same position. Does anyone know how I can make this happen?
I've tried overriding the ItemsSource property on the grid to store the vertical scroll position and then reset it, but this only affects the scrollbar and doesn't force the correct rows to be displayed. Is there a way to force this behaviour?
Here is a similar question about Setting the scroll bar position on a ListBox
After rebinding Silverlight Listbox control how do you get it listbox to scroll to back to the top?
Since the DataGrid also supports a ScrollIntoView method, you should be able to use a similar technique such as
theDataGrid.ItemsSource = data;
theDataGrid.UpdateLayout();
theDataGrid.ScrollIntoView(theDataGrid.SelectedItem, theDataGrid.Columns[0]);
I couldn't find a decent answer last time I looked. I wanted to keep the current element selected in the grid but that wouldn't work on an ICollectionView refresh (I use MVVM and get automatic updates from the server).
ScrollIntoView() was not an option for me because the currently selected item may NOT be in view. Having the CurrentChanged event firing out of control was also quite a bother.
In the end, I used the Infragistics grid and it does just that out of the box. Problem solved for me.
You may have a look at the DevExpress free grid. I think it had the same nice behaviour (I tested it but I can't remember the outcome).
You could try setting the SelectedItem thro the UI thread, so that the UI can refresh itself,
like so
private void Button_Click(object sender, RoutedEventArgs e)
{
Person p = new Person() { Name="sss",Age=11}; //datagird's itemsSource is Collection<person>
people.Add(p);
dg.SelectedItem = p; //dg is my datagrid name
Dispatcher.BeginInvoke(() => { dg.SelectedItem = p; });
}
Im assuming that new rows are loaded thro the ViewModel, so thats why it makes sense to place the BeginInvoke there. Since the ViewModel operations run on a different thread, and just setting the SelectedItem on its own might not work, this has worked for someone else
I've also had issues with this. I solved it by remembering the item I want to scroll to, then re-binding the DataGrid. I handle the LayoutUpdated event in order to implement the desired functionality:
void MyDataGrid_LayoutUpdated(object sender, EventArgs e)
{
// Reference the data item in the list you want to scroll to.
object dataItem = yourDataItem;
// Make sure the item is not null and didn't already scroll to the item.
if (dataItem != null && this.dataItemScrolledTo != dataItem)
{
// Remember the item scrolled to.
this.dataItemScrolledTo = dataItem;
// Scroll datagrid to the desired item.
MyDataGrid.ScrollIntoView(dataItem, MyDataGrid.Columns[0]);
}
}
I've modified CodeMaster's solution so that you don't need a class level variable. Put this code in the method that updates the ItemsSource. It will dynamically create the eventhandler, attach it, then detach it.
EventHandler MyDataGrid_LayoutUpdated = null;
MyDataGrid_LayoutUpdated = (s, e) =>
{
MyDataGrid.ScrollIntoView(dataItem, MyDataGrid.Columns[0]);
MyDataGrid.LayoutUpdated -= MyDataGrid_LayoutUpdated;
};
MyDataGrid.LayoutUpdated += MyDataGrid_LayoutUpdated;
Thanks in advance for any help. I've got two combo boxes which I'd like to be dependent on each others input. For example, when I update one, it filters the available values for the next. Thought I had everything working until after I switched between the two a few times, both just because blank.
I've got two combo boxes with countries, FromCountry and ToCountry. These are bound to an ObservableCollection called Countries. I thought it might make sense to use the DroDownOpened event to limit the values available in the dropping down combobox.
Any suggestions or help would greatly be appreciated.
Method that gets called when you click the drop down on the To Country
private void ToCountry_DropDownOpened(object sender, EventArgs e)
{
int FromId = (Countries.Country)FromCountry.selectedItem).ID;
int ToId = (Countries.Country)ToCountry.selectedItem).ID;
this.ToCountry.ItemsSource = AppInfo.CountryList.Where(p=>p.ID!=FromId).OrderBy(c => c.Name);
}
Method that gets called when you click the drop down on the From Country
private void FromCountry_DropDownOpened(object sender, EventArgs e)
{
int FromId = (Countries.Country)FromCountry.selectedItem).ID;
int ToId = (Countries.Country)ToCountry.selectedItem).ID;
this.FromCountry.ItemsSource = AppInfo.CountryList.Where(p=>p.ID!=ToId).OrderBy(c => c.Name);
}
Have you tried having two ObservableCollections ToCountries and FromCountries and then reacting to the selection changed events to populate?
After some experimentation I managed to find a working solution. The below allows me to have two comboboxes dependent on the same datasource, as well as each other. I’m sure elegant approach, but this seems to work.
1) On my ObservableCollection I added two additional properties, ToSelected and FromSelected.
2) Then instead of binding to the Comboboxes, I populated them directory through a loop. Not the best way to do this, but it seemed to be the most straight forward.
foreach (Countries.Country currentCnty in CountryList)
{
ComboBoxItem toItem = new ComboBoxItem();
toItem.Content = currentCnty;
CmboToCountry.Items.Add(toItem);
ComboBoxItem fromItem = new ComboBoxItem();
fromItem.Content = currentCnty;
CmbFromCountry.Items.Add(fromItem);
}
3) I then created method called UpdateLogic. This simply loops through the ObservableCollection setting the selected records for the From and To comboboxes.
4) Then on selection change for each combobox I added the following to set the visibility other combobox items. For example the To Country combobox would set the visibility of the From Country items and vice versa.
//Update the underlying collection with selected items
UpdateLogic((Countries.Country)CmboToCountry.Content, (Countries.Country)CmboFromCountry.Content);
for (int iLoop = 0; iLoop < CmboToLocation.Items.Count; iLoop++)
{
ComboBoxItem currentItem = CmboToCountry.Items[iLoop] as ComboBoxItem;
Countries.Country currentCountry = (Countries.Country)currentItem.Content;
currentItem.Visibility = ((currentCountry.IsFrom) && (currentCountry.ID > 0))
? Visibility.Collapsed : Visibility.Visible;
}
The business requirement of having two inter-dependent comboboxes has been an interesting challenge. Hopefully this helps someone in the future. Though I’m sure there is a more elegant solution to perform this task.