object reference not set for DataView from gridivew.DataSource as DataView - winforms

I have method validatingeditor to validate for duplicate
private void GridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
{
GridView view = sender as GridView;
DataView currentDataView = view.DataSource as DataView;
if (view.FocusedColumn.FieldName == "Sequence")
{
//check duplicate code
string currentCode = e.Value.ToString();
for (int i = 0; i < currentDataView.Count; i++)
{
if (i != view.GetDataSourceRowIndex(view.FocusedRowHandle))
{
if (currentDataView[i]["Sequence"].ToString() == currentCode)
{
e.ErrorText = "Duplicate Code detected.";
e.Valid = false;
break;
}
}
}
}
}
But it says object reference not set which the problem is at DataView currentDataView = view.DataSource as DataView;
But I do not understand why.

I was populating my gridcontrol with the ado entity dataset. so after calling adapter.Fill(dataset). I also have to write gridcontrol.DataSource=dataset;

Related

GridvView.SelectedCells.Add() does not update view

GridViews Selected cells is a IList, so it does not update the view, when i add selections from my ViewModel.
Is there a way to force updating the view for selected Cells. The way i currently uddate views is by having a Attached behavior, which updates the list on ViewModel, but also the GridView, but the GridView does not update its visuals.
here is my attached behavior:
public static List<GridCell> GetSelectedCells(DependencyObject obj)
{
return (List<GridCell>)obj.GetValue(SelectedCellsProperty);
}
public static void SetSelectedCells(DependencyObject obj, List<GridCell> value)
{
obj.SetValue(SelectedCellsProperty, value);
}
public static readonly DependencyProperty SelectedCellsProperty =
DependencyProperty.RegisterAttached("SelectedCells", typeof(List<GridCell>), typeof(DataGridHelper), new UIPropertyMetadata(null, OnSelectedCellsChanged));
static SelectedCellsChangedEventHandler GetSelectionChangedHandler(DependencyObject obj)
{
return (SelectedCellsChangedEventHandler)obj.GetValue(SelectionChangedHandlerProperty);
}
static void SetSelectionChangedHandler(DependencyObject obj, SelectedCellsChangedEventHandler value)
{
obj.SetValue(SelectionChangedHandlerProperty, value);
}
static readonly DependencyProperty SelectionChangedHandlerProperty =
DependencyProperty.RegisterAttached(nameof(SelectedCellsChangedEventHandler), typeof(SelectedCellsChangedEventHandler), typeof(DataGridHelper), new UIPropertyMetadata(null));
private static bool NewResouce = false;
static void OnSelectedCellsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
{
if (d is DataGrid)
{
NewResouce = true;
DataGrid datagrid = d as DataGrid;
if (GetSelectionChangedHandler(d) == null)
{
SelectedCellsChangedEventHandler selectionchanged = (sender, e) =>
{
if (!NewResouce)
{
List<GridCell> cells = new List<GridCell>();
foreach (var selectedell in datagrid.SelectedCells)
{
string header = selectedell.Column.Header.ToString();
GridCell cell = new GridCell
{
RowIndex = datagrid.Items.IndexOf(selectedell.Item),
ColumnIndex = selectedell.Column.DisplayIndex,
Parent = selectedell.Item as ExpandoObject,
ColumnHeader = header,
Value = (selectedell.Item as IDictionary<string, object>)[header]
};
cells.Add(cell);
}
SetSelectedCells(d, cells);
}
};
SetSelectionChangedHandler(d, selectionchanged);
datagrid.SelectedCellsChanged += GetSelectionChangedHandler(d);
}
foreach (var selected in GetSelectedCells(d) as List<GridCell>)
{
DataGridCellInfo cell = new DataGridCellInfo(selected.Parent, datagrid.Columns[selected.ColumnIndex]);
if (!datagrid.SelectedCells.Contains(cell))
{
datagrid.SelectedCells.Add(cell);
}
}
NewResouce = false;
}
}
}
The reason why i have the NewResource boolean, is that the event selection changed does actually fire when I add newly selected items. Its just the view that does not update its selections.
The SelectedCells is added after the view is loaded, due to its located inside a tab, and it looks like the data on gridview is empty before view is loaded, so I cannot set selected before the view is loaded.
Answer is simple. Please use an ObservableCollection instead of the list.
The observable collection is a dependency object and it will raise a propertyChanged event to the view to notify it regarding the property change and the view will be updated.
I ended up finden some properties that does implement INotifyPropertyChanged, on DataGridCells heres the solution:
static void OnSelectedCellsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
{
if (d is DataGrid)
{
DataGrid datagrid = d as DataGrid;
if (GetSelectionChangedHandler(d) == null)
{
SelectedCellsChangedEventHandler selectionchanged = (sender, e) =>
{
List<GridCell> cells = new List<GridCell>();
foreach (var selectedell in datagrid.SelectedCells)
{
string header = selectedell.Column.Header.ToString();
GridCell cell = new GridCell
{
RowIndex = datagrid.Items.IndexOf(selectedell.Item),
ColumnIndex = selectedell.Column.DisplayIndex,
Parent = selectedell.Item as ExpandoObject,
ColumnHeader = header,
Value = (selectedell.Item as IDictionary<string, object>)[header]
};
cells.Add(cell);
}
SetSelectedCells(d, cells);
};
SetSelectionChangedHandler(d, selectionchanged);
datagrid.SelectedCellsChanged += GetSelectionChangedHandler(d);
}
foreach (var selected in GetSelectedCells(d) as List<GridCell>)
{
DataGridCell actualCell = datagrid.GetCell(selected.RowIndex, selected.ColumnIndex);
actualCell.IsSelected = true;
actualCell.Focus();
}
}
}
for it to work i added some extention method to datagrid, to make it easier to get the Cell here the extention methodes: (stolen from this blog)
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
public static DataGridCell GetCell(this DataGrid grid, int rowIndex, int columnIndex)
{
return GetCell(grid, GetRow(grid, rowIndex), columnIndex);
}
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
public static DataGridRow GetRow(this DataGrid grid, int index)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// May be virtualized, bring into view and try again.
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items[index]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}

