注意,以下是SmartGrid源码中的代码,我们贴出来,只是供我们学习和参考。
(1)判断对象是否存在某个css属性
private bool HasStyle(string styleKey)
{
CssStyleCollection css = this.SmartGrid1.
Attributes.CssStyle;
foreach (string key in css.Keys)
{
if (key.ToLower() == styleKey.ToLower())
{
return true;
}
}
return false;
}
(2)在属性的set方法中判断值是否所需类型(is)
public virtual object DataSource
{
get
{
return this.dataSource;
}
set
{
if (!((((value == null) || (value is IListSource)) || (value is IEnumerable)) || (value is XmlElement)))
{
throw new ArgumentException(Smart.Web.UI.WebControls.SR.GetString("Invalid_DataSource_Type"));
}
this.smartCreated = false;
this.dataSource = value;
this.clientView = "";
this.CurrentRowIndex = -1;
this.selection = "";
if (value is XmlElement)
{
this.DataSourceType = 0;
}
else if (value is DataTable)
{
(value as DataTable).AcceptChanges();
}
}
}
(3)将DataTable、DataView、DataSet转为DataTable
private DataTable GetTableFromTabularSource()
{
DataTable table = null;
if ((!(this.dataSource is DataTable) && !(this.dataSource is DataView)) && !(this.dataSource is DataSet))
{
return table;
}
if (this.dataSource is DataView)
{
return ((DataView)this.dataSource).Table;
}
if (this.dataSource is DataSet)
{
if (this.DataMember.Length == 0)
{
return ((DataSet)this.dataSource).Tables[0];
}
return ((DataSet)this.dataSource).Tables[this.DataMember];
}
return (this.dataSource as DataTable);
}
(4)使用IDataReader的GetSchemaTable获取表结构信息(如栏位)
DataTable dt = ((IDataReader)this.dataSource).GetSchemaTable();
key = string.Empty;
foreach (DataRow row in dt.Rows)
{
if (BaseDataList.IsBindableType((Type)row["DataType"]))
{
col = (((Type)row["DataType"]) == typeof(bool)) ? ((
SmartGridColumn)new CheckBoxColumn()) : ((SmartGridColumn)new TextBoxColumn());
((IStateManager)col).TrackViewState();
col.ColumnName = row["ColumnName"].ToString();
col.ReadOnly = (bool)row["IsReadOnly"];
if (((Type)row["DataType"]) == typeof(string))
{
col.MaxLength = (int)row["ColumnSize"];
}
if (row["NumericScale"] != null)
{
}
if (row["NumericPrecision"] != null)
{
col.Precision = (short)row["NumericPrecision"];
}
if (!((((bool)row["AllowDBNull"]) || ((bool)row["IsAutoIncrement"])) || ((bool)row["IsUnique"])))
{
col.AllowNull = (bool)row["AllowDBNull"];
}
col.DataType = row["DataType"].ToString();
col.AutoGened = true;
this.Columns.Add(col);
if (((this.DataKeyField.Trim().Length == 0) && !(row["IsKey"] is DBNull)) && ((bool)row["IsKey"]))
{
key = key + row["ColumnName"].ToString() + ",";
}
}
}
(5)遍历XmlElement的属性
XmlElement el = (XmlElement)((XmlElement)this.dataSource).FirstChild;
XmlAttributeCollection attrs = el.Attributes;
foreach (XmlAttribute attr in attrs)
{
col = new TextBoxColumn();//TextBoxColumn
((IStateManager)col).TrackViewState();
col.ColumnName = attr.Name;
col.AutoGened = true;
this.Columns.Add(col);
}
(6)遍历对象属性
PropertyDescriptorCollection propertyDescriptorCollection = Smart.Web.UI.WebControls.DataSourceHelper.GetItemProperties(this.dataSource, new PropertyDescriptor[0]);
if ((propertyDescriptorCollection != null) && (propertyDescriptorCollection.Count != 0))
{
foreach (PropertyDescriptor descriptor in propertyDescriptorCollection)
{
Type t = descriptor.PropertyType;
if (BaseDataList.IsBindableType(t))
{
col = (t == typeof(bool)) ? ((SmartGridColumn)new CheckBoxColumn()) : ((SmartGridColumn)new TextBoxColumn());
((IStateManager)col).TrackViewState();
col.ColumnName = descriptor.Name;
col.ReadOnly = descriptor.IsReadOnly;
col.AutoGened = true;
this.Columns.Add(col);
}
}
}
(7)将数据导出到Excel
if ((file == null) || (file.Trim().Length == 0))
{
file = Guid.NewGuid().ToString();
}
this.Page.Response.Charset = "GB2312";
this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + file + ".xls");
this.Page.Response.ContentEncoding = Encoding.GetEncoding("utf-7");
this.Page.Response.ContentType = "
Application/ms-excel";
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.Render2Excel(hw);
this.Page.Response.Write(tw.ToString());
this.Page.Response.End();
private void Render2Excel(HtmlTextWriter writer)
{
this.IsExport2Excel = true;
this.CreatePagedDataSource2();
writer.Write("<table cellspacing=\"0\" cellpadding=\"0\" >");
writer.Write("<colgroup >");
if (this.HasFrozenCols())
{
writer.Write("<col width=\"" + this.CalcFrozenWidth().ToString() + "\" >");
}
writer.Write("<col >");
writer.Write("</colgroup >");
writer.Write("<tr >");
if (this.HasFrozenCols())
{
writer.Write("<td nowrap valign=\"top\" >");
this.RenderFrozenHeadHtml(writer, true);
writer.Write("</td >");
}
writer.Write("<td nowrap valign=\"top\" >");
this.RenderHeadHtml(writer, true);
writer.Write("</td >");
writer.Write("</tr >");
writer.Write("<tr >");
if (this.HasFrozenCols())
{
writer.Write("<td nowrap valign=\"top\" >");
this.RenderFrozenContentHtml(writer, true);
writer.Write("</td >");
}
writer.Write("<td nowrap valign=\"top\" >");
this.RenderContentHtml(writer);
writer.Write("</td >");
writer.Write("</tr >");
writer.Write("</table >");
}
(8)在DataTable中查找符合条件的行(使用Select方法)
private DataRow FindRow(XmlElement ele, DataTable t, string[] dataKeyFields)
{
if (dataKeyFields != null)
{
string filter = string.Empty;
foreach (string dataKey in dataKeyFields)
{
string aa = filter;
filter = aa + "(" + dataKey + "='" + ele.GetAttribute(dataKey) + "') ";
}
return t.Select(filter)[0];
}
int i = int.Parse(ele.GetAttribute("rowNo"));
return t.Rows[i];
}
(9)获取网卡地址(在Asp.net环境下只可获取服务器网卡地址)
using System;
using System.ComponentModel;
using System.Management;
private string GetMacAddress()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE");
string macAddress = null;
foreach (ManagementObject mo in searcher.Get())
{
macAddress = mo["MacAddress"].ToString().Trim();
macAddress = macAddress.Replace(":", "");
break;
}
return macAddress;
}
catch
{
return "";
}
}
(10)注册客户端脚本(Page.RegisterClientScriptBlock)
if ((!this.ReadOnly || this.AllowLoadOnDemand) && !base.Page.IsClientScriptBlockRegistered("SmartGridJSP"))
{
StringBuilder jsp = new StringBuilder();
jsp.Append("<script language=\"javascript\" >\r\n");
jsp.Append("var SG_ConfirmDelete = \"" + Smart.Web.UI.WebControls.SR.GetString("LP_ConfirmDelete") + "\";\r\n");
jsp.Append("var SG_NotNull =\"" + Smart.Web.UI.WebControls.SR.GetString("LP_NotNull") + "\";\r\n");
jsp.Append("var SG_NotExist =\"" + Smart.Web.UI.WebControls.SR.GetString("LP_NotExist") + "\";\r\n");
jsp.Append("var SG_LoadOnDemand=\"" + Smart.Web.UI.WebControls.SR.GetString("LP_LoadOnDemand") + "\";\r\n");
jsp.Append("</script >");
base.Page.RegisterClientScriptBlock("SmartGridJSP", jsp.ToString());
}
t