mardi 12 mars 2019

Loop through all checkboxes in xaml and compose string from their content

I am looping through all checkboxes with this code.

string where = string.Empty;

        foreach (CheckBox c in gridApi.Children.OfType<CheckBox>())
        {


            if (c.IsChecked == true)
            {
                MessageBox.Show(where += "CategoryMulti like '%" + c.Content.ToString() + "%'");
            }
        }

From these checkboxes I would like to build one string (where variable) for SQL command:

select telephone from Contacts where CategoryMulti like '%1%'

Problem is that I need to have or between sql like tags like this

select telephone from Contacts where CategoryMulti like '%1%' or CategoryMulti like'%2%'

I need to tell the program how to behave with or adding. For example sql command can't end with or

select telephone from Contacts where CategoryMulti like '%1%' or CategoryMulti like'%2%' or

Because sql command would end with error. Could you help me how can I build something like that?

I had an idea to leave OR for each checked checkbox and get last checked checkbox position in array and remove last 2 characters.

This would work I guess because every checked checkbox gonna add same amount of characters into the where variable no matter what. Problem is that I have no idea how to add all checkboxes into array in this loop.. Thank you for any idea.

.xaml

    <CheckBox Content="cul" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="chkBoxCul" Margin="325,59,0,0"/>
    <CheckBox Content="inf" Visibility="Visible" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="chkBoxInfo" Margin="408,59,0,0"/>
    <CheckBox Content="spo" Visibility="Visible" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="chkBoxSp" Margin="479,56,0,0"/>
    <CheckBox Content="war" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="chkBoxWar" Margin="540,56,0,0"/>




Aucun commentaire:

Enregistrer un commentaire