在“随风飘散” 博客里面,介绍了一个不错的DataGridView数据窗口控件《DataGridView数据窗口控件开发方法及其源码提供下载》,这种控件在有些场合下,还是非常直观的。因为,在一般要求客户录入数据的地方,一般有两种途径,其一是通过弹出一个新的窗口,在里面列出各种需要输入的要素,然后保存的,如下图所示;

其二就是直接在DataGridView中直接输入。这两种方式各有优劣,本文介绍采用该控件实现第二种模式的数据数据。如下图所示

这种方式,直接通过在DataGridView中下拉列表或者文本框中输入内容,每列的数据可以联动或者做限制,实现用户数据的约束及规范化。

控件只要接受了DataTable的DataSource之后,会根据列的HeadText内容,显示表格的标题及内容,应用还是比较直观方便的。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        private void BindGridViewData(DataTable dt, DataTable dtNoRelation)
{
organTable = dt;
noRelationTable = dtNoRelation; DataGridView dataGridView1 = new DataGridView();
this.groupBox1.Controls.Clear();
this.groupBox1.Controls.Add(dataGridView1);
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.CellValueChanged +=new DataGridViewCellEventHandler(organDataGridView_CellValueChanged);
dataGridView1.UserDeletedRow += new DataGridViewRowEventHandler(organDataGridView_UserDeletedRow); dataGridView1.AutoGenerateColumns = false;
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear(); DataGridViewDataWindowColumn col1 = new DataGridViewDataWindowColumn();
col1.HeaderText = "机构代码";
col1.Name = "机构代码"; //下拉列表的数据
col1.DataSource = GetDataTable(dtNoRelation);
dataGridView1.Columns.Add(col1); DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
col2.HeaderText = "机构名称";
col2.Name = "机构名称";
col2.Width = 300;
col2.ReadOnly = true;
dataGridView1.Columns.Add(col2); if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
string value = dr[0].ToString();
DataGridViewRow row = new DataGridViewRow();
DataGridViewDataWindowCell cell = new DataGridViewDataWindowCell();
cell.Value = value;
row.Cells.Add(cell);
cell.DropDownHeight = 400;
cell.DropDownWidth = 300; DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
cell2.Value = dr[1].ToString();
row.Cells.Add(cell2);
dataGridView1.Rows.Add(row);
}
}
}

由于列之间的数据输入等相关的影响需要处理,因此控件的处理方式是通过委托函数进行处理,如上面的部分代码中就是处理这些事件的。

            dataGridView1.CellValueChanged +=new DataGridViewCellEventHandler(organDataGridView_CellValueChanged);
dataGridView1.UserDeletedRow += new DataGridViewRowEventHandler(organDataGridView_UserDeletedRow);

由于本例子是通过输入内容后,及时更新数据库及控件的显示,因此需要对该事件进行处理,处理代码如下所示。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        private void organDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView organDataGridView = sender as DataGridView;
if (!organDataGridView.IsCurrentCellInEditMode)
return; #region 显示关联机构名称
if (e.RowIndex > -1)
{
if (organDataGridView.CurrentCell.Value == System.DBNull.Value)
{
return;
} if (e.ColumnIndex == 0)
{
DataGridViewCell cell = organDataGridView.Rows[e.RowIndex].Cells["机构代码"];
if (cell.Value == null)
return;
string organCode = cell.Value.ToString();
string organName = GetOrganName(organTable, organCode);
if (string.IsNullOrEmpty(organName))
{
organName = GetOrganName(noRelationTable, organCode);
}
organDataGridView.Rows[e.RowIndex].Cells["机构名称"].Value = organName;
}
}
#endregion if (this.treeView1.SelectedNode != null)
{
string gjOrganCode = this.treeView1.SelectedNode.Tag.ToString();
if (!string.IsNullOrEmpty(gjOrganCode))
{
string yctOrganCode = organDataGridView.CurrentCell.Value.ToString(); OrganCodeMapDAL organMapDal = new OrganCodeMapDAL();
organMapDal.UpdateOrganMapData(gjOrganCode, yctOrganCode);
}
}
} private void organDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
DataGridView organDataGridView = sender as DataGridView;
string organCode = e.Row.Cells[0].Value.ToString();
OrganCodeMapDAL organMapDal = new OrganCodeMapDAL();
organMapDal.DeleteOrganMapData(organCode);
}

另外,该控件还提供了一个用于对话框窗体中的复杂下拉列表的数据显示方式,控件支持内容的过滤检索,非常方便实用,如下所示

