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;
}
}
}
Related
I have a WPF project - a datagrid with four columns: ColumnOne, ColumnTwo, columnThree, ColumnFour. Is it possible that when user sorts by ColumnOne or ColumnTwo - then code behind adds sorting by ColumnThree as well, so it gets sorted like SortBy("ColumnOne").ThenBy("ColumnThree"). If that matters, ItemsSource for my DataGrid is PagedCollectionView, which supports SortDescriptors.
You have to override DataGrid.OnSorting like in this simple example (but please extend it to your full requirements) and use the custom control instead of the standard DataGrid in your XAML.
public class MyDataGrid : DataGrid
{
protected override void OnSorting(DataGridSortingEventArgs eventArgs)
{
base.OnSorting(eventArgs);
var test = eventArgs.Column;
if (test.Header.ToString() == "ColumnOne" && test.SortDirection.HasValue
&& test.SortDirection.Value.Equals(ListSortDirection.Ascending)
)
{
ICollectionView view = CollectionViewSource.GetDefaultView(this.ItemsSource);
view.SortDescriptions.Add(new SortDescription("ColumnThree", ListSortDirection.Ascending));
view.Refresh();
this.Columns[2].SortDirection = ListSortDirection.Ascending;
}
}
}
The above code handles both the colletion sorting and the SortDirection property setting for ColumnThree in just one case: when the user orders by ColumnOne ascending.
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;
}
}
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!
I have a user control that contains a WPF toolkit DataGrid. This control is used in many different places in my app. The grid has no knowledge as to the type of data that will show. Is there a way to initially sort the grid by the first column in ascending order no matter what data the grid is populated with? I don't think I can use a CollectionViewSource because I don't know the PropertyName of the property bound to the first column.
You could hook to an event:
dataGrid.AutoGeneratedColumns += dataGrid_AutoGeneratedColumns;
and sort the first column:
void dataGrid_AutoGeneratedColumns(object sender, EventArgs e)
{
var firstCol = dataGrid.Columns.First();
firstCol.SortDirection = ListSortDirection.Ascending;
dataGrid.Items.SortDescriptions.Add(new SortDescription(firstCol.SortMemberPath, ListSortDirection.Ascending));
}
I would suggest you to create a derived separate DataGrid control, placing this logic there and using the new control to avoid repeating the code every time.
public class CustomDataGrid : DataGrid
{
public DynamicDataGrid()
{ ... }
...
}
There is a datagrid with n number of rows, the first column in the Grid is a CheckBox column, now i want to enable/Disable some of the Rows(so that user cannot check the checkbox) of datagrid depending on some values.o How is it possible using MVVM pattern.
You are probably binding a list (IEnumerable) of data objects to your grid. To keep it nice and clean, what you need to do is wrap each of those data objects with another object, let's call it the RowViewModel. This RowViewModel can then hold extra properties, like a boolean which you can bind your checkbox's IsEnabled property to, that boolean can be calculated from the state of the data object, or even from the state of the parent view model should you pass a reference of it to the RowViewModel.
You can also extend this a little further and have row specific context menu items controlled by each RowViewModel, etc. Using the RowViewModel in this way ensures that you keep your data object nice and pure, you don't pollute it with stuff it doesn't need.
Using the LoadingRow event for each row you can update the controls in any cell you desire. For instance,
private void MyDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
MyDataObjectClass dataContext = (e.Row.DataContext as MyDataObjectClass);
foreach (DataGridColumn col in from cols in MyDataGrid.Columns orderby cols.DisplayIndex select cols)
{
FrameworkElement fe = col.GetCellContent(e.Row);
DataGridCell result = fe.Parent as DataGridCell;
// as an example, find a template column w/o a sort member path
if (col is DataGridTemplateColumn && col.SortMemberPath == null)
{
CheckBox button = VisualTreeExtensions.GetChildrenByType<CheckBox>(fe)[0];
button.IsEnabled = true; // insert your data condition...
}
}
}