Zablokowanie dodania nowej osoby, jeżeli już taka istnieje

0

Potrzebuje dodać do tego przycisku funkcję, która sprawdzi dataGridView pod względem tego, czy istnieje już taka osoba z takim samym numerem PESEL.

        private void bDodaj_Click(object sender, EventArgs e)
        {
            
            int n = dataGridView1.Rows.Add();
            dataGridView1.Rows[n].Cells[0].Value = tbImie.Text;
            dataGridView1.Rows[n].Cells[1].Value = tbNazwisko.Text;
            dataGridView1.Rows[n].Cells[2].Value = tbPesel.Text;
            dataGridView1.Rows[n].Cells[3].Value = tbMiejscowosc.Text;
            dataGridView1.Rows[n].Cells[4].Value = tbUlica.Text;
            dataGridView1.Rows[n].Cells[5].Value = tbNrDomu.Text;
        }

Jeżeli istnieje to wyrzuci komunikat, że taka osoba już jest i zablokuje dodanie tej osoby.

0
private void bDodaj_Click(object sender, EventArgs e)
        {
            var isExisting = dataGridView1.Rows.Any(row => row.Cells[2].Value == tbPesel.Text)
            if (!isExisting)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = tbImie.Text;
                dataGridView1.Rows[n].Cells[1].Value = tbNazwisko.Text;
                dataGridView1.Rows[n].Cells[2].Value = tbPesel.Text;
                dataGridView1.Rows[n].Cells[3].Value = tbMiejscowosc.Text;
                dataGridView1.Rows[n].Cells[4].Value = tbUlica.Text;
                dataGridView1.Rows[n].Cells[5].Value = tbNrDomu.Text;            
            }
            else
            {
                //tutaj obsluz gdy juz istnieje (messagebox czy co tam chcesz pokazac, gdy dany uzytkownik istnieje)
            }
        }
0
fasadin napisał(a):
private void bDodaj_Click(object sender, EventArgs e)
        {
            var isExisting = dataGridView1.Rows.Where(row => row.Cells[2].Value == tbPesel.Text).ToList();
            if (!isExisting)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = tbImie.Text;
                dataGridView1.Rows[n].Cells[1].Value = tbNazwisko.Text;
                dataGridView1.Rows[n].Cells[2].Value = tbPesel.Text;
                dataGridView1.Rows[n].Cells[3].Value = tbMiejscowosc.Text;
                dataGridView1.Rows[n].Cells[4].Value = tbUlica.Text;
                dataGridView1.Rows[n].Cells[5].Value = tbNrDomu.Text;            
            }
            else
            {
                //tutaj obsluz gdy juz istnieje (messagebox czy co tam chcesz pokazac, gdy dany uzytkownik istnieje)
            }
        }

Error CS1061 'DataGridViewRowCollection' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'DataGridViewRowCollection' could be found (are you missing a using directive or an assembly reference?)

0

Wiesz, programowanie nie polega na losowemu probowaniu. Przeczytales w ogole blad? Wiec przetlumaczmy

DataGridViewRowCollection' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'DataGridViewRowCollection' could be found (are you missing a using directive or an assembly reference?)
DataGridViewRowCollection nie posiada definicji dla `Where` oraz nie ma zadnego rozszerzenia (extension) Where ktory aceptuje pierwszy argument `DataGridViewRowCollection`. (Moze Ci brakuje dyrektywy using albo referencji do bibloteki?)

Wiec mozesz zrobic kilka rzeczy

  1. wygooglowac
    https://www.google.nl/search?q=does+not+contain+a+definition+for+%27Where%27+and+no+extension+method+%27Where%27+accepting+a+first+argument&rlz=1C1GCEA_enNL771NL771&oq=does+not+contain+a+definition+for+%27Where%27+and+no+extension+method+%27Where%27+accepting+a+first+argument&aqs=chrome..69i57j0.243j0j7&sourceid=chrome&ie=UTF-8
  2. nacisnac ctrl + . i zobaczyc podpowiedzi rozwiazania problemu (i tak bedziesz miec, using namespace system.linq)
  3. poczytac dokumentacje na temat where https://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx i zobaczyc ze jest w namespace System.Linq

jezeli nadal nie wiesz jak rozwiazac problem. Wroc do ksiazki/tutoriala. Jezeli jest to ksiazka/tutorial to zmien go

0

@fasadin: chyba chciałeś użyć tam Any()?

@Basstard

var any = dataGridView1.Rows.Cast<DataGridViewRow>().Any(viewRow => (int)viewRow.Cells[2].Value == 5); // czy id jest równe 5

Coś takiego to będzie :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1