TextBox Example Into Datagridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshwag
    New Member
    • Aug 2010
    • 71

    TextBox Example Into Datagridview

    I have simple textbox example as below:

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = "Apple";
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text.Length == 1) 
                {
                    if (textBox1.Text == "B" || textBox1.Text == "b") 
                    {
                        textBox1.Text = "Ball";
                    }
                }
            }
    By default textbox1 should return "Apple" on Form load but when I press "b" or "B" then it should return "Ball" on textbox1. I have a confusion on utilize it into datagridview. how can i do it in datagridview?.

    Suppose I have One column on datagridview like below:

    Code:
    private void Form1_Load(object sender, EventArgs e)
            {
                DataGridViewColumn Particulars = new DataGridViewTextBoxColumn();
                dataGridView1.Columns.Insert(0, Particulars );
    If I have above column In datagridview1 than How to do I with datagridview1 which I have did with textbox?.
  • Leito
    New Member
    • Apr 2010
    • 58

    #2
    You could try to process your cell text when it is changed :
    Code:
    private void dataGridView_CellValidating(object sender, DataGridViewCellCancelEventArgs e) {
        if (dataGridView.CurrentCell.Value.ToString() == "B") {
             dataGridView.CurrentCell.Value = "Ball";
         }
    }
    N.B: Not sure that works, I wrote that without testing. But I think this is the good solution.

    Comment

    • maheshwag
      New Member
      • Aug 2010
      • 71

      #3
      Leito, Sorry It's a not solution of my problem it's throw Error on Runtime Like "Null reference". Look at my Solution

      Comment

      • maheshwag
        New Member
        • Aug 2010
        • 71

        #4
        This is a perfect solution of my problem:

        Code:
          private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                {
                    int i = dataGridView1.CurrentCell.RowIndex;
                    if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
                    {
                        if (e.Control is TextBox)
                        {
                            TextBox dgvEditBox = e.Control as TextBox;
                            dgvEditBox.TextChanged += new EventHandler(dgvEditBox_TextChanged);
        
                        }
        
                    }
                    
                           
                }
        
        
                private void dgvEditBox_TextChanged(object sender, EventArgs e)
                {
                    //Extract the textbox control
                    TextBox dgvEditBox = (TextBox)sender;
                
                    //Insert the appropriate string
                    if (dgvEditBox.Text.Length == 1)
                    {
                        if (dgvEditBox.Text == "B" || dgvEditBox.Text == "b")
                        {
                            dgvEditBox.Text = "Ball";
                        }
                        
                                   
                    }
        
                    //Here I Revoke the access of EventHandler for prevent others columns to produce same result.Otherwise other columns would return the same result which column "[0]" return. 
                    dgvEditBox.TextChanged -= new EventHandler(dgvEditBox_TextChanged);
        
                }

        Comment

        Working...