Select to view content in your preferred language

Feature Data Grid - Export - Date and Coded Value Domain

3435
8
10-14-2012 10:59 AM
by Anonymous User
Not applicable
Original User: gregg.breton

Working with Feature Data Grid Export Function and not able to download two fields (Date Field and Coded Value Domain Field)

Darina/Dominque - Have you had this occur?
I did not define any columns to be excluded.


[ATTACH=CONFIG]18413[/ATTACH]
[ATTACH=CONFIG]18414[/ATTACH]
[ATTACH=CONFIG]18415[/ATTACH]

The code is in two parts and was submitted by Dominque and Darina Thank you 🙂
Previous Post

 

private void ExcelExport_Click(object sender, RoutedEventArgs e)
        {
            FeatureLayer fe = (FeatureLayer)Map.Layers["Samples"];
            RelatedRowsDataGrid.Export(fe.Graphics, null);

        }




public static class DataGridExtensionsFDG
    {
        public static void Export(this FeatureDataGrid dg, IEnumerable<Graphic> graphics, List<string> excludedColumns)
        {
            ExportDataGrid(dg, graphics, excludedColumns);
           
        }

        public static void ExportDataGrid(FeatureDataGrid dGrid, IEnumerable<Graphic> graphics, List<string> excludedColumns)
        {
            SaveFileDialog objSFD = new SaveFileDialog() { DefaultExt = "csv", Filter = "CSV Files (*.csv)|*.csv|Excel XML (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 };
            if (objSFD.ShowDialog() == true)
            {
                string strFormat = objSFD.SafeFileName.Substring(objSFD.SafeFileName.IndexOf('.') + 1).ToUpper();
                StringBuilder strBuilder = new StringBuilder();
                if (dGrid.ItemsSource == null) return;
                if (dGrid.HeadersVisibility == DataGridHeadersVisibility.Column || dGrid.HeadersVisibility == DataGridHeadersVisibility.All)
                {
                    var listFields = from col in dGrid.Columns
                                     where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString())
                                     select FormatField(col.Header.ToString(), strFormat);

                    BuildStringOfRow(strBuilder, listFields, strFormat);
                }
                foreach (Graphic graphic in graphics)
                {
                    var listValues = from col in dGrid.Columns
                                     where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString())
                                     select FormatField(GetValue(col, graphic), strFormat);

                    BuildStringOfRow(strBuilder, listValues, strFormat);
                }
                StreamWriter sw = new StreamWriter(objSFD.OpenFile());
                if (strFormat == "XML")
                {
                    //Let us write the headers for the Excel XML
                    sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
                    sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\">");
                    sw.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
                    sw.WriteLine("<Author>Arasu Elango</Author>");
                    sw.WriteLine("<Created>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</Created>");
                    sw.WriteLine("<LastSaved>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</LastSaved>");
                    sw.WriteLine("<Company>Atom8 IT Solutions (P) Ltd.,</Company>");
                    sw.WriteLine("<Version>12.00</Version>");
                    sw.WriteLine("</DocumentProperties>");
                    sw.WriteLine("<Worksheet ss:Name=\"Silverlight Export\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
                    sw.WriteLine("<Table>");
                }
                sw.Write(strBuilder.ToString());
                if (strFormat == "XML")
                {
                    sw.WriteLine("</Table>");
                    sw.WriteLine("</Worksheet>");
                    sw.WriteLine("</Workbook>");
                }
                sw.Close();
            }
        }

      
        //Check for null errors
        private static string GetValue(DataGridColumn col, Graphic graphic)
        {
            if (col is DataGridBoundColumn)
            {
                DataGridBoundColumn column = col as DataGridBoundColumn;
                if ((column.Binding != null) && (column.Binding.Path != null))
                {
                    string path = column.Binding.Path.Path;
                    if (!string.IsNullOrEmpty(path))
                    {
                        var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1");
                        //return graphic.Attributes[att].ToString();
                        return graphic.Attributes[att] == null ? string.Empty : graphic.Attributes[att].ToString();
                    }
                }
            }
            return string.Empty;
        }

        private static void BuildStringOfRow(StringBuilder strBuilder, IEnumerable<string> lstFields, string strFormat)
        {
            switch (strFormat)
            {
                case "XML":
                    strBuilder.AppendLine("<Row>");
                    strBuilder.AppendLine(String.Join("\r\n", lstFields.ToArray()));
                    strBuilder.AppendLine("</Row>");
                    break;
                case "CSV":
                    strBuilder.AppendLine(String.Join(",", lstFields.ToArray()));
                    break;
            }
        }

        private static string FormatField(string data, string format)
        {
            switch (format)
            {
                case "XML":
                    return String.Format("<Cell><Data ss:Type=\"String\">{0}</Data></Cell>", data);
                case "CSV":
                    return String.Format("\"{0}\"", data.Replace("\"", "\"\"\"").Replace("\n", "").Replace("\r", ""));
            }
            return data;
        }

        
        
    }