该控件的使用代码如下所示:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        private void BindData()
{
//设置DataWindow属性
this.dataWindow1.PopupGridAutoSize = false;
this.dataWindow1.DropDownHeight = 300;
this.dataWindow1.DropDownWidth = 240;
this.dataWindow1.FormattingEnabled = true;
this.dataWindow1.sDisplayField = "车辆编码,车牌号码";
this.dataWindow1.sDisplayMember = "车辆编码";
this.dataWindow1.sValueMember = "车辆编码";
this.dataWindow1.SeparatorChar = "|"; BusDAL busDal = new BusDAL();
DataTable dt = busDal.GetYCTBusTable(this.txtOrganName.Tag.ToString());
this.dataWindow1.DataSource = dt;
this.dataWindow1.AfterSelector += new EventHandler(dataWindow1_AfterSelector); //必须在DataSource绑定之后设置该属性
this.dataWindow1.RowFilterVisible = true;
} //选择完下拉表格后执行的事件
private void dataWindow1_AfterSelector(object sender, EventArgs e)
{
}

使用DataGridView数据窗口控件,构建用户快速输入体验的更多相关文章

  1. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  2. C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面

    个人理解,开发应用程序的目的,不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景也最为复杂,包括但不限于:表格记录查询.报表查询.导出文件查询等等 ...

  3. 基于MVC4+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面

    最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开发框架保持一致,而在Web上,我主要采用EasyUI的前端界面处理技术,走MVC的技术路线,在重 ...

  4. Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面 z

    http://www.cnblogs.com/zuowj/p/4504130.html 不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景 也最为 ...

  5. 使用EasyUI的树控件构建Web界面

    最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开发框架保持一致,而在Web上,我主要采用EasyUI的前端界面处理技术,走MVC的技术路线,在重 ...

  6. (转)基于MVC4+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面

    http://www.cnblogs.com/wuhuacong/p/3669575.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...

  7. [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法

    如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...

  8. .NET各大平台数据列表控件绑定原理及比较(WebForm、Winform、WPF)

    说说WebForm: 数据列表控件: WebForm 下的列表绑定控件基本就是GridView.DataList.Repeater:当然还有其它DropDownList.ListBox等. 它们的共同 ...

  9. Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄(转)

    用我的方法来控制其他程序窗体上的窗口控件,必须先了解什么是 回调函数.我的理解是这样的: 回 调函数写出来不是自己的程序去调用的,反而是让其他的东西去调用,比如windows操作系统,比如其他的程序等 ...

随机推荐

  1. Contains DuplicateII

    超时版: /*Contains Duplicate II Given an array of integers and an integer k, find out whether there the ...

  2. 在Yarn上运行spark-shell和spark-sql命令行

    转载自:http://lxw1234.com/archives/2015/08/448.htm 如果你已经有一个正常运行的Hadoop Yarn环境,那么只需要下载相应版本的Spark,解压之后做为S ...

  3. 翻译:深入 AngularUI Router

    原文地址:http://www.ng-newsletter.com/posts/angular-ui-router.html ui-router: https://angular-ui.github. ...

  4. VC++中,如何定义callback函数和它的触发事件?

    对于回调函数的编写始终是写特殊处理功能程序时用到的技巧之一.先介绍一下回调的使用基本方法与原理. 1.在这里设:回调函数为A()(这是最简单的情况,不带参数,但我们应用的实际情况常常很会复杂),使用回 ...

  5. android EditText 只允许输入指定字符

    实现只允许只入数字和字符 方法一:在XML文件中实现布局如下: <EditText android:layout_width="match_parent" android:l ...

  6. hibernate常见错误

    1.Hibernate: Could not synchronize database state with session 1.主键不是自动生成的,然后自己没手动设置.  2.插入的实体字段跟数据库 ...

  7. Selenium2.0介绍

    selenium是一个web的自动化测试工具,和其它的自动化工具相比来说其最主要的特色是跨平台.跨浏览器. 支持windows.linux.MAC,支持ie.ff.safari.opera.chrom ...

  8. FastDFS的安装配置

    一:实验描述: fastdfs 介绍 FastDFS是一个开源的分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别 ...

  9. linux下DNS设置以及解析顺序

    1.编辑/etc/resolv.conf文件,添加如下语句: nameserver dns_ip(例如nameserver 8.8.8.8) 2.如/etc/nsswitch.conf中未包含启用DN ...

  10. USACO Section 2.3 奶牛家谱 Cow Pedigrees

    OJ:http://www.luogu.org/problem/show?pid=1472 #include<iostream> using namespace std; const in ...