Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Problem with DataGrid Rows in WPF

Problem with DataGrid Rows in WPF

Scheduled Pinned Locked Moved WPF
wpfcsharpdatabasearchitecturehelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2970611
    wrote on last edited by
    #1

    I have a datagrid where I have two buttons on each row - one for moving a row up and one for moving a row down. Each button has a command for allowing the user to move the selected row in either direction. The problem that I am facing is that it not working. I think the problem that I may have is that the other controls (combo boxes) on the rows are bound to data sources through the MVVM model where I am manipulating the rows on the code behind of the XAML thinking this would be the logical place in which to do it. The code I have for one of the buttons is below: private void MoveRowDown(object sender, ExecutedRoutedEventArgs e) { int currentRowIndex = dg1.ItemContainerGenerator.IndexFromContainer(dg1.ItemContainerGenerator.ContainerFromItem(dg1.SelectedItem)); if (currentRowIndex >= 0) { this.GetRow(currentRowIndex + 1).IsSelected = true; } } private DataGridRow GetRow(int index) { DataGridRow row = (DataGridRow)dg1.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { dg1.UpdateLayout(); dg1.ScrollIntoView(selectedAttributes.Items[index]); row = (DataGridRow)dg1.ItemContainerGenerator.ContainerFromIndex(index); } return row; }

    M 1 Reply Last reply
    0
    • U User 2970611

      I have a datagrid where I have two buttons on each row - one for moving a row up and one for moving a row down. Each button has a command for allowing the user to move the selected row in either direction. The problem that I am facing is that it not working. I think the problem that I may have is that the other controls (combo boxes) on the rows are bound to data sources through the MVVM model where I am manipulating the rows on the code behind of the XAML thinking this would be the logical place in which to do it. The code I have for one of the buttons is below: private void MoveRowDown(object sender, ExecutedRoutedEventArgs e) { int currentRowIndex = dg1.ItemContainerGenerator.IndexFromContainer(dg1.ItemContainerGenerator.ContainerFromItem(dg1.SelectedItem)); if (currentRowIndex >= 0) { this.GetRow(currentRowIndex + 1).IsSelected = true; } } private DataGridRow GetRow(int index) { DataGridRow row = (DataGridRow)dg1.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { dg1.UpdateLayout(); dg1.ScrollIntoView(selectedAttributes.Items[index]); row = (DataGridRow)dg1.ItemContainerGenerator.ContainerFromIndex(index); } return row; }

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Instead of working with the grid you should work with the data. Add a sort field to the class used to populate the grid and manipulate the order in the VM.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups