Share this question

Welcome to Teachnovice Q&A, where you can ask questions and receive answers from other members of the community.

This is a collaboratively edited question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

RadGrid with custom filters

0 like 0 dislike
6,996 views
What I'm trying to achieve is to use custom made filters with controls like DropDownList, TextBox and CheckBox on RadGrid that uses NeedDataSource event to bind the data.

What I did in my previous attempt was something like this:

Filter is working fine and it uses the following code to filter out the RadGrid:

[source language='c#']

protected void btnSearch_Click(object sender, EventArgs e)
    {
        var kontakti = from k in db.Kontakt
                       select k;
 
        int idTipUsera = Convert.ToInt32(rcbTipUsera.SelectedValue);
        int idTvrtka = Convert.ToInt32(rcbTvrtka.SelectedValue);
 
        if (rcbTvrtka.SelectedValue != "0")
        {
            kontakti = kontakti.Where(k => k.idFirma == idTvrtka);
        }
 
        if (rcbTipUsera.SelectedValue != "0")
        {
            kontakti = kontakti.Where(k => k.idOvlasti == idTipUsera);
        }
 
        if (chkAktivan.Checked == true)
        {
            kontakti = kontakti.Where(k => k.Aktivan == true);
        }
        else
        {
            kontakti = kontakti.Where(k => k.Aktivan == false);
        }
 
        int idKontakt = Convert.ToInt32(Request.QueryString["idk"]);
        int idAuthKontakt = Convert.ToInt32(Session["authenticatedUI"]);
 
        if (idKontakt > 0 && idAuthKontakt == idKontakt)
        {
            gvKontakti.DataSource = from k in kontakti
                                    where k.idKontakt == idKontakt
                                    orderby k.Prezime, k.Ime
                                    select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
        }
        else if (idKontakt > 0 && idAuthKontakt != idKontakt)
        {
            gvKontakti.DataSource = from k in kontakti
                                    where k.idKontakt == idKontakt
                                    orderby k.Prezime, k.Ime
                                    select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
        }
        else
        {
            gvKontakti.DataSource = from k in kontakti
                                    orderby k.Prezime, k.Ime
                                    select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
        }
 
        gvKontakti.DataBind();
    }
 

[/source]

The problem with the code mentioned is that whenever I try to click "Detalji" (which is actually standard Edit button) wrong record gets selected. I assume that problem is in Edit button which, as I believe, does the postback which causes the NeedDataSource event to get triggered and loads the default DataSource which then calls the index of the previously selected row.

My question is how to fix this or what is the alternative?
asked Jan 4, 2012 by anonymous  

1 Answer

0 like 0 dislike
Ok, I've found the solution!

What I did is to use the Session which will help us to determine later if the filtered RadGrid DataSource was initiated or it was the default one.

[source language=c#]

protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["SearchKontakti"] = "1";
    }
[/source]

After that I had to set PreRender with if loop to check for previously mentioned Session.

[source langauge='c#']

protected void gvKontakti_PreRender(object sender, EventArgs e)
    {
        int idKontakt = Convert.ToInt32(Request.QueryString["idk"]);
 
        if (Session["SearchKontakti"] == "1")
        {
            var kontakti = from k in db.Kontakt
                           select k;
 
            int idTipUsera = Convert.ToInt32(rcbTipUsera.SelectedValue);
            int idTvrtka = Convert.ToInt32(rcbTvrtka.SelectedValue);
 
            if (rcbTvrtka.SelectedValue != "0")
            {
                kontakti = kontakti.Where(k => k.idFirma == idTvrtka);
            }
 
            if (rcbTipUsera.SelectedValue != "0")
            {
                kontakti = kontakti.Where(k => k.idOvlasti == idTipUsera);
            }
 
            if (chkAktivan.Checked == true)
            {
                kontakti = kontakti.Where(k => k.Aktivan == true);
            }
            else
            {
                kontakti = kontakti.Where(k => k.Aktivan == false);
            }
 
            int idAuthKontakt = Convert.ToInt32(Session["authenticatedUI"]);
 
            if (idKontakt > 0 && idAuthKontakt == idKontakt)
            {
                gvKontakti.DataSource = from k in kontakti
                                        where k.idKontakt == idKontakt
                                        orderby k.Prezime, k.Ime
                                        select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
            }
            else if (idKontakt > 0 && idAuthKontakt != idKontakt)
            {
                gvKontakti.DataSource = from k in kontakti
                                        where k.idKontakt == idKontakt
                                        orderby k.Prezime, k.Ime
                                        select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
            }
            else
            {
                gvKontakti.DataSource = from k in kontakti
                                        orderby k.Prezime, k.Ime
                                        select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
            }
 
            gvKontakti.DataBind();
        }
    }
[/source]

One last thing I had to do was to reset Session on reset Button but that is trivial.
answered Jan 4, 2012 by anonymous  
...