mahesh waghela

mahesh waghela

  • NA
  • 93
  • 113.9k

Revoke the Access Of EventHandller of Datagridview

Nov 5 2011 7:35 AM

I have Bound Datagridview and and trying to access of editing control as below:

private void newdgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
   
{
       
if (newdgv.CurrentCell.ColumnIndex ==0)
       
{
           
if (e.Control is TextBox)
           
{
               
TextBox tbox = e.Control as TextBox;
                tbox
.KeyDown += new KeyEventHandler(tbox_KeyDown);
                tbox
.TextChanged += new EventHandler(tbox_TextChanged);
           
}
       
}
   
}

   
private void tbox_TextChanged(object sender, EventArgs e)
   
{
       
TextBox tbox = (TextBox)sender;
       
if (tbox.Text.Length  > 2)
       
{
            listBox2
.Visible = true;
            button2
.Visible = true;
           
int inddex = listBox2.FindString(tbox.Text);

           
if (inddex != -1)
           
{
                listBox2
.SetSelected(inddex, true);
                listBox2
.Focus();
           
}

       
}
        tbox
.TextChanged -= new EventHandler(tbox_TextChanged);              
   
}

Above at tbox_Texchanged EventHandller I am trying to Revoke the access of the same due to avoid of repeating same task on other columns's control. But it won't allow me above task. I can't see Listbox2 on passing the text limit of control.

Now look at the same example in other way:

private void newdgv_EditingControlShowing(object sender,  DataGridViewEditingControlShowingEventArgs e)
   
{
       
if (newdgv.CurrentCell.ColumnIndex ==0)
       
{
           
if (e.Control is TextBox)
           
{
               
TextBox tbox = e.Control as TextBox;
                tbox
.KeyDown += new KeyEventHandler(tbox_KeyDown);
                tbox
.TextChanged -= new EventHandler(tbox_TextChanged);              
                tbox
.TextChanged += new EventHandler(tbox_TextChanged);
           
}
       
}
   
}

   
private void tbox_TextChanged(object sender, EventArgs e)
   
{
       
TextBox tbox = (TextBox)sender;
       
if (tbox.Text.Length  > 2)
       
{
            listBox2
.Visible = true;
            button2
.Visible = true;
           
int inddex = listBox2.FindString(tbox.Text);

           
if (inddex != -1)
           
{
                listBox2
.SetSelected(inddex, true);
                listBox2
.Focus();
           
}

       
}

}

Now see here it is allowed to see listbox2 on Column "0" and "1" Controls by passing of text limit. But I just want to fire the even on only column="0"'s Controls Not on Columns="1"'s Controls.

I Confused Bewteen Revoke the Access Of EventHandller.

How to Do?.



Answers (5)