Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
try
         {
             ncol=dataGridView1.CurrentCell.ColumnIndex;
             strKey += e.KeyChar.ToString();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[ncol].Value.ToString().ToUpper().Equals(strKey.ToUpper()) || row.Cells[ncol].Value.ToString().ToUpper().Contains(strKey.ToUpper()))
                    {
                        rowIndex = row.Index;
                        colindex = row.Cells[ncol].ColumnIndex;
                        break;
                    }
                }
                if (rowIndex != -1)
                {
                    if (rowIndex >= 5)
                    {
                        if (dataGridView1.FirstDisplayedScrollingRowIndex + dataGridView1.DisplayedRowCount(false) <= rowIndex)
                        {
                            dataGridView1.FirstDisplayedScrollingRowIndex = rowIndex - dataGridView1.DisplayedRowCount(false) + 1;
                        }
                        else if (dataGridView1.FirstDisplayedScrollingRowIndex + dataGridView1.DisplayedRowCount(false) >= rowIndex)
                        {
                            dataGridView1.FirstDisplayedScrollingRowIndex = rowIndex - dataGridView1.DisplayedRowCount(false) + 1;
                        }
                    }
                    else
                    {
                        dataGridView1.FirstDisplayedScrollingRowIndex = rowIndex;
                    }

                    if (dataGridView1.SelectedRows.Count == 0)
                    {
                        dataGridView1.Rows[rowIndex].Selected = true;
                        if (dataGridView1.Rows[rowIndex].Selected == true)
                        {
                            textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
                        }

                    }
                }
Posted
Updated 18-Jan-15 17:21pm
v2
Comments
Sanket Saxena 13-Jan-15 8:08am    
insufficient information to understand the problem.
Member 11367741 18-Jan-15 23:36pm    
Hello Dear,
My question is How to do search option in grid view used key press event. Suppose we press the letter or digit cursor move to particular letter positions

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