Exemple #1
0
        private void PopulateRows(XmlNode rows)
        {
            object[] rowValues = new object[_DataTable.Columns.Count];

            bool bSkipMsg = false;

            foreach (XmlNode rNode in rows.ChildNodes)
            {
                if (rNode.Name != "Row")
                {
                    continue;
                }
                int  col       = 0;
                bool bBuiltRow = false;                 // if all columns will be null we won't add the row
                foreach (DataColumn dc in _DataTable.Columns)
                {
                    XmlNode dNode = _Draw.GetNamedChildNode(rNode, dc.ColumnName);
                    if (dNode != null)
                    {
                        bBuiltRow = true;
                    }

                    if (dNode == null)
                    {
                        rowValues[col] = null;
                    }
                    else if (dc.DataType == typeof(string))
                    {
                        rowValues[col] = dNode.InnerText;
                    }
                    else
                    {
                        object box;
                        try
                        {
                            if (dc.DataType == typeof(int))
                            {
                                box = Convert.ToInt32(dNode.InnerText, NumberFormatInfo.InvariantInfo);
                            }
                            else if (dc.DataType == typeof(decimal))
                            {
                                box = Convert.ToDecimal(dNode.InnerText, NumberFormatInfo.InvariantInfo);
                            }
                            else if (dc.DataType == typeof(long))
                            {
                                box = Convert.ToInt64(dNode.InnerText, NumberFormatInfo.InvariantInfo);
                            }
                            else if (DesignerUtility.IsNumeric(dc.DataType))    // catch all numeric
                            {
                                box = Convert.ToDouble(dNode.InnerText, NumberFormatInfo.InvariantInfo);
                            }
                            else if (dc.DataType == typeof(DateTime))
                            {
                                box = Convert.ToDateTime(dNode.InnerText,
                                                         System.Globalization.DateTimeFormatInfo.InvariantInfo);
                            }
                            else
                            {
                                box = dNode.InnerText;
                            }
                            rowValues[col] = box;
                        }
                        catch (Exception e)
                        {
                            if (!bSkipMsg)
                            {
                                if (MessageBox.Show(string.Format("Unable to convert {1} to {0}: {2}",
                                                                  dc.DataType.ToString(), dNode.InnerText, e.Message) + Environment.NewLine + "Do you want to see any more errors?",
                                                    "Error Reading Data Rows", MessageBoxButtons.YesNo) == DialogResult.No)
                                {
                                    bSkipMsg = true;
                                }
                            }
                            rowValues[col] = dNode.InnerText;
                        }
                    }
                    col++;
                }
                if (bBuiltRow)
                {
                    _DataTable.Rows.Add(rowValues);
                }
            }
        }