Xtragrid SelectionChanged or Alternative

I'm having an issue here.
I have a XtraGrid in winforms with a multiselect mode true, I need to validate if the row I'm selecting, if it matches a condition, select it, if not, deselect it. I'm currently using the SelectionChanged method like this:
private void grdProducts_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
try
{
GridView view = sender as GridView;
int[] selectedRows = view.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
if (view.IsRowSelected(selectedRows[i]))
{
Product product = view.GetRow(selectedRows[i]) as Candidato;
ProcessStatus processStatus = _procesoStatusService.GetProduct(product.IdProduct);
if (processStatus.Proccess.Inventory == (int)ProductInventory.Yes)
{
view.UnselectRow(selectedRows[i]);
XtraMessageBox.Show("One or more products are currently in inventory.");
}
}
}
}
catch (Exception)
{
throw;
}
}
The problem here is when the code reaches the view.UnselectRow(selectedRows[i]); line, the SelectionChanged method is called again and the program send multiple XtraMessageBox.
Any help?
You must use BaseView.BeginSelection method before your code and BaseView.EndSelection method after your code. This will prevent the ColumnView.SelectionChanged event to raise.
Here is example:
private void grdProducts_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
var view = sender as GridView;
if (view == null) return;
view.BeginSelection();
try
{
int[] selectedRows = view.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
if (view.IsRowSelected(selectedRows[i]))
{
Product product = view.GetRow(selectedRows[i]) as Candidato;
ProcessStatus processStatus = _procesoStatusService.GetProduct(product.IdProduct);
if (processStatus.Proccess.Inventory == (int)ProductInventory.Yes)
{
view.UnselectRow(selectedRows[i]);
XtraMessageBox.Show("One or more products are currently in inventory.");
}
}
}
}
catch (Exception)
{
view.EndSelection();
throw;
}
view.EndSelection();
}

