-2

I was wondering if anyone had an example of pulling a csv list of options from the database and checking the rows based on the text value pulled. So say I have a grid view with options showing various programming languages. I would like to match those languages to the ones that have been saved in the Database.

O    ASP.NET
O    C#
O    VB.NET
...

So, if my returned list of languages is just ASP.NET and VB.NET, how do I get the checkboxes of those grid view rows to be checked? I could use View State but I am thinking more a data-based set of information by stepping through the recordset and checking the items based on the returned dataset.

bbcompent1
  • 494
  • 1
  • 10
  • 25
  • @FLICKER The reason I marked it as SQL is because I am pulling the list from SQL Server via a query. If that was wrong, I apologize. :) – bbcompent1 Dec 29 '17 at 20:15
  • it's okay. remember that by using tags, you are choosing the audience of your question, so having better tags helps you target the right persons to answer your question. No SQL knowledge is needed to answer your question and that's why I removed the tag. – FLICKER Dec 29 '17 at 20:41
  • Gotcha, that makes sense. I am already grabbing the data via the sql query so that's cool :) – bbcompent1 Dec 29 '17 at 21:21
  • Would you elaborate a bit more on how your `Gridview` & table be like? – Circle Hsiao Dec 30 '17 at 06:43
  • Ok, i meant to say a list view control. The value abd text are the same, value is stored in hidden field in same one as the check box. The other field is a label. The data set is a csv string returned from the database query. Fur example, ASP..NET, c#, VB.NET If the hidden field value is a match, check the check box. – bbcompent1 Dec 30 '17 at 16:08
  • I did figure it out so I'll post the answer to help others. – bbcompent1 Jan 03 '18 at 15:02

1 Answers1

1

Surprised I was able to figure this one out but suddenly the answer was staring me in the face.

        protected void SetLangs()
    {
        List<string> sellangs = new List<string>();
        string langs = hfPrgLangs.Value;
        string langtrim = langs.Replace(" ", "");
        sellangs = langtrim.Split(',').ToList<string>();
        foreach (DataListItem dl in dlLanguages.Items)
        {
            Label lblLangName = (dl.FindControl("lblLangName") as Label);
            CheckBox isChk = (dl.FindControl("cbLang") as CheckBox);
            for (int i = 0; i < sellangs.Count; i++)
            {
                if (sellangs[i].ToString() == lblLangName.Text.ToString())
                {
                    isChk.Checked = true;
                }
            }
        }
    }
bbcompent1
  • 494
  • 1
  • 10
  • 25