检查数据行中是否存在具有给定名称的列

POI*_*OIR 5 c# asp.net data-binding listview datarow

如何检查填充列表视图的结果中是否存在列?列表视图是从存储过程填充的.

这是我尝试但没有成功:

<%#  Container.DataItem.GetType().GetProperty("Phone")==null?"phone is null":"we have phone property" #>
Run Code Online (Sandbox Code Playgroud)

或者我应该使用e而不是Container.DataItem

Tim*_*ter 12

首先,我会使用代码隐藏,如果它变得复杂(我几乎总是使用它).在这里,我将使用为每个项触发的ListView ItemDataBound事件:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        // assuming you have an ItemTemplate with a label where you want to show this
        Label lblInfo = (Label) e.Item.FindControl("LblInfo");
        DataRowView rowView = (DataRowView)e.Item.DataItem;
        if (rowView.Row.Table.Columns.Contains("Phone"))
        {
            lblInfo.Text = "we have the phone property";
        }
        else
        {
            lblInfo.Text = "no phone available";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这使得代码更具可读性,可维护性,可调试性和类型安全性.