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[];
}
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);
//}
C#.NET 通用控件数据源绑定类的更多相关文章
- 在Bootstrap开发框架中使用dataTable直接录入表格行数据(2)--- 控件数据源绑定
在前面随笔<在Bootstrap开发框架中使用dataTable直接录入表格行数据>中介绍了在Web页面中使用Jquery DataTable插件进行对数据直接录入操作,这种处理能够给用户 ...
- WPF DevExpress Chart控件 界面绑定数据源,不通过C#代码进行绑定
<Grid x:Name="myGrid" Loaded="Grid_Loaded" DataContext="{Binding PartOne ...
- 在GridControl控件上绑定图片的几种操作方式
我们知道,基于DevExpress的开发Winform的项目界面的时候,GridControl控件是经常用来绑定数据的,一般以常规的字符内容为主,有时候也会有图片的显示需要,那么如果显示图片,我们应该 ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton
[源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...
- 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox
[源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...
- 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton
介绍背水一战 Windows 10 之 控件(按钮类) ButtonBase Button HyperlinkButton RepeatButton ToggleButton AppBarButton ...
- asp.net学习之 数据绑定控件--表格绑定控件
原文:asp.net学习之 数据绑定控件--表格绑定控件 数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...
随机推荐
- PHP+Zend 输出时中文乱码问题
1.把输入的格式改成 echo iconv("GB2312","UTF-8",'我爱PHP'); 2.其他的方法,还不会用,有待完善........
- equals和==的区别
---恢复内容开始--- equals:用于判断两个变量是否是对同一个对象的引用,即堆中的内容是否相同. 1.第一:对象不同,内容相同: ==:等于.比较两个地址是不是一样的(地址一样值肯定一样)(比 ...
- Maven环境搭建
1,下载maven zip版 http://maven.apache.org/download.cgi 2,配置maven环境变量 (1) 新建MAVEN_HOME环境变量 E:\TOOLS\A ...
- 读取config配置
在搭建自动化测试框架时,经常会使用config.properties文件存储配置,文件内容格式如下: 读取config.properties文件代码如下: public class Putils { ...
- 阿里云服务器Linux CentOS安装配置(六)resin多端口配置、安装、部署
阿里云服务器Linux CentOS安装配置(六)resin多端口配置.安装.部署 1.下载resin包 http://125.39.66.162/files/2183000003E08525/cau ...
- VECTOR COMPUTATION
COMPUTER OR GANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Although the perform ...
- Hibernate 异常提示_1
INFO: HHH000041: Configured SessionFactory: null九月 15, 2016 12:29:35 上午 org.hibernate.engine.jdbc.co ...
- EasyUI分页
$("#tanModelBox1").css("display","none"); $('#dg').datagrid({ fitColum ...
- vim - save current file with a new name but keep editing current file
http://superuser.com/questions/414110/vim-save-a-file-as-a-different-filename-but-keep-w-as-the-curr ...
- git学习--常用命令
1.初始化一个 Git 仓库 git init <directory> //参数 directory:作为Git仓库的目录 2.从Git 仓库中拷贝项目 git clone <rep ...