0 Kudos
8 Replies
DarinaTchountcheva
Frequent Contributor
Gregg,

The date and coded values domain are not exporting because they are inside templated columns. The date field is displayed via a DatePicker and the coded values domain is displayed via a ComboBox.

The GetValue method in the DataGridExtension class does not handle these.

I modified the GetValue method to deal with templated columns and the controls used in your scenario. I don't have a sample like this one set up, so I have not tested, and I haven't looked at the FeatureDataGrid ControlTemplate to confirm my assumptions are correct. I expect the code to not handle something right. But hope it will give you an idea of how to resolve the problem.

private static string GetValue(DataGridColumn col, Graphic graphic)
        {
            string path = string.Empty;
            
            if (col is DataGridBoundColumn)
            {
                DataGridBoundColumn column = col as DataGridBoundColumn;

                if ((column.Binding != null) && (column.Binding.Path != null))
                {
                    path = column.Binding.Path.Path;                    
                }
            }
            else if (col is DataGridTemplateColumn)
            {
                Control innerControl = ((DataGridTemplateColumn)col).CellEditingTemplate.LoadContent() as Control;
                if (innerControl != null)
                {
                    switch (innerControl.GetType().FullName)
                    {
                        case "System.Windows.Controls.ComboBox":
                            path = ((ComboBox)innerControl).GetBindingExpression(ComboBox.SelectedItemProperty).ParentBinding.Path.Path.ToString();
                            break;
                        case "System.Windows.Controls.DatePicker":
                            path = ((DatePicker)innerControl).GetBindingExpression(DatePicker.SelectedDateProperty).ParentBinding.Path.Path.ToString();
                            break;
                    }                    
                }
            }
            if (!string.IsNullOrEmpty(path))
            {
                var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1");
                return graphic.Attributes[att] == null ? string.Empty : graphic.Attributes[att].ToString();
            }
            return string.Empty;
        }


Good Luck!
0 Kudos
by Anonymous User
Not applicable
Original User: gregg.breton

Hello Darina,

Tested the code and still not able to retrieve the date and domain from inside the templated columns
No error messages, allows you to export the file but no date and domain attributes

[ATTACH=CONFIG]18738[/ATTACH]

It definitlely helps me understand why the records are not being downloaded with the other attributes -

Thank you for explanation 🙂
0 Kudos
DarinaTchountcheva
Frequent Contributor
Gregg,

I tested the code with a HyperlinkButton (added another "case") and a DataGrid, and it exports the hyperlink content to Excel.

So, it could be something little that needs to change.

Can you add a break point at  the line "if (innerControl.GetType().FullName)" and debug to see if innerControl is null? And if it is not what values you get for path and att?
0 Kudos
by Anonymous User
Not applicable
Original User: gregg.breton

I started with a breakpoint at GetValue and stepped through:

[ATTACH=CONFIG]18756[/ATTACH]

After this step is steps over and

[ATTACH=CONFIG]18757[/ATTACH]

steps into (!string.IsNullOrEmpty(path))

[ATTACH=CONFIG]18758[/ATTACH]

Will try again and let you know
0 Kudos
DarinaTchountcheva
Frequent Contributor
Gregg,

I didn't realize you are missing the coded values domain and date for all the records.

What version of the API are you using?
0 Kudos
by Anonymous User
Not applicable
Original User: gregg.breton

I am using 3.0.0.388

[ATTACH=CONFIG]18775[/ATTACH]

I believe that is the latest correct?
0 Kudos
DarinaTchountcheva
Frequent Contributor
Gregg,

I am not on version 3 yet.
I looked at the source code of the FeatureDataGrid for my and your version and the Date and Coded Values Domain are handled differently.

If I find some time to install 3 in the next few days, I will try to figure out why the dates and coded values domains are not coming through.
0 Kudos
by Anonymous User
Not applicable
Original User: gregg.breton

I believe there was a issue in 2.4 that did not honor the coded values and range domains that I set in the geodatabase. I was told to upgrade to 3.0. The editing now honors the domains and relationship class (1:M)

OK Thank you
0 Kudos