M
M
Maxim Vlasyuk2018-12-26 07:15:09
MySQL
Maxim Vlasyuk, 2018-12-26 07:15:09

How to keep selected row color after updating data from Mysql database?

Hello, I ask for your help. There is this C# code:

Dictionary<int, Color> colorsList = new Dictionary<int, Color>();
        private void toolStripButton5_Click(object sender, EventArgs e)//ИЗМЕНЕНИЕ ЦВЕТА СТРОКИ
        {

            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Selected == true)
                {
                    if (toolStripComboBox1.Text == "Красный")
                        

                    {

                        listView1.Items[i].BackColor = Color.Red;
                        if (!colorsList.ContainsKey(i)) { colorsList.Add(i, Color.Red); }
                        else { colorsList[i] = Color.Red; }
                    }

                    else if (toolStripComboBox1.Text == "Желтый")

                    {

                        listView1.Items[i].BackColor = Color.Yellow;
                        if (!colorsList.ContainsKey(i)) { colorsList.Add(i, Color.Yellow); }
                        else { colorsList[i] = Color.Yellow; }
                    }

                    else if (toolStripComboBox1.Text == "Зеленый")

                    {

                        listView1.Items[i].BackColor = Color.Green;
                        if (!colorsList.ContainsKey(i)) { colorsList.Add(i, Color.Green); }
                        else { colorsList[i] = Color.Green; }

                    }

                    else if (toolStripComboBox1.Text == "Без цвета")

                    {

                        listView1.Items[i].BackColor = Color.White;
                        if (!colorsList.ContainsKey(i)) { colorsList.Add(i, Color.White); }
                        else { colorsList[i] = Color.White; }
                    }
                }
            }
        }

        void SetColor()
        {

            foreach (var c in colorsList)
            {
                if (listView1.Items.Count >= c.Key)
                    listView1.Items[c.Key].BackColor = c.Value;
            }
        }

The selected row in the listview is highlighted in color, but after updating the data from mysql to the form, the color is reset accordingly. Can you tell me how to make the listview rows remember the color?
The update code is simple on the button:
private async void toolStripButton4_Click(object sender, EventArgs e)//ОБНОВЛЕНИЕ ДАННЫХ
        {
            listView1.Items.Clear();
            await LoadBanksAsync();
        }

I'm not a programmer myself, sis. admin. I have a database, but the employees store it just in excel on a Google drive for general use, and this makes me a little hover. Here I sit, I understand, then there is a video, then there is a forum. This is me to the fact that do not judge strictly, comrades!)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Vlasyuk, 2018-12-28
@Over8000

up

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question