adnan

adnan

  • NA
  • 4
  • 4.2k

How to select data and display it in a DataGrid

Aug 6 2010 6:26 AM
Hi people, I'm new in this kind of programing and I have a problem whit my project.

I try to write the code for a action button (the find button that allow to find rows whit the specific parametrs), but i don't know how to write it in C# exactly.

In sql the code is SELECT * FROM atable WHERE ...... but how can I do it in C#


my code is:


private void btnTrazi_Click(object sender, EventArgs e)
        {
           
            try
            {       string wanted="", wanted1, wanted2, wanted3, wanted4, wanted5;
      
                    //inicijalizacija varijabli po kojima se vrsi trazenje ali bez ikakvih ne dozvoljenih znakova
                    wanted1 = txtJMBG.Text.Trim();
                    wanted2 = txtIme.Text.Trim();
                    wanted3 = txtPrezime.Text.Trim();
                    wanted4 = txtPbrStan.Text.Trim();
                    wanted5 = txtBrTel.Text.Trim();


              
                    if (wanted1 != "") 
                    {
                        katvDataSet1.korisnik.DefaultView.Sort = "JMBG";
                        int npoz = katvDataSet1.korisnik.DefaultView.Find(wanted1);

                        if (npoz == -1)
                            MessageBox.Show("Nepostoji zapis koji zadovoljava tražene parametre");
                        else
                            this.BindingContext[katvDataSet1, "korisnik"].Position = npoz;
                    }

                    else
                    {
                        if (wanted2 != "") wanted = "ime = " + wanted2;

                        if (wanted3 != "") wanted = wanted + " AND " + "prezime = " + wanted3;

                        if (wanted4 != "") wanted = wanted + " AND " + "pbrStan = " + wanted4;

                        if (wanted5 != "") wanted = wanted + " AND " + "telefon = " + wanted5;
                       
                        Select *; from korisnik; where wanted; into dataGridView1;

              }
   
                    }
                catch
                    {
                    }
          }

So if anyone can help me please do it :)

Answers (2)