[C#] C#自定义的GridView操作类,简化GridView操作 →→→→→进入此内容的聊天室

来自 , 2020-12-22, 写在 C#, 查看 136 次.
URL http://www.code666.cn/view/3e9f0fc9
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. using System.Data;
  6. using System.Collections;
  7. using System.Reflection;
  8.  
  9. namespace DotNet.Utilities
  10. {
  11.     public class GridViewHelper
  12.     {
  13.         #region 私有方法
  14.         /// <summary>
  15.         /// 截取内容长度
  16.         /// </summary>
  17.         /// <param name="o_Str">原字符串</param>
  18.         /// <param name="len">截取长度</param>
  19.         /// <returns>截取后字符串</returns>
  20.         private static string GetStrPartly(string o_Str, int len)
  21.         {
  22.             if (len == 0)
  23.             {
  24.                 return o_Str;
  25.             }
  26.             else
  27.             {
  28.                 if (o_Str.Length > len)
  29.                 {
  30.                     return o_Str.Substring(0, len) + "..";
  31.                 }
  32.                 else
  33.                 {
  34.                     return o_Str;
  35.                 }
  36.             }
  37.         }
  38.  
  39.         /// <summary>
  40.         /// 获取单元格内容
  41.         /// </summary>
  42.         /// <param name="cell">TableCell</param>
  43.         /// <returns>内容</returns>
  44.         private static string GetCellText(TableCell cell)
  45.         {
  46.             string text = cell.Text;
  47.             if (!string.IsNullOrEmpty(text))
  48.             {
  49.                 return text;
  50.             }
  51.             foreach (Control control in cell.Controls)
  52.             {
  53.                 if (control != null && control is IButtonControl)
  54.                 {
  55.                     IButtonControl btn = control as IButtonControl;
  56.                     text = btn.Text.Replace("\r\n", "").Trim();
  57.                     break;
  58.                 }
  59.                 if (control != null && control is ITextControl)
  60.                 {
  61.                     LiteralControl lc = control as LiteralControl;
  62.                     if (lc != null)
  63.                     {
  64.                         continue;
  65.                     }
  66.                     ITextControl l = control as ITextControl;
  67.                     text = l.Text.Replace("\r\n", "").Trim();
  68.                     break;
  69.                 }
  70.             }
  71.             return text;
  72.         }
  73.  
  74.         /// <summary>
  75.         /// 设置单元格内容
  76.         /// </summary>
  77.         /// <param name="cell">TableCell</param>
  78.         /// <param name="maxLen">最大长度</param>
  79.         private static void SetCellText(TableCell cell, int maxLen)
  80.         {
  81.             string text = cell.Text;
  82.             if (!string.IsNullOrEmpty(text))
  83.             {
  84.                 cell.Text = GetStrPartly(text, maxLen);
  85.             }
  86.             foreach (Control control in cell.Controls)
  87.             {
  88.                 if (control != null && control is IButtonControl)
  89.                 {
  90.                     IButtonControl btn = control as IButtonControl;
  91.                     text = btn.Text.Replace("\r\n", "").Trim();
  92.                     btn.Text = GetStrPartly(text, maxLen);
  93.                     break;
  94.                 }
  95.                 if (control != null && control is ITextControl)
  96.                 {
  97.                     LiteralControl lc = control as LiteralControl;
  98.                     if (lc != null)
  99.                     {
  100.                         continue;
  101.                     }
  102.                     ITextControl l = control as ITextControl;
  103.                     text = l.Text.Replace("\r\n", "").Trim();
  104.                     if (l is DataBoundLiteralControl)
  105.                     {
  106.                         cell.Text = GetStrPartly(text, maxLen);
  107.                         break;
  108.                     }
  109.                     else
  110.                     {
  111.                         l.Text = GetStrPartly(text, maxLen);
  112.                         break;
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.         #endregion
  118.  
  119.         #region 公有方法
  120.         /// <summary>
  121.         /// 从GridView的数据生成DataTable
  122.         /// </summary>
  123.         /// <param name="gv">GridView对象</param>
  124.         public static DataTable GridView2DataTable(GridView gv)
  125.         {
  126.             DataTable table = new DataTable();
  127.             int rowIndex = 0;
  128.             List<string> cols = new List<string>();
  129.             if (!gv.ShowHeader && gv.Columns.Count == 0)
  130.             {
  131.                 return table;
  132.             }
  133.             GridViewRow headerRow = gv.HeaderRow;
  134.             int columnCount = headerRow.Cells.Count;
  135.             for (int i = 0; i < columnCount; i++)
  136.             {
  137.                 string text = GetCellText(headerRow.Cells[i]);
  138.                 cols.Add(text);
  139.             }
  140.             foreach (GridViewRow r in gv.Rows)
  141.             {
  142.                 if (r.RowType == DataControlRowType.DataRow)
  143.                 {
  144.                     DataRow row = table.NewRow();
  145.                     int j = 0;
  146.                     for (int i = 0; i < columnCount; i++)
  147.                     {
  148.                         string text = GetCellText(r.Cells[i]);
  149.                         if (!String.IsNullOrEmpty(text))
  150.                         {
  151.                             if (rowIndex == 0)
  152.                             {
  153.                                 string columnName = cols[i];
  154.                                 if (String.IsNullOrEmpty(columnName))
  155.                                 {
  156.                                     continue;
  157.                                 }
  158.                                 if (table.Columns.Contains(columnName))
  159.                                 {
  160.                                     continue;
  161.                                 }
  162.                                 DataColumn dc = table.Columns.Add();
  163.                                 dc.ColumnName = columnName;
  164.                                 dc.DataType = typeof(string);
  165.                             }
  166.                             row[j] = text;
  167.                             j++;
  168.                         }
  169.                     }
  170.                     rowIndex++;
  171.                     table.Rows.Add(row);
  172.                 }
  173.             }
  174.             return table;
  175.         }
  176.  
  177.         /// <summary>
  178.         /// 将集合类转换成DataTable
  179.         /// </summary>
  180.         /// <param name="list">集合</param>
  181.         public static DataTable ToDataTable(IList list)
  182.         {
  183.             DataTable result = new DataTable();
  184.             if (list.Count > 0)
  185.             {
  186.                 PropertyInfo[] propertys = list[0].GetType().GetProperties();
  187.                 foreach (PropertyInfo pi in propertys)
  188.                 {
  189.                     result.Columns.Add(pi.Name, pi.PropertyType);
  190.                 }
  191.  
  192.                 for (int i = 0; i < list.Count; i++)
  193.                 {
  194.                     ArrayList tempList = new ArrayList();
  195.                     foreach (PropertyInfo pi in propertys)
  196.                     {
  197.                         object obj = pi.GetValue(list[i], null);
  198.                         tempList.Add(obj);
  199.                     }
  200.                     object[] array = tempList.ToArray();
  201.                     result.LoadDataRow(array, true);
  202.                 }
  203.             }
  204.             return result;
  205.         }
  206.  
  207.         /// <summary>
  208.         /// 将泛型集合类转换成DataTable
  209.         /// </summary>
  210.         /// <typeparam name="T">集合项类型</typeparam>
  211.         /// <param name="list">集合</param>
  212.         /// <param name="propertyName">需要返回的列的列名</param>
  213.         /// <returns>数据集(表)</returns>
  214.         public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
  215.         {
  216.             List<string> propertyNameList = new List<string>();
  217.             if (propertyName != null) propertyNameList.AddRange(propertyName);
  218.  
  219.             DataTable result = new DataTable();
  220.             if (list.Count > 0)
  221.             {
  222.                 PropertyInfo[] propertys = list[0].GetType().GetProperties();
  223.                 foreach (PropertyInfo pi in propertys)
  224.                 {
  225.                     if (propertyNameList.Count == 0)
  226.                     {
  227.                         result.Columns.Add(pi.Name, pi.PropertyType);
  228.                     }
  229.                     else
  230.                     {
  231.                         if (propertyNameList.Contains(pi.Name)) result.Columns.Add(pi.Name, pi.PropertyType);
  232.                     }
  233.                 }
  234.  
  235.                 for (int i = 0; i < list.Count; i++)
  236.                 {
  237.                     ArrayList tempList = new ArrayList();
  238.                     foreach (PropertyInfo pi in propertys)
  239.                     {
  240.                         if (propertyNameList.Count == 0)
  241.                         {
  242.                             object obj = pi.GetValue(list[i], null);
  243.                             tempList.Add(obj);
  244.                         }
  245.                         else
  246.                         {
  247.                             if (propertyNameList.Contains(pi.Name))
  248.                             {
  249.                                 object obj = pi.GetValue(list[i], null);
  250.                                 tempList.Add(obj);
  251.                             }
  252.                         }
  253.                     }
  254.                     object[] array = tempList.ToArray();
  255.                     result.LoadDataRow(array, true);
  256.                 }
  257.             }
  258.             return result;
  259.         }
  260.         #endregion
  261.     }
  262. }
  263. //csharp/8579

回复 "C#自定义的GridView操作类,简化GridView操作"

这儿你可以回复上面这条便签

captcha