Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="html"></param>
        /// <param name="dt"></param>
        /// <param name="dr">if null the row will be created and added</param>
        public static void sb_fillDataRowWithHtmlControls(string html, Model.htmlTable dt, DataRow dr)
        {
            if (string.IsNullOrEmpty(html))
            {
                throw new Exception("html is null or empty");
            }
            if (dt == null)
            {
                throw new Exception("dt");
            }
            bool addRow = false;

            if (dr == null)
            {
                dr = dt.NewRow(); addRow = true;
            }


            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);
            int    i;
            string controlChecked;

            Model.htmlColumn htmlCol;
            for (i = 0; i <= dt.Columns.Count - 1; i++)
            {
                if (!(dt.Columns[i] is Model.htmlColumn))
                {
                    continue;
                }
                htmlCol = (Model.htmlColumn)dt.Columns[i];

                if (!string.IsNullOrEmpty(htmlCol.prp_controlId))
                {
                    if (htmlCol.prp_controlType == Model.htmlColumn.enum_controlType.inputCheckBox)
                    {
                        controlChecked = fnc_getElementValue(html, htmlCol.prp_controlId, htmlCol.prp_controlType);
                        if (string.IsNullOrEmpty(controlChecked) || controlChecked.ToLower() != "checked")
                        {
                            dr[i] = false;
                        }
                        else
                        {
                            dr[i] = true;
                        }
                    }
                    else
                    {
                        var convertor = System.ComponentModel.TypeDescriptor.GetConverter(dt.Columns[i].DataType);
                        var val       = fnc_getElementValue(html, htmlCol.prp_controlId, htmlCol.prp_controlType);
                        dr[i] = Functions.IsNull(val) ? DBNull.Value : convertor.ConvertFromString(null, CultureInfo.InvariantCulture, val);
                    }
                }
            }
            if (addRow)
            {
                dt.Rows.Add(dr);
            }
        }
Esempio n. 2
0
 public static void sb_fillDataRowWithHtmlControls(string html, Model.htmlTable dt)
 {
     sb_fillDataRowWithHtmlControls(html, dt, null);
 }
Esempio n. 3
0
        public static void sb_fillDatatableWithHtmlTableId(string html, string tableId, Model.htmlTable dt)
        {
            if (string.IsNullOrEmpty(html))
            {
                throw new Exception("html is null or empty");
            }
            if (string.IsNullOrEmpty(tableId))
            {
                throw new Exception("tableId is null or empty");
            }


            if (dt == null)
            {
                throw new Exception("dt is null");
            }
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);
            var table = document.GetElementbyId(tableId);

            HtmlAgilityPack.HtmlNodeCollection rows;
            if (table == null)
            {
                return;
            }
            HtmlAgilityPack.HtmlNode tbody = table.SelectSingleNode("tbody");
            if (tbody != null)
            {
                rows = tbody.SelectNodes("tr");
            }
            else
            {
                rows = table.SelectNodes("tr");
            }
            int     i, j;
            DataRow dr;
            string  cellContent;
            int     skipRowCountAtBegining = dt.prp_skipRowTop;
            int     skipRowCountAtEnd      = dt.prp_skipRowBottom;

            Model.htmlColumn column;
            for (i = skipRowCountAtBegining; i <= rows.Count - 1 - skipRowCountAtEnd; i++)
            {
                if (dt.prp_skipRowIndecies != null && dt.prp_skipRowIndecies.Any(o => o == i))
                {
                    continue;
                }
                var cells = rows[i].SelectNodes("td");
                dr = dt.NewRow();
                for (j = 0; j <= dt.Columns.Count - 1; j++)
                {
                    column = (Model.htmlColumn)dt.Columns[j];
                    if (!column.prp_tableColIndex.HasValue)
                    {
                        continue;
                    }
                    if (column.prp_tableColIndex.Value >= cells.Count)
                    {
                        continue;
                    }
                    cellContent           = cells[j].InnerText.Replace("&nbsp;", "").TrimEnd().TrimStart();
                    dr[column.ColumnName] = string.IsNullOrEmpty(cellContent) ? Convert.DBNull : cellContent;
                }

                dt.Rows.Add(dr);
            }
        }