技术开发 频道

巧用DataGridView控件构建快速输入体验

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

1
 


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

1
 

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

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

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);
                }
            }
        }

 

0
相关文章