"Object Reference is Not set to an instacne of an object"
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
BuildSampleGrid();
this.sampleGrid.ItemsSource = SampleGridItems;
}
/// <summary>
/// Gets the Sample Grid Items
/// </summary>
public ObservableCollection<SampleGridItem> SampleGridItems { get; private set; }
private void BuildSampleGrid()
{
this.SampleGridItems = new ObservableCollection<SampleGridItem>();
this.SampleGridItems.Add(new SampleGridItem("John Smith", "123 Main St.","Deepak_agarwal@priyanet.com"));
this.SampleGridItems.Add(new SampleGridItem("Mary Jones", "4050 N. Mountain Dr.","Deepak.agarwal1984@hotmail.com"));
this.SampleGridItems.Add(new SampleGridItem("April Billings", "1 Hollywood Blvd.",string.Empty));
this.SampleGridItems.Add(new SampleGridItem("Robert Basket", "98 Southern Pine Rd.",null));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string extension = string.Empty;
ExportFormat format = ExportFormat.Csv;
extension = "csv";
SaveFileDialog dialog = new SaveFileDialog()
{
DefaultExt = extension,
Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Csv"),
FilterIndex = 1
};
if (dialog.ShowDialog() == true)
{
using (Stream stream = dialog.OpenFile())
{
sampleGrid.Export(
stream,
new GridViewExportOptions() { Format = format, ShowColumnHeaders = true, });
}
}
}
}
public class SampleGridItem
{
/// <summary>
/// Initializes a new instance of the <see cref="SampleGridItem"/> class.
/// </summary>
public SampleGridItem()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SampleGridItem"/> class.
/// </summary>
/// <param name="name">
/// name as String
/// </param>
/// <param name="address">
/// Address as String
/// </param>
public SampleGridItem(string name, string address,string eMail)
{
this.Name = name;
this.Address = address;
this.Email = eMail;
}
/// <summary>
/// Gets the Name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets the Address
/// </summary>
public string Address { get; set; }
/// <summary>
/// Gets the Address
/// </summary>
public string Email { get; set; }
}
XAML Code:
<Grid x:Name="LayoutRoot" Background="White">
<telerikGrid:RadGridView x:Name="sampleGrid" HorizontalAlignment="Left"
AutoGenerateColumns="False" IsReadOnly="True" CanUserDeleteRows="False" CanUserInsertRows="False"
ShowGroupPanel="False" GridLinesVisibility="Horizontal" Width="341" CanUserFreezeColumns="False"
IsFilteringAllowed="False" ItemsSource="{Binding SampleGridItems}"
FontFamily="Arial"
FontSize="12"
Foreground="Green"
Background="{StaticResource ItemBackground}"
AlternationCount="2"
AlternateRowBackground="Blue" OpacityMask="#FFC0CC58" RowStyle="{StaticResource GridViewRowStyle1}" Margin="0,0,0,181">
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name"/>
<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Address}" Header="Address"/>
<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Email}" Header="Email"/>
</telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>
<Button Content="Export" Height="50" Width="50" Click="Button_Click" Margin="347,1,3,249"></Button>
</Grid>