用此链接的代码,可以导出到excel   https://blog.csdn.net/longshengguoji/article/details/9664209但对于绑定关联表字段的栏位,没有处理到,该处何调整代码?谢谢。 如: <c1ataGridTextColumn Header="产品编号" Binding="{Binding table1.table2.Code}"/>
以上绑定关联表table2的字段Code,便导出是空。

解决方案 »

  1.   

    感觉是以下代码某个地方需要做一下调整,请大侠帮看一下。
     if (col is DataGridTemplateColumn)
                                {
                                    //This is a template column... let us see the underlying dependency object
                                    DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent();
                                    FrameworkElement oFE = (FrameworkElement)objDO;
                                    FieldInfo oFI = oFE.GetType().GetField("TextProperty");
                                    if (oFI != null)
                                    {
                                        if (oFI.GetValue(null) != null)
                                        {
                                            if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)) != null)
                                                objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)).ParentBinding;
                                        }
                                    }
                                }
                                if (objBinding != null)
                                {
                                    if (objBinding.Path.Path != "")
                                    {
                                        PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path);
                                        if (pi != null) strValue = pi.GetValue(data, null).ToString();
                                    }