When I set a DataGridTextColumn IsVisible to False, I get an exception

5 posts, 0 answers
  1. Randy
    Randy avatar
    3 posts
    Member since:
    Nov 2014

    Posted 18 Aug Link to this post

                foreach (ColumnConfig loopColumn in sduiConfig.columns)
                {
                    DataGridTextColumn newColumn = new DataGridTextColumn();
                    newColumn.PropertyName = loopColumn.columnName;
                    newColumn.Header = loopColumn.caption;
                    // When newColumn.IsVisible is assigned 1 (true) it works, but 0 (false) throws an exception: System.NullReferenceException
                    newColumn.IsVisible = loopColumn.visible;
                    newColumn.CanUserEdit = false;
                    scheduleDataGrid.Columns.Add(newColumn);
                }

  2. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    138 posts

    Posted 18 Aug Link to this post

    Hi Randy,

    The DataGridTextColumn.IsVisible property is a boolean, you'll need to convert your int value before setting IsVisible. You could just use a small expression to return true or false. 

    For example, if your values for loopColumn.visible are always 1 or 0, this would work:

    newColumn.IsVisible = loopColumn.Visible == 1;

    Or to be a bit more precise, the IsVisible default value is true, so you only need to do this:

    if (loopColumn.Visible == 0)
       newColumn.IsVisible = false;


    There are a number of ways you can approach this, but as long you're getting a boolean result it will work to set the columns' visibility.

    If you have any further questions, please lets us know. Thank you for contacting Support and for choosing Telerik by Progress.


    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Randy
    Randy avatar
    3 posts
    Member since:
    Nov 2014

    Posted 18 Aug in reply to Lance | Tech Support Engineer, Sr. Link to this post

    When I try:  newColumn.IsVisible = loopColumn.visible == 1;  or  if (loopColumn.visible == 0)

    I get:  Operator '==' cannot be applied to operands of type 'bool' and 'int'

  5. Randy
    Randy avatar
    3 posts
    Member since:
    Nov 2014

    Posted 18 Aug in reply to Lance | Tech Support Engineer, Sr. Link to this post

    In a solution of your own, please attempt to set IsVisible to false (newColumn.IsVisible = false;) and see if you get the same error.
  6. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    138 posts

    Posted 18 Aug Link to this post

    Hello Randy,

    The IsVisible property itself works as expected, I've attached a demo app that toggles the visibility of all the columns. Here is the code:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <grid:RadDataGrid x:Name="MyDataGrid"/>
     
            <Button Content="Toggle Column Visibility"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Click="ToggleVisibilityButton_OnClick"/>
        </Grid>

    and the code behind:

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                Loaded += MainPage_Loaded;
            }
     
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                MyDataGrid.ItemsSource = GetData();
            }
     
            private void ToggleVisibilityButton_OnClick(object sender, RoutedEventArgs e)
            {
                foreach (var column in MyDataGrid.Columns)
                {
                    //just iterating over the columns and toggling their visibility
                    column.IsVisible = !column.IsVisible;
                }
            }
     
            private static ObservableCollection<Data> GetData() => new ObservableCollection<Data>
            {
                new Data {Country = "India", Capital = "New Delhi", Visible = 1},
                new Data {Country = "South Africa", Capital = "Cape Town", Visible = 0},
                new Data {Country = "Nigeria", Capital = "Abuja", Visible = 0},
                new Data {Country = "Singapore", Capital = "Singapore", Visible = 1}
            };
        }
     
        public class Data
        {
            public string Country { get; set; }
            public string Capital { get; set; }
            public int Visible { get; set; }
        }



    As far as the error you got, it's because you directly applied my psudeo code directly in your app. Because you didn't share exactly what your class is (and it's properties), I took your definition of "1" and "0" as an int and wrote the code as illustrative point. 

    You need to know what object  ColumnConfig.Visible is and convert it accordingly, however that last error tells me that it's already a bool. You state that you're setting it with a "1" and a "0" and the original exception is not being thrown in the Telerik components, it's happening somewhere else, I suspect its because you're trying to use an int where it should be a bool.

    Ultimately the problem is unrelated to the RadDataGrid, rather it's due to how you are setting your value types,  int and bool are not converted implicitly. You must convert int to bool or bool to int where appropriate.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner