mahesh
mahesh

Reputation: 1390

Restrict EventHandler In Specific Columns Of Datagridview

I have tried to restrict Following EventHandler In specific Columns of Datagridview but fail to do so.

private void dataGridView1_EditingControlShowing(object sender,     
DataGridViewEditingControlShowingEventArgs e)
    {

 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;
      //  TextBox dgvEditBox = new TextBox();

        //Insert the appropriate string
        if (dgvEditBox.Text.Length == 1)
        {
            if (dgvEditBox.Text == "B" || dgvEditBox.Text == "b")
            {
                dgvEditBox.Text = "Ball";
            }
        }    
    }

The above code suggested that I am trying to put “Ball” text into datagridview1’s “0” column but it’s

affect others columns like if I press “b” or “B” on columns 1 than it’s also return “Ball” on columns 1.

Which I don’t want. Is It possible to restrict it upto columns”O” only as per above way?.

Upvotes: 0

Views: 184

Answers (1)

user415789
user415789

Reputation:

add this to function dgvEditBox_TextChanged at the last line outside the if (dgvEditBox.Text.Length == 1) block;

dgvEditBox.TextChanged -= new EventHandler(dgvEditBox_TextChanged);

Upvotes: 1

Related Questions