C#.NET 通用控件数据源绑定类
using System.Data; using System.Collections; using System.Collections.Generic; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Windows.Forms; namespace Nbjjy.Utilities { public static class ControlHelper { #region 数据源 /// <summary> /// 获取或设置源,该源包含用于填充控件中的项的值列表。 /// </summary> private static object dataSource; public static object DataSource { get { if (dataSource is DataSet) { DataSet ds = dataSource as DataSet; dataSource = ds.Tables[0]; } else if (dataSource is DataTable) { DataTable dt = dataSource as DataTable; dataSource = dt; } else if (dataSource is DataView) { DataView dv = dataSource as DataView; dataSource = dv; } else if (dataSource is IDataReader) { IDataReader dr = dataSource as IDataReader; dataSource = dr; } else if (dataSource is IEnumerable) { IEnumerable objList = dataSource as IEnumerable; List<object> list = new List<object>(); foreach (object obj in objList) { list.Add(obj); } dataSource = list; } return dataSource; } set { dataSource = value; } } #endregion #region WebUI绑定Control /// <summary> ///WebUI绑定Control /// </summary> /// <param name="control">定义由所有 ASP.NET 服务器控件共享的属性、方法和事件。</param> /// <param name="dataSource">获取或设置源,该源包含用于填充控件中的项的值列表。</param> public static void BindControl(System.Web.UI.Control control, object dataSource) { DataSource = dataSource; if (control is System.Web.UI.WebControls.DataList) { (control as System.Web.UI.WebControls.DataList).DataSource = DataSource; (control as System.Web.UI.WebControls.DataList).DataBind(); } else if (control is System.Web.UI.WebControls.GridView) { (control as System.Web.UI.WebControls.GridView).DataSource = DataSource; (control as System.Web.UI.WebControls.GridView).DataBind(); } else if (control is System.Web.UI.WebControls.Repeater) { (control as System.Web.UI.WebControls.Repeater).DataSource = DataSource; (control as System.Web.UI.WebControls.Repeater).DataBind(); } } /// <summary> ///WebUI绑定Control /// </summary> /// <param name="control">定义由所有 ASP.NET 服务器控件共享的属性、方法和事件。</param> /// <param name="dataSource">获取或设置源,该源包含用于填充控件中的项的值列表。</param> /// <param name="dataTextField">获取或设置为列表项提供文本内容的数据源字段。</param> /// <param name="dataValueField">获取或设置为各列表项提供值的数据源字段。</param> public static void BindControl(System.Web.UI.Control control, object dataSource, string dataTextField, string dataValueField) { DataSource = dataSource; if (control is System.Web.UI.WebControls.CheckBoxList) { (control as System.Web.UI.WebControls.CheckBoxList).DataSource = DataSource; (control as System.Web.UI.WebControls.CheckBoxList).DataTextField = dataTextField; (control as System.Web.UI.WebControls.CheckBoxList).DataValueField = dataValueField; (control as System.Web.UI.WebControls.CheckBoxList).DataBind(); } else if (control is System.Web.UI.WebControls.DropDownList) { (control as System.Web.UI.WebControls.DropDownList).DataSource = DataSource; (control as System.Web.UI.WebControls.DropDownList).DataTextField = dataTextField; (control as System.Web.UI.WebControls.DropDownList).DataValueField = dataValueField; (control as System.Web.UI.WebControls.DropDownList).DataBind(); } else if (control is System.Web.UI.WebControls.RadioButtonList) { (control as System.Web.UI.WebControls.RadioButtonList).DataSource = DataSource; (control as System.Web.UI.WebControls.RadioButtonList).DataTextField = dataTextField; (control as System.Web.UI.WebControls.RadioButtonList).DataValueField = dataValueField; (control as System.Web.UI.WebControls.RadioButtonList).DataBind(); } else if (control is System.Web.UI.WebControls.BulletedList) { (control as System.Web.UI.WebControls.BulletedList).DataSource = DataSource; (control as System.Web.UI.WebControls.BulletedList).DataTextField = dataTextField; (control as System.Web.UI.WebControls.BulletedList).DataValueField = dataValueField; (control as System.Web.UI.WebControls.BulletedList).DataBind(); } } #endregion #region WinUI绑定Control /// <summary> /// DataGridView绑定 /// </summary> /// <param name="dataGridView">在可自定义的网格中显示数据。</param> /// <param name="dataSource">获取或设置数据源中 System.Windows.Forms.DataGridView 显示其数据的列表或表的名称。</param> public static void BindControl(System.Windows.Forms.DataGridView dataGridView, object dataSource) { DataSource = dataSource; dataGridView.DataSource = DataSource; } /// <summary> /// ListView绑定 /// </summary> /// <param name="listView">表示 Windows 列表视图控件,该控件显示可用四种不同视图之一显示的项集合。</param> /// <param name="text">获取或设置该项的文本。</param> /// <param name="value">基于数组创建新子项,并将其添加到集合中。[可空参数]</param> /// <param name="view">每个项都显示为一个最大化图标,在它的下面有一个标签。</param> public static void BindControl(System.Windows.Forms.ListView listView, string text, string[] value, System.Windows.Forms.View view) { listView.View = view; ListViewItem lvi = new ListViewItem(); lvi.Text = text; listView.Items.Add(lvi); if (value != null) { lvi.SubItems.AddRange(value); } } /// <summary> /// WinUI绑定Control /// </summary> /// <param name="control">定义控件的基类,控件是带有可视化表示形式的组件。ID</param> /// <param name="dataSource">获取或设置此 System.Windows.Forms.Control 的数据源。</param> /// <param name="displayMember">获取或设置要为此 System.Windows.Forms.Control 显示的属性。</param> /// <param name="valueMember">获取或设置一个属性,该属性将用作 System.Windows.Forms.Control 中的项的实际值。</param> public static void BindControl(System.Windows.Forms.Control control, object dataSource, string displayMember, string valueMember) { DataSource = dataSource; if (control is System.Windows.Forms.ComboBox) { (control as System.Windows.Forms.ComboBox).DataSource = DataSource; (control as System.Windows.Forms.ComboBox).DisplayMember = displayMember; (control as System.Windows.Forms.ComboBox).ValueMember = valueMember; } else if (control is System.Windows.Forms.CheckedListBox) { (control as System.Windows.Forms.CheckedListBox).DataSource = DataSource; (control as System.Windows.Forms.CheckedListBox).DisplayMember = displayMember; (control as System.Windows.Forms.CheckedListBox).ValueMember = valueMember; } else if (control is System.Windows.Forms.ListBox) { (control as System.Windows.Forms.ListBox).DataSource = DataSource; (control as System.Windows.Forms.ListBox).DisplayMember = displayMember; (control as System.Windows.Forms.ListBox).ValueMember = valueMember; } } #endregion } }
调用方法:
List<Model.BaseItemsType> list = BLL.BaseItemsType.GetList("", "");
foreach (Model.BaseItemsType model in list)
{
ControlHelper.BindControl(this.listView1, model.FullName, null, View.List);
}
ControlHelper.BindControl(this.dataGridView1, list);
ControlHelper.BindControl(this.comboBox1, list, "FullName", "Id");
ControlHelper.BindControl(this.listBox1, list, "FullName", "Id");
ControlHelper.BindControl(this.checkedListBox1, list, "FullName", "Id");
//DataTable list = BLL.BaseItemsType.GetDataTable("", "");
//DataSet list = BLL.BaseItemsType.GetDataSet("", "");
//DataView list = BLL.BaseItemsType.GetDataView("", "");
//for (int i = 0; i < list.Count; i++)
//{
// ControlHelper.BindControl(this.listView1, list.Table.Rows[i]["FullName"].ToString(), null, View.List);
//}
//for (int i = 0; i < list.Tables[0].Rows.Count; i++)
//{
// ControlHelper.BindControl(this.listView1, list.Tables[0].Rows[i]["FullName"].ToString(), null, View.List);
//}
//for (int i = 0; i < list.Rows.Count; i++)
//{
// ControlHelper.BindControl(this.listView1, list.Rows[i]["FullName"].ToString(), null, View.List);
//}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步