Unable to get exact WPF DataGrid embedded control type From DataGridTemplateColumn

public void MouseSingleClickEditable(object sender, MouseButtonEventArgs e)
{
if ((!datagrid.HasItems) || datagrid.SelectedIndex < 0) return;
DataGridColumn clmn = datagrid.CurrentColumn;
if (clmn != null)
{
String columnType = clmn.GetType().Name;
switch (columnType)
{
case "DataGridTemplateColumn":
case "DataGridCheckBoxColumn":
row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromItem(datagrid.Items[datagrid.SelectedIndex]);
if (!row.IsEditing)
{
datagrid.IsReadOnly = false;
datagrid.BeginEdit();
}
break;
default:
break;
}
}
}
I have 2 DatePickers, Checkbox and a combobox in my WPF Datagrid but while geeting the column type I am getting it only as DataGridTemplateColumn instead of type DataGridDatePickerColumn or DataGridComboboxColumn. How to extract the exact type of Control from the DataGridTemplateColumn.
public void MouseSingleClickEditable(object sender, MouseButtonEventArgs e) {
var datagrid = new DataGrid();
if ((!datagrid.HasItems) || datagrid.SelectedIndex < 0) return;
DataGridColumn clmn = datagrid.CurrentColumn;
if (clmn != null) {
if (clmn is DataGridCheckBoxColumn) {
//do something
} else if (clmn is DataGridTemplateColumn) {
var templateColumn = (DataGridTemplateColumn)clmn;
var rootControlOfCellTemplate = templateColumn.CellTemplate.LoadContent();
var rootControlOfCellEditingTemplate = templateColumn.CellEditingTemplate.LoadContent();
// you can now check for types of the template. CellEditingTemplate is for template in edit mode, and CellTemplate for "non-edit" mode
// for example
if (rootControlOfCellTemplate is Button) {
//do something
}
if (rootControlOfCellEditingTemplate is DatePicker) {
//do something
}
}
}
}
Note how I check for types. Normally you should do it like this, not by hard-coded strings.

RepositoryItemCheckEdit doesn't stay checked

I try to add a RepositoryItemCheckEdit to my GridView using devexpress and Winforms. However, I can get only one checkbox be checked. If I check another one, the checkbox I checked before becomes unchecked. I followed everything I can find on the net, but couldn't make this work. What am I missing?
The code part I insert the column:
gcIsEmirleri.DataSource = (from i in isemirleri
select new
{
ID = i.isEmriId,
// other attributes
}).ToList();
GridColumn column = gvIsEmirleri.Columns["Sec"];
if (column == null)
{
gvIsEmirleri.BeginUpdate();
DataColumn col = new DataColumn("Sec", typeof(bool));
column = gvIsEmirleri.Columns.AddVisible("Sec");
col.VisibleIndex = 0;
col.Caption = "Sec";
col.Name = "Sec";
col.OptionsColumn.AllowEdit = true;
gvIsEmirleri.EndUpdate();
gvIsEmirleri.Columns["Sec"].UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
RepositoryItemCheckEdit chk = new RepositoryItemCheckEdit();
chk.ValueChecked = true;
chk.ValueUnchecked = false;
gvIsEmirleri.Columns["Sec"].ColumnEdit = chk;
chk.QueryCheckStateByValue += chk_QueryCheckStateByValue;
}
The code part I make the checkbox two-stated instead of three:
private void chk_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
{
if (e.Value == null)
{
e.CheckState = CheckState.Unchecked;
e.Handled = true;
}
}
EDIT: I created a List<bool> chkList; and do the following operations:
This function is added to checkedits' CheckStateChanged:
private void chk_CheckStateChanged(object sender, EventArgs e)
{
CheckEdit chk = sender as CheckEdit;
if (chk.Checked)
chkList[gvIsEmirleri.FocusedRowHandle] = true;
else
chkList[gvIsEmirleri.FocusedRowHandle] = false;
FillBindingSource();
}
In FillBindingSource I added the lines:
for (int i = 0; i < chkList.Count; i++)
{
if (chkList[i])
gvIsEmirleri.SetRowCellValue(i, "Sec", true);
}
I debug these lines, I see that List has correct bool values and gvIsEmirleri.SetRowCellValue(i, "Sec", true); is operated when it has to. However, it still doesn't work.
My guess is : You are using an unbound Column, and you are not saving the checked / unckecked info, so, after the selected row is left, the checkBox get it's initial value (unckecked).
For this, I suggest you handle the CustomUnboundColumnData event of your view. Here is a simple :
readonly Dictionary<object, bool> checkedMap = new Dictionary<object, bool>();
private void viewScales_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
// Check what column
if (e.Column != gvIsEmirleri.Columns["Sec"])
return;
if (e.IsGetData)
{
// check if the row has been checked and set it's value using e.Value
bool checked;
if (checkedMap.TryGetValue(e.Row, out checked))
e.Value = checked;
}
if (e.IsSetData)
{
var checked = Convert.ToBoolean(e.Value);
// Check if the key already exist
if (checkedMap.ContainsKey(e.Row))
scaleMap.Remove(e.Row);
checkedMap.Add(e.Row, checked);
}
}
Note : This is the way I resolved a similar problem, but I did not test the code I just wrote.

