Selecting Row on a Datagrid from a query. C#

Asked

Viewed 1,127 times

2

Good afternoon, you guys, Someone has a code where I can select a Row in a DataGrid, from a survey ?

Where I place mine Button_Click to a TextBox with a certain value, searching DataGridView, thus selecting the Row concerning that value of TextBox.

  • As I understand it, you have a button, and a textbox, where you enter with some value, and when you click the button you want it to go through the datagrid and find the line, for example paint it red and that? be more specific in your question, if possible add prints, and part of the code

  • @Thomaserichpimentel just like that. A command that searches Datagrid for a line with the value I determined, and when the command finds it, select this beautiful one.

  • 1

    I think the @zekk answer has solved your problem ne?

  • Running perfectly !

1 answer

4


You can go through the rows of DataGrid and compare the values with the text of TextBox.

string pesquisar = textBox1.Text.ToLower();

dataGridView1.ClearSelection();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (row.Cells["NomeDaColuna"].Value != null)
    {
        if (row.Cells["NomeDaColuna"].Value.ToString().ToLower().Equals(pesquisar))
        {
            row.Selected = true;
            break;
        }
    }
}
  • Dude, this command is working partially when the execution comes in the 2nd if to value the int rowIndex, it does not enter the key.

  • @Mauríciosanches Did you put the name of the correct column? something else, the text of textbox has to be exact.

  • OK! is correct now, the problem was I was not considering Uppers and Minuscules. is running right now.

  • 1

    @Mauritosanches You can use the method ToLower in both strings and compare them, on the second problem, before doing the research, use: DataGridView1.ClearSelection();.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.