iterating through rows of a datagrid

拟墨画扇 提交于 2019-12-06 07:43:41

Use a DataGridRow not a DataRow they are a different objects

foreach (DataGridRow drv in PGIPortfolio.Items)

However it is not clear what Items is in this context. Assuming that PGIPortfolio is the DataGridView then your loop should be written as

foreach (DataGridRow drv in PGIPortfolio.Rows)

EDIT I assumed that you was using the DataGridView control in WinForms, not the WPF DataGrid In this case then the correct approach is to use the ItemsSource property.
Please try this code....

    var itemsSource = PGIPortfolio.ItemsSource as IEnumerable;
    if (itemsSource != null)
    {
        foreach (var item in itemsSource)
        {
            var row = PGIPortfolio.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
            if (row != null) 
            {
               .....
            }

        }
    }

foreach(DataGridViewRow r in dataGridView1.Rows)

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