Get row in datagrid

天涯浪子 提交于 2019-12-17 16:38:04

问题


I tried to get row like this :

DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;

But I only got null. Is there another solution? What am I doing wrong?

I want to get data from my cells. My cells are checkboxes.


回答1:


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.




回答2:


  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.#



来源:https://stackoverflow.com/questions/8464704/get-row-in-datagrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!