65.9K
CodeProject 正在变化。 阅读更多。
Home

处理 DataGrid 单元格的键盘事件

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.62/5 (6投票s)

2006年3月9日

CPOL
viewsIcon

61708

处理 DataGrid 中 DataGridCells 的 Key-Down 和类似事件。

引言

这是我第一次向 The Code Project 贡献。最近,尝试捕获 DataGrid 中的单元格的 KeyDown KeyPress 事件,结果发现难度远超预期,因为 KeyPressKeyDown 和类似的事件仅为 DataGrid 提供。

处理 DataGridCell KeyDown 事件的示例代码

DataGrid 创建一个 TableStyle

private void CreateTableStyle()
{
    try
    {
        DataGridTableStyle ts = new DataGridTableStyle();
        ts.HeaderForeColor = Color.Black;  
        ts.HeaderFont = new Font("Verdana", (float)8.25);   
        ts.MappingName = this.dt.TableName;
            
        DataGridColumnStyle cs;
        DataGridTextBoxColumn tbc;
    
        foreach( DataRow dr in this.columns.Rows)
        {
            if ( dr["ColType"].ToString() == "System.Boolean")
            {
                cs = new DataGridBoolColumn();
                ((DataGridBoolColumn)cs).AllowNull = false;
            }
            else 
            {
                cs = new DataGridTextBoxColumn();
                tbc = (DataGridTextBoxColumn) cs;
                tbc.TextBox.KeyDown += new KeyEventHandler(TextBox_KeyDown);
            }
        
            cs.HeaderText = dr["ColName"].ToString();
            cs.MappingName = dr["ColName"].ToString();
            cs.Width = Convert.ToInt32(dr["ColWidth"].ToString());
                                                
            if (dr["ReadOnly"].ToString() == "true")
            {
                cs.ReadOnly = true;
            }

            switch( dr["ColAlign"].ToString().ToUpper())
            {
                case "C":
                    cs.Alignment = HorizontalAlignment.Center;
                    break;
                case "L":
                    cs.Alignment = HorizontalAlignment.Left;
                    break;
                case "R":
                    cs.Alignment = HorizontalAlignment.Right; 
                    break;                            
            }
                        
            ts.HeaderFont = new System.Drawing.Font("Verdana", 8.25F);
            ts.HeaderBackColor = Color.Gainsboro; 
            ts.HeaderForeColor = Color.Black;
            ts.SelectionBackColor  = Color.Silver;                    
            ts.AllowSorting = true;    
            ts.GridColumnStyles.Add(cs);                    
        }

        this.dg.TableStyles.Clear();
        this.dg.TableStyles.Add(ts);
        this.dg.DataSource = this.dt;
    }
    catch ( Exception e )
    {
        throw e;
    }
}

将代码添加到 TextBox KeyDown 事件的处理程序中

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    try
    {
        if ((e.KeyCode >= Keys.A) && (e.KeyCode <= Keys.Z))
        {
            this.SearchExpression += (char) e.KeyValue;
            this.SetFilter(this.GetCurrentColumnName());                    
        }
        else
        {
            if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back || 
            e.KeyCode == Keys.Space)
            {
                this.SearchExpression = string.Empty;
                this.ViewRecords.RowFilter = string.Empty;
                this.dg.SetDataBinding(this.dt,"");
            }            
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

获取 DataGrid 单元格的当前列名

private string GetCurrentColumnName()
{
    try
    {
        int colNo;
    
        if (this.dt != null)
        {
            colNo = this.dg.CurrentCell.ColumnNumber;
            return this.dg.TableStyles[0].GridColumnStyles[colNo].MappingName.ToString();
        }
        else
        {
            return string.Empty;
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
        return string.Empty;
    }            
}

Form UserControl 中声明的 DataView 上设置 RowFilter

private void SetFilter(string ColumnName)
{            
    try
    {
        this.ViewRecords = new DataView(this.dt);
    
        this.ViewRecords.AllowNew = false;
        this.ViewRecords.AllowEdit = false;
        this.ViewRecords.AllowDelete = false;

        if (ColumnName != string.Empty)
        {
            this.ViewRecords.RowFilter = "[" + ColumnName + "] " + 
                "LIKE '" +  this.SearchExpression + "' + " + "'%'";
            this.dg.SetDataBinding(this.ViewRecords,"");
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

关注点

本文档展示了如何捕获和处理 DataGrid 中单元格的 键盘 事件。

历史

  • 2006-03-09:上传了第一个版本
© . All rights reserved.