IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Diskuze: vymazanie riadku v datagriedview

V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.

Aktivity
Avatar
František Pastorek:29.8.2015 17:15

Dobrý deň.

V datagriedview si postupne vytváram riadky. Keď dosiahnem určitý počet tak som chcel nastaviť, že prvý, pre mňa už nepodstatný sa vymaže. Pomocou tlačítka to ide bez problémov. Ale automaticky to stále hlási chybu a neviem čo s tým. Na nete riešili viacmenej iba prípad kedy sa vymažú označené riadky. Aj to mi ide. A preto nechápem prečo to nejde automaticky.

Prikladám časť kódu + problémová časť kódu:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows.Count > 15)
            {
                dataGridView1.Rows.RemoveAt(0);
            }

            int rowcount = dataGridView1.Rows.Count;

            if (rowcount > 6)
            {
                for (int i = 6; i < rowcount; i++)
                {
                    dataGridView1.Rows[i].Cells["b1"].Value = Convert.ToInt32(dataGridView1.Rows[i - 6].Cells["a1"].Value) + 6;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["b1"].Value) > 12)
                        dataGridView1.Rows[i].Cells["b1"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["b1"].Value) - 12;
                    dataGridView1.Rows[i].Cells["b1"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["b2"].Value = Convert.ToInt32(dataGridView1.Rows[i - 5].Cells["a1"].Value) + 5;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["b2"].Value) > 12)
                        dataGridView1.Rows[i].Cells["b2"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["b2"].Value) - 12;
                    dataGridView1.Rows[i].Cells["b2"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["b3"].Value = Convert.ToInt32(dataGridView1.Rows[i - 4].Cells["a1"].Value) + 4;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["b3"].Value) > 12)
                        dataGridView1.Rows[i].Cells["b3"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["b3"].Value) - 12;
                    dataGridView1.Rows[i].Cells["b3"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["b4"].Value = Convert.ToInt32(dataGridView1.Rows[i - 3].Cells["a1"].Value) + 3;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["b4"].Value) > 12)
                        dataGridView1.Rows[i].Cells["b4"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["b4"].Value) - 12;
                    dataGridView1.Rows[i].Cells["b4"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["b5"].Value = Convert.ToInt32(dataGridView1.Rows[i - 2].Cells["a1"].Value) + 2;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["b5"].Value) > 12)
                        dataGridView1.Rows[i].Cells["b5"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["b5"].Value) - 12;
                    dataGridView1.Rows[i].Cells["b5"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["b6"].Value = Convert.ToInt32(dataGridView1.Rows[i - 1].Cells["a1"].Value) + 1;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["b6"].Value) > 12)
                        dataGridView1.Rows[i].Cells["b6"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["b6"].Value) - 12;
                    dataGridView1.Rows[i].Cells["b6"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["c1"].Value = Convert.ToInt32(dataGridView1.Rows[i - 6].Cells["a1"].Value) - 6;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["c1"].Value) < 0)
                        dataGridView1.Rows[i].Cells["c1"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["c1"].Value) + 12;
                    dataGridView1.Rows[i].Cells["c1"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["c2"].Value = Convert.ToInt32(dataGridView1.Rows[i - 5].Cells["a1"].Value) - 5;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["c2"].Value) < 0)
                        dataGridView1.Rows[i].Cells["c2"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["c2"].Value) + 12;
                    dataGridView1.Rows[i].Cells["c2"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["c3"].Value = Convert.ToInt32(dataGridView1.Rows[i - 4].Cells["a1"].Value) - 4;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["c3"].Value) < 0)
                        dataGridView1.Rows[i].Cells["c3"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["c3"].Value) + 12;
                    dataGridView1.Rows[i].Cells["c3"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["c4"].Value = Convert.ToInt32(dataGridView1.Rows[i - 3].Cells["a1"].Value) - 3;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["c4"].Value) < 0)
                        dataGridView1.Rows[i].Cells["c4"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["c4"].Value) + 12;
                    dataGridView1.Rows[i].Cells["c4"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["c5"].Value = Convert.ToInt32(dataGridView1.Rows[i - 2].Cells["a1"].Value) - 2;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["c5"].Value) < 0)
                        dataGridView1.Rows[i].Cells["c5"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["c5"].Value) + 12;
                    dataGridView1.Rows[i].Cells["c5"].Style.BackColor = Color.Green;

                    dataGridView1.Rows[i].Cells["c6"].Value = Convert.ToInt32(dataGridView1.Rows[i - 1].Cells["a1"].Value) - 1;
                    if (Convert.ToInt32(dataGridView1.Rows[i].Cells["c6"].Value) < 0)
                        dataGridView1.Rows[i].Cells["c6"].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells["c6"].Value) + 12;
                    dataGridView1.Rows[i].Cells["c6"].Style.BackColor = Color.Green;
                }
            }
        }

Problémová časť:

dataGridView1.Rows.RemoveAt(0);

Hlásená chyba:
An unhandled exception of type 'System.Invali­dOperationExcep­tion' occured in System.Window­s.Forms.dll
Additional information: Operation cannot be performed in this event handler.

Neviem prečo mu tak moc vadí event handler, v ktorom ho používam.
Ak problémovú časť použijem napríklad po stlačený tlačidla tak bez problémov.

Vopred ďakujem za skorú odpoveď.

 
Odpovědět
29.8.2015 17:15
Avatar
Eldan
Člen
Avatar
Eldan:30.8.2015 11:14

Teoreticky bys mohl prostě všechno posunout o jeden řádek nahoru a první řádek nechat přepsat :)
Bude to pomalejší a určitě to není good practice, ale tvůj problém to vyřeší :D

Nahoru Odpovědět
30.8.2015 11:14
Unobfuscated executable == free source code
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 2 zpráv z 2.