This question is locked. New answers and comments are not allowed.
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);
}
{
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);
}
4 Answers, 1 is accepted
0
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:
Or to be a bit more precise, the IsVisible default value is true, so you only need to do this:
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
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
0
Randy
Top achievements
Rank 1
answered on 18 Aug 2016, 05:50 PM
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'
0
Randy
Top achievements
Rank 1
answered on 18 Aug 2016, 06:46 PM
In a solution of your own, please attempt to set IsVisible to false (newColumn.IsVisible = false;) and see if you get the same error.
0
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:
and the code behind:
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
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