Get row in datagrid

后端 未结 2 1934
野性不改
野性不改 2020-12-06 13:59

I tried to get row like this :

DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock cellContent = dataGrid.Columns         


        
相关标签:
2条回答
  • 2020-12-06 14:15

    This depends on how / when you are attempting to get this data. WPF is more geared towards accessing the data by the objects bound in the ItemsSource. So, if your ItemsSource is a List of MyObject, then the specific row will be of type MyObject instead of a pure DataRow.

    If you are accessing the data by means of clicking on it, you can do something like this:

    var currentItem = myDataGrid.SelectedItem as MyObject;
    

    Now, you have the current MyObject in it's originally intended form rather than picking at the grid.

    0 讨论(0)
  •   for(int row =0; row < dg_CountInventory.Rows.Count; row ++) //Loop through each row
        {
            //Provide the Column Index and row as in Loop
            TextBlock b = dg_CountInventory.Columns[1].GetCellContent(dg_CountInventory.Items[row ]) as TextBlock; 
        }
    

    dg_CountInventory is my Grid Name.This code will loop through all records present in Data-grid and Cell/Column Provided.#

    0 讨论(0)
提交回复
热议问题