技术开发 频道

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

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

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

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

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

 

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

1
 

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

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)
        {
        }
0
相关文章