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

If you don’t mind tabbing into and immediately out of the grid (when not in edit mode) or getting stuck in a single row (when in edit mode), then yes you are correct you do not have to set the CurrentCell. However, I am attempting to modify the default behavior to make the grid perform tabbing more like the default MS DataGrid (i.e. pressing the tab button moves you into the grid, through each cell, and then out of the grid at the bottom right.) Below is the code that I am using to achieve this functionality.

private static void MoveFirstCell(GridControl grid)
{
// Retrieve the list of the DataRows as they are sorted in the grid.
ReadOnlyDataRowList sortedDataRowList = grid.GetSortedDataRows(true);

// Retrieve the current row.
if(grid.CurrentRow.GetType() == typeof(Xceed.Grid.DataRow))
{
Xceed.Grid.DataRow currentDataRow = (Xceed.Grid.DataRow)grid.CurrentRow;

// Retrieve the current row’s sort index.
int currentRowSortedIndex = sortedDataRowList.IndexOf(currentDataRow);

// Retrieve the current column’s visible index.
int currentColVisibleIndex = grid.CurrentColumn.VisibleIndex;

// 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, move to the next cell in the current row
if(grid.CurrentCell.ReadOnly)
grid.CurrentCell =
currentDataRow.Cells[grid.Columns.GetColumnAtVisibleIndex(currentColVisibleIndex + 1).Index];
// Skip read only cells
if(grid.CurrentCell.ReadOnly)
MoveFirstCell(grid);
// Enter edit mode
else
grid.CurrentCell.EnterEdit();
}
// If the end of the current row is reached
else if(currentRowSortedIndex < (sortedDataRowList.Count – 1))
{
// If the current cell is read only, go to first cell of the next row and continue
if(grid.CurrentCell.ReadOnly)
grid.CurrentCell =
sortedDataRowList[currentRowSortedIndex + 1].Cells[grid.Columns.GetColumnAtVisibleIndex(0).Index];
// Reset the selected row
grid.SelectedRows.Clear();
grid.SelectedRows.Add(sortedDataRowList[currentRowSortedIndex + 1]);
// Skip read only cells
if(grid.CurrentCell.ReadOnly)
MoveFirstCell(grid);
// Enter edit mode
else
grid.CurrentCell.EnterEdit();
}
// 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)
{
GridControl grid = (GridControl)sender;

// 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. Normally, we would check if the current row is a DataRow
// before retrieving it.
Xceed.Grid.DataRow currentDataRow = (Xceed.Grid.DataRow)grid.CurrentRow;

// Retrieve the current row’s sort index.
int currentRowSortedIndex = sortedDataRowList.IndexOf(currentDataRow);

// Retrieve the current column’s visible index.
int currentColVisibleIndex = grid.CurrentColumn.VisibleIndex;

// 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
grid.CurrentCell =
currentDataRow.Cells[grid.Columns.GetColumnAtVisibleIndex(currentColVisibleIndex + 1).Index];
// Skip read only cells
if(grid.CurrentCell.ReadOnly)
MoveNextCell(sender, e);
// Enter edit mode
else
grid.CurrentCell.EnterEdit();
}
// 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
grid.CurrentCell =
sortedDataRowList[currentRowSortedIndex + 1].Cells[grid.Columns.GetColumnAtVisibleIndex(0).Index];
// Reset the selected row
grid.SelectedRows.Clear();
grid.SelectedRows.Add(sortedDataRowList[currentRowSortedIndex + 1]);
// Skip read only cells
if(grid.CurrentCell.ReadOnly)
MoveNextCell(sender, e);
// Enter edit mode
else
grid.CurrentCell.EnterEdit();
}
// If the end of the grid is reached, go to the next control in the tab order
else
{
grid.FindForm().SelectNextControl(grid.Parent, true, true, false, true);
}
}
else if(e.KeyData == (Keys.Shift | Keys.Tab))
{
// Retrieve the list of the DataRows as they are sorted in the grid.
ReadOnlyDataRowList sortedDataRowList = grid.GetSortedDataRows(true);

// Retrieve the current row. Normally, we would check if the current row is a DataRow
// before retrieving it.
Xceed.Grid.DataRow currentDataRow = (Xceed.Grid.DataRow)grid.CurrentRow;

// Retrieve the current row’s sort index.
int currentRowSortedIndex = sortedDataRowList.IndexOf(currentDataRow);

// Retrieve the current column’s visible index.
int currentColVisibleIndex = grid.CurrentColumn.VisibleIndex;

// 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 beginning of a row
if (currentColVisibleIndex > 0)
{
// Move back one cell in the current row
grid.CurrentCell =
currentDataRow.Cells[grid.Columns.GetColumnAtVisibleIndex(currentColVisible

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