Home Forums WinForms controls Xceed Grid for WinForms Data Binding (showing specific columns from Data Table) Reply To: Data Binding (showing specific columns from Data Table)

User (Old forums)
Member
Post count: 23064

Thanks for all of your help. I had to make some major adjustments to the code above, mainly because it assumed that the next Cell that could receive focus was always just one step away the Current Cell. I also found that the CanBeCurrent property returns true even when the Cell’s Visible property is false, so I could not use that property. For anyone who is interested here is the complete code for making an Exceed grid behave (feel) more like the standard Microsoft DataGrid (just wire your grid’s Enter and KeyDown events into the Handlers below and use the rest of the code as necessary):

using System;
using System.Windows.Forms;
using Xceed.Grid;
using Xceed.Grid.Collections;

namespace WinUI.GridMod
{
/// <summary>
/// Author: David Doyle
/// Purpose: Helps make an Xceed Grid behave more like the standard DataGrid
/// </summary>
public sealed class XceedGridHelper
{
private XceedGridHelper()
{
}

private static bool alreadyEntered = false;

public static void FormatGrid(GridControl grid, Column[] columns)
{
// Hide all database columns by default
foreach(Column c in grid.Columns)
c.Visible = false;

// Show the columns indicated by the column array
foreach(Column c in columns)
{
Column gridColumn = grid.Columns[c.FieldName];
gridColumn.Visible = true;
gridColumn.Width = c.Width;
gridColumn.Title = c.Title;
gridColumn.ReadOnly = c.ReadOnly;
}
}

public static Xceed.Grid.Column MakeColumn(string fieldName, Type dataType, string title,
int width, bool isReadOnly)
{
Column newCol = new Column(fieldName, dataType);
newCol.Title = title;
newCol.Width = width;
newCol.ReadOnly = isReadOnly;
newCol.Visible = true;

return newCol;
}

private static void MoveFirstCell(GridControl grid, int currentRowSortedIndex,
int currentColVisibleIndex)
{
Xceed.Grid.Cell nextCell = grid.CurrentCell;

// Retrieve the list of the DataRows as they are sorted in the grid.
ReadOnlyDataRowList sortedDataRowList = grid.GetSortedDataRows(true);

// Retrieve the current row.
Xceed.Grid.DataRow currentDataRow = grid.DataRows[currentRowSortedIndex];

// Navigate through the grid in the order that the columns and rows are displayed.

// If we are not at the end of the row
if(currentColVisibleIndex < grid.Columns.Count – 1)
{
// If the current cell is read only or not visible, move to the next cell in the current row
if(grid.CurrentCell.ReadOnly || ! grid.CurrentCell.Visible)
nextCell =
currentDataRow.Cells[grid.Columns.GetColumnAtVisibleIndex(currentColVisibleIndex + 1).Index];
// If cell is not read only and is visible
if(! nextCell.ReadOnly && nextCell.Visible)
{
// Enter edit mode
grid.CurrentCell = nextCell;
grid.CurrentCell.EnterEdit();
}
// Move to the next cell
else
{
MoveFirstCell(grid, currentRowSortedIndex, currentColVisibleIndex + 1);
}
}
// If the end of the current row is reached
else if(currentRowSortedIndex < sortedDataRowList.Count – 1)
{
// If the current cell is read only or not visible, go to first cell of the next row and continue
if(grid.CurrentCell.ReadOnly || ! grid.CurrentCell.Visible)
{
nextCell =
sortedDataRowList[currentRowSortedIndex + 1].Cells[grid.Columns.GetColumnAtVisibleIndex(0).Index];
// Reset the selected row
grid.SelectedRows.Clear();
grid.SelectedRows.Add(sortedDataRowList[currentRowSortedIndex + 1]);
}
// If cell is not read only and is visible
if(! nextCell.ReadOnly && nextCell.Visible)
{
// Enter edit mode
grid.CurrentCell = nextCell;
grid.CurrentCell.EnterEdit();
}
// Move to the next cell
else
{
MoveFirstCell(grid, currentRowSortedIndex + 1, 0);
}
}
// Else edit the current cell, if not read only
else
{
if(! grid.CurrentCell.ReadOnly)
grid.CurrentCell.EnterEdit();
}
}

private static void MoveNextCell(object sender, System.Windows.Forms.KeyEventArgs e,
int currentRowSortedIndex, int currentColVisibleIndex)
{
GridControl grid = (GridControl)sender;
Xceed.Grid.Cell nextCell = null;

// Check if the current cell is being edited. If it is, then we will
// handle the Tab and arrow keys.
if(e.KeyData == Keys.Tab)
{
// Retrieve the list of the DataRows as they are sorted in the grid.
ReadOnlyDataRowList sortedDataRowList = grid.GetSortedDataRows(true);

// Retrieve the current row.
Xceed.Grid.DataRow currentDataRow = grid.DataRows[currentRowSortedIndex];

// Let the grid know that we handled the Tab and/or arrow key
e.Handled = true;

// Navigate through the grid in the order that the columns and rows are displayed.

// If we are not at the end of the row
if (currentColVisibleIndex < grid.Columns.Count – 1)
{
// Move to the next cell in the current row
nextCell =
currentDataRow.Cells[grid.Columns.GetColumnAtVisibleIndex(currentColVisibleIndex + 1).Index];
// If next cell can be current
if(! nextCell.ReadOnly && nextCell.Visible)
{
grid.CurrentCell = nextCell;
grid.CurrentCell.EnterEdit();
}
// Move to the next cell
else
{
MoveNextCell(sender, e, currentRowSortedIndex, currentColVisibleIndex + 1);
}
}
// If the end of the current row is reached
else if(currentRowSortedIndex < sortedDataRowList.Count – 1)
{
// Go to first cell of the next row and continue
nextCell =
sortedDataRowList[currentRowSortedIndex + 1].Cells[grid.Columns.GetColumnAtVisibleIndex(0).Index];
// Reset the selected row
g

Imported from legacy forums. Posted by dd3000 (had 5975 views)