我想在DataGridView中找一个类似TextBox的TextChanged的事件,DataGridView的CellValueChanged是不行的,因 为他要编辑完了后触发,我要的是输入每一个字符都要触发,,谢谢各位,在线等

解决方案 »

  1.   

    Public Class Form1    Private DataGridView1 As New DataGridView
        Private ComboBox1 As New ComboBox
        Private TextBox1 As New TextBox    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler DataGridView1.CellClick, AddressOf DataGridView1_CellClick
            AddHandler ComboBox1.TextChanged, AddressOf ComboBox1_TextChanged
            AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged        Me.DataGridView1.AllowUserToResizeRows = False
            Me.DataGridView1.AllowUserToResizeColumns = False
            Me.DataGridView1.Dock = DockStyle.Fill        Me.DataGridView1.Columns.Add("Column1", "TextBox")
            Me.DataGridView1.Columns.Add("Column2", "ComboBox")        For i As Integer = 1 To 3
                Me.DataGridView1.Rows.Add("Row" + i.ToString + " Column1", "Row" + i.ToString + " Column2")
            Next        For i As Integer = 0 To 10
                Me.ComboBox1.Items.Add("Item" + i.ToString)
            Next
            Me.ComboBox1.Visible = False
            Me.TextBox1.Visible = False        Me.Controls.Add(ComboBox1)
            Me.Controls.Add(TextBox1)
            Me.Controls.Add(DataGridView1)
        End Sub    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
            If e.ColumnIndex = 1 Then
                Dim p As Point = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
                p.Offset(Me.DataGridView1.Left, Me.DataGridView1.Top)
                Me.ComboBox1.Location = p
                Me.ComboBox1.Size = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Size
                Me.ComboBox1.Visible = True
                Me.TextBox1.Visible = False
            ElseIf e.ColumnIndex = 0 Then
                Dim p As Point = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
                p.Offset(Me.DataGridView1.Left, Me.DataGridView1.Top)
                Me.TextBox1.Location = p
                Me.TextBox1.Size = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Size
                Me.TextBox1.Text = Me.DataGridView1.CurrentCell.Value
                Me.ComboBox1.Visible = False
                Me.TextBox1.Visible = True
            Else
                Me.ComboBox1.Visible = False
                Me.TextBox1.Visible = False
            End If
        End Sub    Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.DataGridView1.CurrentCell.Value = Me.ComboBox1.Text
            Me.ComboBox1.Visible = False
        End Sub    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.DataGridView1.CurrentCell.Value = Me.TextBox1.Text
            Debug.WriteLine(String.Format("[{0}]{1}", Now, TextBox1.Text))
        End SubEnd Class
      

  2.   

    类似的问题参考:
    http://topic.csdn.net/u/20080617/10/61942f0c-2fca-4ab1-be32-a73216d58dfc.html
    http://topic.csdn.net/u/20080403/22/f3023d92-d42a-4bff-bc38-dae7be29046e.html
      

  3.   

            private DataGridViewTextBoxEditingControl currencyTextBox = null;        private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: 这行代码将数据加载到表“adventureWorksDataSet.Address”中。您可以根据需要移动或移除它。
                this.addressTableAdapter.Fill(this.adventureWorksDataSet.Address);        }        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                DataGridViewTextBoxEditingControl control = e.Control as DataGridViewTextBoxEditingControl;
                if (control != null)
                {
                    control.TextChanged += new EventHandler(currencyTextBox_TextChanged);
                }            currencyTextBox = control;
                
                
            }        private void currencyTextBox_TextChanged(Object sender, EventArgs e)
            {
                try
                {
                    // Convert the text to a Double and determine if it is a negative number.
                    if (double.Parse(currencyTextBox.Text) < 0)
                    {
                        // If the number is negative, display it in Red.
                        currencyTextBox.ForeColor = Color.Red;
                    }
                    else
                    {
                        // If the number is not negative, display it in Black.
                        currencyTextBox.ForeColor = Color.Black;
                    }
                }
                catch
                {
                    // If there is an error, display the text using the system colors.
                    currencyTextBox.ForeColor = SystemColors.ControlText;
                }
            }
      

  4.   

    原理:10. How do I hook up events on the editing control?Sometimes you will need to handle specific events provided by the editing control for a cell. You can do this by first handling the DataGridView.EditingControlShowing event. Next access the DataGridViewEditingControlShowingEventArgs.Control property to get the editing control for the cell. You might need to cast the control to a specific control type if the event you are interested is not based of the Control class. NOTE: The DataGridView reuses editing controls across cells if the type is the same. Because of this you should make sure that you do not continuously hook up a new event handler if there is already one hooked up otherwise you your event handler will get called multiple times.