代码之家  ›  专栏  ›  技术社区  ›  Sadegh

在编辑DataGridView单元格值时访问其值

  •  2
  • Sadegh  · 技术社区  · 15 年前

    我有一个带有DataGridView的窗体,当用户开始为第一行中的第一个单元格输入值时,也可以按F2提交该值,但除非用户点击Tab并转到另一个单元格,否则我无法访问单元格值。

    下面是我的代码,用于在按F2键时访问单元格值

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            var key = new KeyEventArgs(keyData);
    
            ShortcutKey(this, key);
    
            return base.ProcessCmdKey(ref msg, keyData);
        }
    
    
        protected virtual void ShortcutKey(object sender, KeyEventArgs key)
        {
            switch (key.KeyCode)
            {
                case Keys.F2:
                    MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
                    break;
            }
        }
    

    DataGridView1.SelectedCells[0].值返回空值

    4 回复  |  直到 10 年前
        1
  •  5
  •   BFree    15 年前

    不如这样吧。钩住DataGridView的“EditingControlShowing”事件并在那里捕获F2。一些代码:

    public partial class Form1 : Form
    {
        private DataTable table;
        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(HandleEditingControlShowing);
            this.table = new DataTable();
            table.Columns.Add("Column");
            table.Rows.Add("Row 1");
            this.dataGridView1.DataSource = table;
        }
    
    
        private void HandleEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            var ctl = e.Control as DataGridViewTextBoxEditingControl;
            if (ctl == null)
            {
                return;
            }
    
            ctl.KeyDown -= ctl_KeyDown;
            ctl.KeyDown += new KeyEventHandler(ctl_KeyDown);
    
        }
    
        private void ctl_KeyDown(object sender, KeyEventArgs e)
        {
            var box = sender as TextBox;
            if (box == null)
            {
                return;
            }
    
            if (e.KeyCode == Keys.F2)
            {
                this.dataGridView1.EndEdit();
                MessageBox.Show(box.Text);
            }
        }
    

    }

    这个想法很简单,你可以加入到编辑控件显示事件中。每当一个单元格进入编辑模式,它就会被激发。很酷的是,它公开了实际的底层控件,您可以将其强制转换为实际的WinForms控件,并像平常一样钩住所有的事件。

        2
  •  2
  •   Sadegh    15 年前

    @bfree谢谢你的代码启发了我;)为什么不直接调用这个.dataGridView1.endedit(); 在messagebox.show之前(dataGridView1.SelectedCells[0].value.ToString());

    这个代码工作得很好:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            var key = new KeyEventArgs(keyData);
    
            ShortcutKey(this, key);
    
            return base.ProcessCmdKey(ref msg, keyData);
        }
    
    
        protected virtual void ShortcutKey(object sender, KeyEventArgs key)
        {
            switch (key.KeyCode)
            {
                case Keys.F2:
    dataGridView1.EndEdit();
                    MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
                    break;
            }
        }
    
        3
  •  1
  •   Illidanek    10 年前

    你可以试试这个

    string str = dataGridView.CurrentCell.GetEditedFormattedValue
                 (dataGridView.CurrentCell.RowIndex, DataGridViewDataErrorContexts.Display)
                 .ToString();
    
        4
  •  0
  •   Nick Berardi    15 年前

    有一个 OnKeyDown 处理程序 DataGridViewCell :

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.onkeydown.aspx

    但是,唯一的问题是,您必须创建自己的自定义单元 DataGridViewTextBoxCell 以获得预期的功能。因为没有为此处理程序公开的事件。