Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
    for (int i = 0; i <(dataGridView2.Rows.Count); i++)

                {
                    if (dataGridView2.Rows[i].Cells["dt coulme name"].Value.ToString().StartsWith(typedChars, true, CultureInfo.InvariantCulture))
                    {
                        dataGridView2.Rows[i].Cells[0].Selected = true;
                        
                        return;

What I have tried:

i am trying to filter datagridview on keyboard keypress   i excute code on gridview keypress event but i cant please help me also give message when press another data which is not avilable so
Posted
Updated 30-Aug-22 1:14am
v2
Comments
Richard MacCutchan 30-Aug-22 6:12am    
What is the actual problem?
vaishali jadhav 2022 30-Aug-22 6:29am    
Actually I want to search a cell in a grid one by one when key presss so I execute upper 👆 code but when press another key like your name is Richard and I press Richards so that Time I want execute else part but it can't execute and show null exceptions errors
Richard MacCutchan 30-Aug-22 6:50am    
Well unfortunately we cannot see the code you are referring to, nor can we see any of the error messages. If you want help with your problem then you need to provide full details in your question. Please do not expect us to guess what your code is doing and what errors occur.
vaishali jadhav 2022 1-Sep-22 4:38am    
I m Trying your this code
public string typedChars = string.Empty; private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{ if(e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) { typedChars = string.Empty; return; } if (Char.IsLetter(e.KeyChar)) { typedChars += e.KeyChar.ToString();
for (int i = 0; i < (dataGridView1.Rows.Count); i++)
{
if (dataGridView1.Rows[i].Cells["Name"].Value.ToString().StartsWith(typedChars, true, CultureInfo.InvariantCulture))
{
dataGridView1.Rows[i].Cells[0].Selected = true; return;
}
}
}
}

but when i excute else part get error or i comment else part so if i type another value which is not exist ge null exception error
Richard MacCutchan 1-Sep-22 4:43am    
Please add that information (properly formatted) to your question.

1 solution

Why not just use a filter? If you use a DataTable as the DataSource for the DGV, it has a DefaultView property which supports the RowFilter property:
(MyDataGridView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Title LIKE '%{0}%'", filterString);

Then each time they type a character, add it to the filter string and set the filter
If you have a lot of rows, use a timer set to 1/4 second, and set a timeout each time they type:
     Timer updateFilter = new Timer();
     updateFilter.Interval = 250;
     updateFilter.Tick += UpdateFilter_Tick;
     updateFilter.Start();
     }

private void UpdateFilter_Tick(object sender, EventArgs e)
     {
     if (UpdateFilter >= DateTime.Now)
         {
         (ItemsDisplay.DataSource as DataTable).DefaultView.RowFilter = string.Format("Title LIKE '%{0}%'", TitleFilter.Text);
         UpdateFilter = DateTime.MinValue;
         }
     }
private void TitleFilter_TextChanged(object sender, EventArgs e)
     {
     if (sender is TextBox filter)
         {
         UpdateFilter = DateTime.Now.AddMilliseconds(500);
         }
     }
 
Share this answer
 

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