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

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13554 |

    I am trying to bind the Xceed Grid to a Data Table and show only certain columns from the Data Table. The only way I have found to do this is to bind the Data Table to the grid and hide the columns that I do not want to see.

    I am trying to do this in combination with the code provided for Xceed Grid FAQ “How do I stay in edit mode when navigating through the grid?” which often tries to set the CurrentCell property to one of the hidden cells, and then throws an exception saying that a hidden cell cannot be the Current Cell.

    Does anyone know of a better way to achieve this funcitonality? Does the Xceed Grid support anything like the default MS DataGrid’s Table Styles? Is there a function (or easy method) for finding the Next Visible Cell?

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

    User (Old forums)
    Member
    Post count: 23064

    Try this:
    – catch the <b>CurrentCellChanged</b> event of the gridcontrol
    – in the event handler, call the <b>EnterEdit</b> method of the cell
    Here’s some (untested) code:<code>myGridControl.CurrentCellChanged += new EventHandler(myGridControl_CurrentCellChanged);
    private void myGridControl_CurrentCellChanged(object sender, EventArgs e)
    {
    Cell cell = myGridControl.CurrentCell;
    if (cell != null && !cell.ReadOnly) cell.EnterEdit();
    }</code>

    Imported from legacy forums. Posted by Tommy (had 146 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for the input, but the above code really does not solve the problem. I am getting a SystemArgumentException when I try to set a non-Visible cell to the current cell. (ex.Message = The specified cell cannot be the current cell because it is not visible.) This Exception occurs before the CurrentCellChanged Event occurs.

    As I said above, I really only have a problem when I want to bind to a Data Table and not show (hide) some of the columns. The only solution that I have found so far is to actually remove the columns that are not displayed, but sometimes they contain data that is essential to have (just not necessarily essential to show to the user).

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

    User (Old forums)
    Member
    Post count: 23064

    I don’t think you have to set the CurrentCell yourself. The CurrentCellChanged event should be called automatically when navigating the grid.

    Imported from legacy forums. Posted by Tommy (had 162 views)

    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)

    User (Old forums)
    Member
    Post count: 23064

    Instead of setting the <b>CurrentCell</b> property while looping through the rows and columns, just use a variable <i>currentCell</i>.
    I see you skip cells that are read-only: add an extra check to also skip the cells that can not be the current cell: <code>if (<i>currentCell</i>.<b>ReadOnly</b> || !<i>currentCell</i>.<b>CanBeCurrent</b>) skip…</code>
    After you have found the correct cell, set the <b>CurrentCell</b> property of the gridcontrol to the variable <i>currentCell</i>.

    Imported from legacy forums. Posted by Tommy (had 189 views)

    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)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.