Row[Index].Cell[Index].Value from DataGridView

Aug 31, 2009 at 9:42 PM
Edited Aug 31, 2009 at 9:43 PM

I would like to know if there is a way to replicate the following C# code from a Windows Forms application code behind in the WPF DataGridView.  The C# from Windows Forms is:

private void dgvGroup_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
      {
         FormMain.gsNoticeGroupID = dgvGroup.Rows[e.RowIndex].Cells[1].Value.ToString();
         this.Close();
      }

Essentially, I have a hidden cell containing a primary key value that I would like to set in a global variable when a user double-clicks on a row in my datagrid.  I'm having trouble finding a straightforward replication of the Row index and Cell index in WPF.

 

Sep 17, 2009 at 4:37 AM

there isn't any Rows property in datagridcolumn. Use the following function to get Rows collection. 

 

        public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
        {
            var itemsSource = grid.ItemsSource as IEnumerable;
            if (null == itemsSource) yield return null;
            foreach (var item in itemsSource)
            {
                var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
                if (null != row) yield return row;
            }
        }

        public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)

        {

            var itemsSource = grid.ItemsSource as IEnumerable;

            if (null == itemsSource) yield return null;

            foreach (var item in itemsSource)

            {

                var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;

                if (null != row) yield return row;

            }

        }