A
A
Andrew Kolomiets2020-12-14 23:47:23
C++ / C#
Andrew Kolomiets, 2020-12-14 23:47:23

Why isn't the right Item selected from comboBox1 when I click on the button?

private void Form4_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Коломиец Андрей Михайлович");
            comboBox1.Items.Add("Чкалов Павел Сергеевич");
            comboBox1.Items.Add("Шумаков Олег Константинович");
            comboBox1.Items.Add("Костров Олег Никитич");
            comboBox1.Items.Add("Варашилов Никита Игоревич");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1.Rows[1].Selected = false;
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    if (dataGridView1.Rows[i].Cells[j].Value != null)
                        if (dataGridView1.Rows[i].Cells[j].Value.ToString().Contains(comboBox1.Text))
                        {
                            dataGridView1.Rows[i].Selected = true;
                            break;
                        }
            }
            dataGridView1.DataSource = controller.UpdataSorudniki();
        }
5fd7cdf5348c4102243867.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2020-12-15
@yarosroman

not an expert on winforms, but it seems to me that it's all about the line

dataGridView1.DataSource = controller.UpdataSorudniki();

You update the data in the table and the selection is reset.

V
Vladislav, 2020-12-14
@Jewish_Cat

In the DataGrid parameters there is a parameter responsible for the type of selection: "Single cell", "Entire row", etc.

S
soloveid, 2020-12-15
@soloveid

dataGridView1.Rows[1].Selected = false;
There must be at least an error here.
dataGridView1.Rows[i].Selected = false;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question