在ASP.NET應用程序開發中,GirdView的應用還是比較廣泛的。一般的數據展現時,都會使用到GridView。今天大家分享一個Grid View的幫助類,希望可以給學習ASP.NET開發的朋友一點幫助。
實現代碼
- 輔助類提供的接口如下:
/// <summary>
/// 從GridView的數據生成DataTable
/// </summary>
/// <param name="gv">GridView對象</param>
public static DataTable GridView2DataTable(GridView gv)
/// <summary>
/// 將集合類轉換成DataTable
/// </summary>
/// <param name="list">集合</param>
public static DataTable ToDataTable(IList list)
/// <summary>
/// 將泛型集合類轉換成DataTable
/// </summary>
/// <typeparam name="T">集合項類型</typeparam>
/// <param name="list">集合</param>
/// <param name="propertyName">需要返回的列的列名</param>
/// <returns>數據集(表)</returns>
public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
在日常開發過程中,可以使用上面提供的方法,將GirdView綁定的數據轉換爲DataTable了。而且,接口提供了Ilist集合類轉換爲DataTable的方法,可以將數據集合轉換爲DataTable綁定到GirdView上。
附上源碼
/// <summary>
/// GridView和DataGridView常用的操作輔助類
/// </summary>
public class GridViewHelper
{
/// <summary>
/// 從GridView的數據生成DataTable
/// </summary>
/// <param name="gv">GridView對象</param>
public static DataTable GridView2DataTable(GridView gv)
{
DataTable table = new DataTable();
int rowIndex = 0;
List<string> cols = new List<string>();
if (!gv.ShowHeader && gv.Columns.Count == 0)
{
return table;
}
GridViewRow headerRow = gv.HeaderRow;
int columnCount = headerRow.Cells.Count;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(headerRow.Cells[i]);
cols.Add(text);
}
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
int j = 0;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(r.Cells[i]);
if (!String.IsNullOrEmpty(text))
{
if (rowIndex == 0)
{
string columnName = cols[i];
if (String.IsNullOrEmpty(columnName))
{
continue;
}
if (table.Columns.Contains(columnName))
{
continue;
}
DataColumn dc = table.Columns.Add();
dc.ColumnName = columnName;
dc.DataType = typeof(string);
}
row[j] = text;
j++;
}
}
rowIndex++;
table.Rows.Add(row);
}
}
return table;
}
/// <summary>
/// 獲取DataGrid控件中選擇的項目的ID字符串(要求DataGrid設置datakeyfield="ID")
/// </summary>
/// <param name="dg">DataGrid控件</param>
/// <returns>如果沒有選擇, 那麼返回爲空字符串, 否則返回逗號分隔的ID字符串(如1,2,3)</returns>
public static string GetDatagridItems(DataGrid dg)
{
return GetDatagridItems(dg, false);
}
/// <summary>
/// 獲取DataGrid控件中選擇的項目的ID字符串(要求DataGrid設置datakeyfield="ID")
/// </summary>
/// <param name="dg">DataGrid控件</param>
/// <param name="UseSemicolon">是否使用''將ID括起</param>
/// <returns>如果沒有選擇, 那麼返回爲空字符串, 否則返回逗號分隔的ID字符串(如1,2,3)</returns>
public static string GetDatagridItems(DataGrid dg, bool UseSemicolon)
{
string idstring = string.Empty;
foreach (DataGridItem item in dg.Items)
{
string key = dg.DataKeys[item.ItemIndex].ToString();
bool isSelected = ((CheckBox)item.FindControl("cbxDelete")).Checked;
if (isSelected)
{
if (UseSemicolon)
{
idstring += "'" + key + "',";
}
else
{
idstring += key + ",";
}
}
}
idstring = idstring.Trim(',');
return idstring;
}
/// <summary>
/// 設置下拉列表的值
/// </summary>
/// <param name="control">下拉列表控件</param>
/// <param name="strValue">待顯示的值</param>
public static void SetDropDownListItem(DropDownList control, string strValue)
{
if (!string.IsNullOrEmpty(strValue))
{
//control.SelectedIndex = -1;
control.ClearSelection();
ListItem item = control.Items.FindByValue(strValue);
if (item != null)
{
control.SelectedValue = item.Value;
}
}
}
#region 私有方法
/// <summary>
/// 截取內容長度
/// </summary>
/// <param name="o_Str">原字符串</param>
/// <param name="len">截取長度</param>
/// <returns>截取後字符串</returns>
private static string GetStrPartly(string o_Str, int len)
{
if (len == 0)
{
return o_Str;
}
else
{
if (o_Str.Length > len)
{
return o_Str.Substring(0, len) + "..";
}
else
{
return o_Str;
}
}
}
/// <summary>
/// 獲取單元格內容
/// </summary>
/// <param name="cell">TableCell</param>
/// <returns>內容</returns>
private static string GetCellText(TableCell cell)
{
string text = cell.Text;
if (!string.IsNullOrEmpty(text))
{
return text;
}
foreach (Control control in cell.Controls)
{
if (control != null && control is IButtonControl)
{
IButtonControl btn = control as IButtonControl;
text = btn.Text.Replace("\r\n", "").Trim();
break;
}
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
continue;
}
ITextControl l = control as ITextControl;
text = l.Text.Replace("\r\n", "").Trim();
break;
}
}
return text;
}
/// <summary>
/// 設置單元格內容
/// </summary>
/// <param name="cell">TableCell</param>
/// <param name="maxLen">最大長度</param>
private static void SetCellText(TableCell cell, int maxLen)
{
string text = cell.Text;
if (!string.IsNullOrEmpty(text))
{
cell.Text = GetStrPartly(text, maxLen);
}
foreach (Control control in cell.Controls)
{
if (control != null && control is IButtonControl)
{
IButtonControl btn = control as IButtonControl;
text = btn.Text.Replace("\r\n", "").Trim();
btn.Text = GetStrPartly(text, maxLen);
break;
}
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
continue;
}
ITextControl l = control as ITextControl;
text = l.Text.Replace("\r\n", "").Trim();
if (l is DataBoundLiteralControl)
{
cell.Text = GetStrPartly(text, maxLen);
break;
}
else
{
l.Text = GetStrPartly(text, maxLen);
break;
}
}
}
}
#endregion
}