private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
List<Employee> list = new List<Employee>();
list.Add(new Employee() { Name = "Nancy", City = "Tokyo", Phone = "000-88888888", Age = 30, IsMale = false });
list.Add(new Employee() { Name = "Janet", City = "Sendai", Phone = "000-88888888", Age = 20, IsMale = true });
list.Add(new Employee() { Name = "Rose", City = "Tokyo", Phone = "000-88888888", Age = 40, IsMale = false });
list.Add(new Employee() { Name = "Buchanan", City = "Osaka", Phone = "000-88888888", Age = 20, IsMale = true });
list.Add(new Employee() { Name = "Leverling", City = "Tokyo", Phone = "000-88888888", Age = 30, IsMale = false });
list.Add(new Employee() { Name = "Gavin", City = "Nagoya", Phone = "000-88888888", Age = 40, IsMale = true });
list.Add(new Employee() { Name = "Apple", City = "Osaka", Phone = "000-88888888", Age = 20, IsMale = false });
list.Add(new Employee() { Name = "Tim", City = "Nara", Phone = "000-88888888", Age = 30, IsMale = true });
gcSpreadGrid1.AutoGenerateColumns = false;
gcSpreadGrid1.ItemsSource = list;
gcSpreadGrid1.ColumnCount = 3;
PropertyDataField dataField1 = new PropertyDataField();
dataField1.Property = "Name";
gcSpreadGrid1.Columns[0].DataField = dataField1;
BindingDataField dataField2 = new BindingDataField();
Binding binding = new Binding();
binding.Path = new PropertyPath("City");
dataField2.Binding = binding;
gcSpreadGrid1.Columns[1].DataField = dataField2;
PropertyDataField dataField3 = new PropertyDataField();
dataField3.Property = "IsMale";
dataField3.Converter = new IsMaleConverter();
gcSpreadGrid1.Columns[2].DataField = dataField3;
}
class IsMaleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
{
throw new InvalidOperationException("Value is invalid.");
}
return ((bool)value) ? "Male" : "Female";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = value as string;
if (str == null)
{
throw new InvalidOperationException("Value is invalid");
}
if (string.Equals(str, "Male", StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
else if (string.Equals(str, "Female", StringComparison.CurrentCultureIgnoreCase))
{
return false;
}
throw new ArgumentException("Value is not valid");
}
}
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs)
Dim list As New List(Of Employee)()
list.Add(New Employee() With { _
.Name = "Nancy", _
.City = "Tokyo", _
.Phone = "000-88888888", _
.Age = 30, _
.IsMale = False _
})
list.Add(New Employee() With { _
.Name = "Janet", _
.City = "Sendai", _
.Phone = "000-88888888", _
.Age = 20, _
.IsMale = True _
})
list.Add(New Employee() With { _
.Name = "Rose", _
.City = "Tokyo", _
.Phone = "000-88888888", _
.Age = 40, _
.IsMale = False _
})
list.Add(New Employee() With { _
.Name = "Buchanan", _
.City = "Osaka", _
.Phone = "000-88888888", _
.Age = 20, _
.IsMale = True _
})
list.Add(New Employee() With { _
.Name = "Leverling", _
.City = "Tokyo", _
.Phone = "000-88888888", _
.Age = 30, _
.IsMale = False _
})
list.Add(New Employee() With { _
.Name = "Gavin", _
.City = "Nagoya", _
.Phone = "000-88888888", _
.Age = 40, _
.IsMale = True _
})
list.Add(New Employee() With { _
.Name = "Apple", _
.City = "Osaka", _
.Phone = "010-88888888", _
.Age = 20, _
.IsMale = False _
})
list.Add(New Employee() With { _
.Name = "Tim", _
.City = "Nara", _
.Phone = "010-88888888", _
.Age = 30, _
.IsMale = True _
})
gcSpreadGrid1.AutoGenerateColumns = False
gcSpreadGrid1.ItemsSource = list
gcSpreadGrid1.ColumnCount = 3
Dim dataField1 As New PropertyDataField()
dataField1.[Property] = "Name"
gcSpreadGrid1.Columns(0).DataField = dataField1
Dim dataField2 As New BindingDataField()
Dim binding As New Binding()
binding.Path = New PropertyPath("City")
dataField2.Binding = binding
gcSpreadGrid1.Columns(1).DataField = dataField2
Dim dataField3 As New PropertyDataField()
dataField3.[Property] = "IsMale"
dataField3.Converter = New IsMaleConverter()
gcSpreadGrid1.Columns(2).DataField = dataField3
End Sub
Private Class IsMaleConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
If Not (TypeOf value Is Boolean) Then
Throw New InvalidOperationException("Value is invalid.")
End If
Return If(CBool(value), "Male", "Female")
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Dim str As String = TryCast(value, String)
If str Is Nothing Then
Throw New InvalidOperationException("Value is invalid")
End If
If String.Equals(str, "Male", StringComparison.CurrentCultureIgnoreCase) Then
Return True
ElseIf String.Equals(str, "Female", StringComparison.CurrentCultureIgnoreCase) Then
Return False
End If
Throw New ArgumentException("Value is not valid")
End Function
End Class