Get the DefaultView DataRowView from a DataRow

情到浓时终转凉″ 提交于 2019-12-10 03:45:43

问题


Here's the situation: I need to bind a WPF FixedPage against a DataRow. Bindings don't work against DataRows; they work against DataRowViews. I need to do this in the most generic way possible, as I know nothing about and have no control over what is in the DataRow.

What I need is to be able to get a DataRowView for a given DataRow. I can't use the Find() method on the DefaultView because that takes a key, and there is no guarantee the table will have a primary key set.

Does anybody have a suggestion as to the best way to go around this?


回答1:


Not Exactly a sexy piece of code but their doesn't seem to be an automated way to find the row without just looping the table.

        DataRowView newRowView = null;
        foreach (DataRowView tempRowView in myDataTable.DefaultView)
        {
            if (tempRowView.Row == rowToMatch)
                newRowView = tempRowView;
        }
        if (newRow != null)
            UseNewRowView(newRowView);
        else
            HandleRowNotFound();



回答2:


row.Table.DefaultView[row.Table.Rows.IndexOf(row)]

This is an okay answer. But if you find yourself in this situation, you should consider learning more about DataViews and how they are used, then refactor your code to be view-centric rather than table-centric.



来源:https://stackoverflow.com/questions/32460/get-the-defaultview-datarowview-from-a-datarow

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