0

I want to choose columns from a query with using the checkbox and Display in datagrid . In the following figure is an overview of the program :

App screenshot

and with this code for search :

Database1Entities DBE = new Database1Entities();
var search = from c in DBE.Tbl_PersonalInformation
                     where
                     string.IsNullOrEmpty(textBoxFirstName.Text) || (textBoxFirstName.Text) == (c.First_Name)
                     &&
                     string.IsNullOrEmpty(textBoxLastName.Text) || (textBoxLastName.Text) == (c.Last_Name)
                     select new { c.First_Name, c.Last_Name };
        dataGrid.ItemsSource = search.ToList();

I would, for example if checkBoxFirstName.IsChecked==true and checkBoxLastName.IsChecked==false only FirstName be displayed on the datagrid .

saeid
  • 13
  • 4
  • Possible duplicate of [Can I filter a collection from xaml?](http://stackoverflow.com/questions/4923935/can-i-filter-a-collection-from-xaml) – ASh Jun 23 '16 at 11:36

2 Answers2

0

you could pass a filter method

public object FilterMethod(Person person)
{
var condition = false; // checkBoxFirstName.IsChecked==true
if (condition)
    return new {person.Name};
else
    return new { person.Name, person.SurName};
}

and use it in the select line select FilterMethod(c);

Tom Söhne
  • 512
  • 2
  • 6
0

I am assuming from your screenshot you are using WinForms and not WPF. You can use:this.yourGridView.Columns["First_Name"].Visible = checkBoxFirstName.IsChecked==true;

To set the the visibility of the First_Name column.

see https://msdn.microsoft.com/en-us/library/0c24a0d7(v=vs.110).aspx

  • If you are using code-behind then in WPF you would do: YourDataGrid.Columns[IndexOftheColumn].Visibility = Visibility.Collapsed; if you are using MVVM and want to hide the column in the view XAML: where the type of the firstnameVisibility property is Systems.Windows.Visibility and set it to collapsed based on your checkbox value. – Pascal Charbonneau Jun 23 '16 at 12:23