Home Forums WPF controls Xceed DataGrid for WPF Change view on the fly in code?

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

    How do you change from Table view to Card view or Compact Card view, via the codebehind?

    Imported from legacy forums. Posted by yahgrid27 (had 8548 views)

    Xceed Support
    Member
    Post count: 5658

    Set the DataGridControl’s View property to the view you want.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 466 views)

    User (Old forums)
    Member
    Post count: 23064

    Just curious….the Cardview doesn’t support a Children? Is that correct?

    Imported from legacy forums. Posted by David (had 694 views)

    Xceed Support
    Member
    Post count: 5658

    If by children you are referring to master/detail, then no. Master/Detail is currently only supported by the TableView

    Imported from legacy forums. Posted by Jenny [Xceed] (had 451 views)

    User (Old forums)
    Member
    Post count: 23064

    This is the way I am changing the View. I saw similar code in one of the sample apps.

    private void ComboBox_Grid_Type_Selector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    if (ComboBox_Grid_Type_Selector.SelectedIndex == 0)
    {
    TableView tableView = new TableView();
    dataGridControl_Site_ID.View = tableView;
    }

    if (ComboBox_Grid_Type_Selector.SelectedIndex == 1)
    {
    CompactCardView cardView = new CompactCardView();
    dataGridControl_Site_ID.View = cardView;
    }
    }

    I make no claims as to if this is the best way to do things….but it does work.
    If you have a master/detail grid….you could run into problems going from a table view to a card view.

    Imported from legacy forums. Posted by David (had 842 views)

    User (Old forums)
    Member
    Post count: 23064

    I am curious…is there a method or something that if you are currently on a childrow to place the DataGrid’s focus on that child’s parent row?
    If not that would be a great thing to add….2 methods. 1 method to go up 1 level and another method to go down 1 level.

    Imported from legacy forums. Posted by David (had 555 views)

    Xceed Support
    Member
    Post count: 5658

    You can retrieve an item’s DataGridContext and check its ParentItem property to get the parent item. A child item can be accessed through the DataGridContext’s Items collection.

    (Note: Please start a new thread if you have a question not related to the current thread’s subject 🙂 )

    Imported from legacy forums. Posted by Jenny [Xceed] (had 335 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jenny,

    You wouldn’t have a small code snippet of that would you?

    I asked here because the code above won’t work if you have detail rows. If you are focused on a child row and change the view it throws a exception. It would be nice if it automatically put you to the parent row when you did change a view. 🙂

    Imported from legacy forums. Posted by David (had 849 views)

    Xceed Support
    Member
    Post count: 5658

    The reason you are getting the exception is because only TableView supports master/detail, that said, you could do something like the following:

    <code>
    DataGridContext currentContext = this.myGrid.CurrentContext;
    System.Data.DataRow parentItem = currentContext.ParentItem;

    bool masterItemFound = false;

    while( !masterItemFound )
    {
    if( currentContext.ParentItem != null )
    {
    parentItem = currentContext.ParentItem;
    currentContext = currentContext.ParentDataGridContext;
    }
    else
    {
    masterItemFound = true;
    }
    }

    this.myGrid.CurrentItem = parentItem;

    Disclaimer: I have not tested this code so it is quite possible that it will need to be tweaked.

    // if we no longer have a parentDataGridContext, then we can assume that
    // we are at the master grid level.
    System.Data.DataRow masterRow = lastContext

    </code>

    Imported from legacy forums. Posted by Jenny [Xceed] (had 308 views)

    User (Old forums)
    Member
    Post count: 23064

    Ahh thanks Jenny!
    Its working now.

    If anyone is interested here is the completed code:

    private void ComboBox_Grid_Type_Selector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    // change to tableview
    if (ComboBox_Grid_Type_Selector.SelectedIndex == 0)
    {
    TableView tableView = new TableView();
    dataGridControl_Site_ID.View = tableView;
    }

    //change to card view
    if (ComboBox_Grid_Type_Selector.SelectedIndex == 1)
    {
    DataGridContext currentContext = dataGridControl_Site_ID.CurrentContext;
    System.Data.DataRow parentItem = (System.Data.DataRow)currentContext.ParentItem;

    bool masterItemFound = false;

    while( !masterItemFound )
    {
    if( currentContext.ParentItem != null )
    {
    parentItem = (System.Data.DataRow)currentContext.ParentItem;
    currentContext = currentContext.ParentDataGridContext;
    }
    else
    {
    masterItemFound = true;
    }
    }

    dataGridControl_Site_ID.CurrentItem = parentItem;
    CompactCardView cardView = new CompactCardView();
    dataGridControl_Site_ID.View = cardView;
    }
    }

    Imported from legacy forums. Posted by David (had 701 views)

    Xceed Support
    Member
    Post count: 5658

    No problem. Glad I could help 🙂

    Imported from legacy forums. Posted by Jenny [Xceed] (had 357 views)

    User (Old forums)
    Member
    Post count: 23064

    I found a bug in my previous code…..this following code fixes it.

    private void ComboBox_Grid_Type_Selector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    if (ComboBox_Grid_Type_Selector.SelectedIndex == 0)
    {
    TableView tableView = new TableView();
    dataGridControl_Site_ID.View = tableView;
    }

    if ((ComboBox_Grid_Type_Selector.SelectedIndex == 1) || (ComboBox_Grid_Type_Selector.SelectedIndex == 2))
    {
    dataGridControl_Site_ID.CurrentItem = Get_Parent_Row_dataGridControl_Site_ID();

    if (ComboBox_Grid_Type_Selector.SelectedIndex == 1)
    {
    CompactCardView ccardView = new CompactCardView();
    dataGridControl_Site_ID.View = ccardView;
    }

    if (ComboBox_Grid_Type_Selector.SelectedIndex == 2)
    {
    CardView cardView = new CardView();
    dataGridControl_Site_ID.View = cardView;
    }

    }

    ComboBox_Theme_Selector_SelectionChanged(sender, e);
    }

    public System.Data.DataRow Get_Parent_Row_dataGridControl_Site_ID()
    {
    // Returns the DataRow that is the parent
    DataGridContext currentContext = dataGridControl_Site_ID.CurrentContext;
    System.Data.DataRow parentItem = (System.Data.DataRow)currentContext.ParentItem;

    if (parentItem == null)
    {
    return ((System.Data.DataRow)dataGridControl_Site_ID.SelectedItem);
    }

    bool masterItemFound = false;

    while( !masterItemFound )
    {
    if( currentContext.ParentItem != null )
    {
    parentItem = (System.Data.DataRow)currentContext.ParentItem;
    currentContext = currentContext.ParentDataGridContext;
    }
    else
    {
    masterItemFound = true;
    }
    }

    return (parentItem);

    }

    Imported from legacy forums. Posted by David (had 10129 views)

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