How to get DataGridColumnHeader from DataGridColumn?

My code is as follows:
void mainDataContextObj_CutSelectedColumnEvent(string columnId)
{
IList<DataGridColumn> columns = dg.Columns;
for(int i=2; i<dg.Columns.Count; i++)
{
DataGridColumnHeader headerObj = dg.Columns[i].Header as DataGridColumnHeader;
//This always returns headerObj as null!!!
}
}
I need DataGridColumnHeader from the column. Where am I going wrong?
The Header object of the DataGridColumn is actually the visible header of that column, whatever you set it to be. DataGridColumn is not part of the Visual Tree so there is not direct way to access the DataGridColumnHeader for it (we can't even be sure it exists yet). But you can do something like this to try and access it
DataGridColumnHeader headerObj = GetColumnHeaderFromColumn(column);
private DataGridColumnHeader GetColumnHeaderFromColumn(DataGridColumn column)
{
// dataGrid is the name of your DataGrid. In this case Name="dataGrid"
List<DataGridColumnHeader> columnHeaders = GetVisualChildCollection<DataGridColumnHeader>(dataGrid);
foreach (DataGridColumnHeader columnHeader in columnHeaders)
{
if (columnHeader.Column == column)
{
return columnHeader;
}
}
return null;
}
public List<T> GetVisualChildCollection<T>(object parent) where T : Visual
{
List<T> visualCollection = new List<T>();
GetVisualChildCollection(parent as DependencyObject, visualCollection);
return visualCollection;
}
private void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
{
visualCollection.Add(child as T);
}
else if (child != null)
{
GetVisualChildCollection(child, visualCollection);
}
}
}
While Fredrik's answer provides a refactored approach with additional method that could potentially be reused in other parts of the code, I preferred to consolidate his methods in to one single method. There may also be some small performance gain because it can end the search as soon as it finds the header and it does not need to continue to search through all the children in the visual tree (this is most likely negligible for most cases).
private DataGridColumnHeader GetHeader(DataGridColumn column, DependencyObject reference)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(reference); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(reference, i);
DataGridColumnHeader colHeader = child as DataGridColumnHeader;
if ((colHeader != null) && (colHeader.Column == column))
{
return colHeader;
}
colHeader = GetHeader(column, child);
if (colHeader != null)
{
return colHeader;
}
}
return null;
}
And it is used like so:
DataGridColumnHeader colHeader = GetHeader(column, myDataGrid);
if (colHeader == null) { /* Not found */ }

Resources