Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my code i have to search data in DataGridview so i have create TextBox dynamically on the datagridview column header also i have assign KeypressEventHandler to this Textbox its works fine but when i enter keys like (q,.,!,(,#) at that time keypresseEvnt not fired but if i press delete button or space bar button after that event fires for that key Please help me
C#
private void chkBoxChange(object sender, EventArgs e)
        {

            CheckBox chk = (CheckBox)sender;
            if (chk.Checked == true)
            {
                TextBox txtBx = new TextBox();
                Rectangle rect = this.dgvItemVendor.GetCellDisplayRectangle(Convert.ToInt32(chk.Name.Split('#')[0]), -1, false);
                txtBx.Name = chk.Name.Split('#')[0] + "#-1";
                txtBx.Size = new Size(rect.Width - 20, 10);
                rect.Offset(0, 0);
                txtBx.Location = rect.Location;
                txtBx.Multiline = true;
                txtBx.Height = 18;
                this.dgvItemVendor.Controls.Add(txtBx);
                txtBx.Focus();
                txtBx.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBx_txtKeyPress);
            }
        }

        private void txtBx_txtKeyPress(object sender, KeyPressEventArgs e)
        {
            
                cpattern = 'S';
                dgvItemVendor.CurrentCell = null;
                TextBox txtGetText = (TextBox)sender;
                if (e.KeyChar == Convert.ToChar(Keys.Back))
                {
                    if (txtGetText.Text != string.Empty)
                    {
                        strSearchQuery = txtGetText.Text.Substring(0, txtGetText.Text.Length - 1);
                    }
                }
                else
                {
                    strSearchQuery = txtGetText.Text + e.KeyChar;
                }
                if (e.KeyChar == '')
                {
                    if (Clipboard.ContainsText())
                    {
                        strSearchQuery = Clipboard.GetText();
                    }
                }

                if (strSearchQuery.Length > 1)
                {
                    if (strSearchQuery.StartsWith("*"))
                        cpattern = 'E';
                    if (strSearchQuery.StartsWith("*") && strSearchQuery.EndsWith("*"))
                        cpattern = 'C';
                }
                strSearchQuery = strSearchQuery.Replace("*", string.Empty);
                //if (System.Text.RegularExpressions.Regex.IsMatch(dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString()
                //                           , strSearchQuery
                //                           , System.Text.RegularExpressions.RegexOptions.IgnoreCase))

                for (int k = 0; k < dgvItemVendor.RowCount; k++)
                {
                    switch (cpattern)
                    {
                        case 'E':
                            dgvItemVendor.Rows[k].Visible = (dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString().EndsWith(strSearchQuery, true, null)) ? true : false;
                            break;
                        case 'C':
                            dgvItemVendor.Rows[k].Visible = (dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString().ToUpper().Contains(strSearchQuery.ToUpper())) ? true : false;
                            break;
                        default:
                            dgvItemVendor.Rows[k].Visible = (dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString().StartsWith(strSearchQuery, true, null)) ? true : false;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                CustomException.ShowCustomException(ex);
            }
        }
Posted
Updated 11-Jul-13 2:15am
v3

1 solution

Hi Kiran,
please post the code for more knowledge.
 
Share this answer
 
Comments
Basmeh Awad 11-Jul-13 7:45am    
don't add comment as a solution..
[no name] 11-Jul-13 8:10am    
private void chkBoxChange(object sender, EventArgs e)
{

CheckBox chk = (CheckBox)sender;
if (chk.Checked == true)
{
TextBox txtBx = new TextBox();
Rectangle rect = this.dgvItemVendor.GetCellDisplayRectangle(Convert.ToInt32(chk.Name.Split('#')[0]), -1, false);
txtBx.Name = chk.Name.Split('#')[0] + "#-1";
txtBx.Size = new Size(rect.Width - 20, 10);
rect.Offset(0, 0);
txtBx.Location = rect.Location;
txtBx.Multiline = true;
txtBx.Height = 18;
this.dgvItemVendor.Controls.Add(txtBx);
txtBx.Focus();
txtBx.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBx_txtKeyPress);
}
}

private void txtBx_txtKeyPress(object sender, KeyPressEventArgs e)
{

cpattern = 'S';
dgvItemVendor.CurrentCell = null;
TextBox txtGetText = (TextBox)sender;
if (e.KeyChar == Convert.ToChar(Keys.Back))
{
if (txtGetText.Text != string.Empty)
{
strSearchQuery = txtGetText.Text.Substring(0, txtGetText.Text.Length - 1);
}
}
else
{
strSearchQuery = txtGetText.Text + e.KeyChar;
}
if (e.KeyChar == '')
{
if (Clipboard.ContainsText())
{
strSearchQuery = Clipboard.GetText();
}
}

if (strSearchQuery.Length > 1)
{
if (strSearchQuery.StartsWith("*"))
cpattern = 'E';
if (strSearchQuery.StartsWith("*") && strSearchQuery.EndsWith("*"))
cpattern = 'C';
}
strSearchQuery = strSearchQuery.Replace("*", string.Empty);
//if (System.Text.RegularExpressions.Regex.IsMatch(dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString()
// , strSearchQuery
// , System.Text.RegularExpressions.RegexOptions.IgnoreCase))

for (int k = 0; k < dgvItemVendor.RowCount; k++)
{
switch (cpattern)
{
case 'E':
dgvItemVendor.Rows[k].Visible = (dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString().EndsWith(strSearchQuery, true, null)) ? true : false;
break;
case 'C':
dgvItemVendor.Rows[k].Visible = (dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString().ToUpper().Contains(strSearchQuery.ToUpper())) ? true : false;
break;
default:
dgvItemVendor.Rows[k].Visible = (dgvItemVendor.Rows[k].Cells[Convert.ToInt32(txtGetText.Name.Split('#')[0])].Value.ToString().StartsWith(strSearchQuery, true, null)) ? true : false;
break;
}
}
}
catch (Exception ex)
{
CustomException.ShowCustomException(ex);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900