Как выделить строку DataGridView или сделать ее светящейся временно? - PullRequest
16 голосов
/ 13 апреля 2011

Используя C # DataGridView, как я могу:

  1. Выделить строку
  2. Сделать строку светящейся временно (желтеть на пару секунд)

Ответы [ 5 ]

18 голосов
/ 20 апреля 2011

Чтобы смоделировать пользователя, выбирающего строку, используйте

myDataGrid.Rows[n].IsSelected = true;

, как предложил Габриэль.

Чтобы временно выделить цветом строку в элементе управления DataGridView, установите DefaultCellStyle.BackColor выберите цвет по вашему выбору для интересующей вас строки. Затем включите System.Windows.Forms.Timer элемент управления для выбранного вами периода времени.Когда срабатывает событие таймера Tick, отключите таймер и установите для строки DefaultCellStyle.BackColor исходный цвет.

Ниже приведен короткий пример приложения WinForm, в котором имеется DataGridView с именем GlowDataGrid, таймерGlowTimer и кнопка с именем GlowButton.При нажатии на GlowButton третья строка DataGridView временно светится желтым в течение двух секунд.

private void Form1_Load(object sender, EventArgs e)
    {
        // initialize datagrid with some values
        GlowDataGrid.Rows.Add(5);
        string[] names = new string[] { "Mary","James","Michael","Linda","Susan"};
        for(int i = 0; i < 5; i++)
        {
            GlowDataGrid[0, i].Value = names[i];
            GlowDataGrid[1, i].Value = i;
        }
    }

    private void GlowButton_Click(object sender, EventArgs e)
    {
        // set third row's back color to yellow
        GlowDataGrid.Rows[2].DefaultCellStyle.BackColor = Color.Yellow;
        // set glow interval to 2000 milliseconds
        GlowTimer.Interval = 2000;
        GlowTimer.Enabled = true;
    }

    private void GlowTimer_Tick(object sender, EventArgs e)
    {
        // disable timer and set the color back to white
        GlowTimer.Enabled = false;
        GlowDataGrid.Rows[2].DefaultCellStyle.BackColor = Color.White;
    }
4 голосов
/ 20 апреля 2011

Мой код для вас

    private void Form1_Load(object sender, EventArgs e)
    {
        Timer t = new Timer();
        t.Interval = 500;
        t.Enabled = false;

        dataGridView1.CellMouseEnter += (a, b) =>
        {
            if (b.RowIndex != -1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[b.RowIndex].Cells[0];
                dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionForeColor = Color.Black;
                t.Tick += (c, d) =>
                {
                    dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Blue;
                    dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionForeColor = Color.White;
                    t.Enabled = false;
                };
                t.Enabled = true;
            }
        };
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dataGridView1.Columns.Add("Col1", "Col1");
        dataGridView1.Columns.Add("Col2", "Col2");
        dataGridView1.Rows.Add("Row1", "Col1");
        dataGridView1.Rows.Add("Row1", "Col2");
        dataGridView1.Rows.Add("Row2", "Col1");
        dataGridView1.Rows.Add("Row2", "Col2");
        dataGridView1.Rows.Add("Row3", "Col1");
        dataGridView1.Rows.Add("Row3", "Col2");
        dataGridView1.Rows.Add("Row4", "Col1");
        dataGridView1.Rows.Add("Row4", "Col2");
    }
0 голосов
/ 26 апреля 2011

Используйте как

gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.Yellow

, чтобы установить цвет, тогда вам нужно будет сбросить цвета после Сетка отсортирована.

Затем с помощью таймера измените цвет подсветки после задержки.

gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.white
0 голосов
/ 13 апреля 2011

Вы можете использовать свойство GridView AutoFormat.

0 голосов
/ 13 апреля 2011

Вы можете выделить 'n' строку с помощью someDataGridView.Rows [n] .IsSelected = true;

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...