This is a migrated thread and some comments may be shown as answers.

Dynamic Column Binding

1 Answer 96 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Madhan
Top achievements
Rank 1
Madhan asked on 11 Dec 2012, 06:04 PM
Hi,

I am using the demo from the following link.http://blogs.telerik.com/vladimirenchev/posts/11-09-28/dynamic-binding-for-your-silverlight-applications.aspx with silverlight 4 and VS2010Sp1. Telerik gridview version is (2011.1.315.1040)

I get a blank grid when trying to bind the columns to the grid without Header and Rows. If the collection returns n number rows then the grid shows n number of blank rows without header. Please let me know if there is any issue with this code.

my XAML:

 

 

 

 

<telerik:RadGridView x:Name="RadGridView1" Margin="10,12,10,0"

 

 

 

RowIndicatorVisibility="Collapsed" CanUserFreezeColumns="False"

 

 

 

ItemsSource="{Binding Path=Data}" Foreground="Black" Width="864" />

 

 
Here Data is observableCollection<MyDataRow> in Viewmodel

 

 

 

 

 

 

 

private ObservableCollection<MyDataRow> _exceldata;

 

 

 

public ObservableCollection<MyDataRow> Exceldata

 

{

 

 

get {return _exceldata;}

 

 

 

set{_exceldata = value;

 

RaisePropertyChanged(

 

"Exceldata");

 

}

}

 

 

 

I am grtting the Grid data using

 

Data = GetGridData(UploadProcessData,UploadTypeId);

 

 



 

 

 

 

private ObservableCollection<MyDataRow> GetGridData(List<UploadDetail> lst)

 

{

 

 

var data = new ObservableCollection<MyDataRow>();

 

 

 

var string1 = "Company";

 

 

 

var string2 = "ChannelID";

 

 

 

foreach (UploadDetail memberInfo in lst)

 

{

 

 

MyDataRow row = new MyDataRow();

 

row.ID = memberInfo.Col_1;

row.Name = memberInfo.Col_2;

 

data.Add(row);

}

 

 

return data;

 

}

 

 



MYDataRow Class:

 

public

 

 

class MyDataRow : DynamicObject, INotifyPropertyChanged

 

 

 

 

 

 

{

 

 

IDictionary<string, object> data;

 

 

 

public MyDataRow()

 

{

data =

 

new Dictionary<string, object>();

 

}

 

 

public MyDataRow(IDictionary<string, object> source)

 

{

data = source;

}

 

 

public override IEnumerable<string> GetDynamicMemberNames()

 

{

 

 

return data.Keys;

 

}

 

 

public override bool TryGetMember(GetMemberBinder binder, out object result)

 

{

result =

 

this[binder.Name];

 

 

 

return true;

 

}

 

 

public override bool TrySetMember(SetMemberBinder binder, object value)

 

{

 

 

this[binder.Name] = value;

 

 

 

return true;

 

}

 

 

public object this[string columnName]

 

{

 

 

get

 

 

 

 

 

 

{

 

 

if (data.ContainsKey(columnName))

 

{

 

 

return data[columnName];

 

}

 

 

return null;

 

}

 

 

set

 

 

 

 

 

 

{

 

 

if (!data.ContainsKey(columnName))

 

{

data.Add(columnName,

 

value);

 

OnPropertyChanged(columnName);

}

 

 

else

 

 

 

 

 

 

{

 

 

if (data[columnName] != value)

 

{

data[columnName] =

 

value;

 

OnPropertyChanged(columnName);

}

}

}

}

 

 

private void OnPropertyChanged(string propertyName)

 

{

 

 

if (PropertyChanged != null)

 

{

PropertyChanged(

 

this, new PropertyChangedEventArgs(propertyName));

 

}

}

 

 

 

 

#region

 

 

INotifyPropertyChanged Members

 

 

 

public event PropertyChangedEventHandler PropertyChanged;

 

 

 

 

 

#endregion

 

 

 

 

 

}

 

 

 

 

 

#endregion

 

 

Data Load Procedures

 

 

}

 

 

 

1 Answer, 1 is accepted

Sort by
0
Madhan
Top achievements
Rank 1
answered on 11 Dec 2012, 06:21 PM
I formatted and corrected  the code: 

 

<telerik:RadGridView x:Name="RadGridView1" Margin="10,12,10,0" 

RowIndicatorVisibility="Collapsed" CanUserFreezeColumns="False"  

ItemsSource="{Binding Path=Data}" Foreground="Black" Width="864" />

Here Data is observableCollection<MyDataRow> in Viewmodel 

 

 

private ObservableCollection<MyDataRow> _data; 

 

public ObservableCollection<MyDataRow> Data

 {

 get {return _data;}

 set{_exceldata = value;  

RaisePropertyChanged("Data"); 

}

}

 

 

 

I am getting the Grid data using

 

Data = GetGridData(UploadProcessData,UploadTypeId); 

 

 

private ObservableCollection<MyDataRow> GetGridData(List<UploadDetail> lst)

 {

 var data = new ObservableCollection<MyDataRow>();

 

var string1 = "Company"

var string2 = "ChannelID";

  

foreach (UploadDetail memberInfo in lst)

 { 

MyDataRow row = new MyDataRow();  

 

row[string1] = memberInfo.Col_1;

row[string2] = memberInfo.Col_2;

data.Add(row);

}

 return data;  

}

 

MyDatarow Class:

 

 

 

public class MyDataRow : DynamicObject, INotifyPropertyChanged

 

 

 

 

 

{

 

 

IDictionary<string, object> data;

 

 

 

public MyDataRow()

 

{

data =

 

new Dictionary<string, object>();

 

}

 

 

public MyDataRow(IDictionary<string, object> source)

 

{

data = source;

}

 

 

public override IEnumerable<string> GetDynamicMemberNames()

 

{

 

 

return data.Keys;

 

}

 

 

public override bool TryGetMember(GetMemberBinder binder, out object result)

 

{

result =

 

this[binder.Name];

 

 

 

return true;

 

}

 

 

public override bool TrySetMember(SetMemberBinder binder, object value)

 

{

 

 

this[binder.Name] = value;

 

 

 

return true;

 

}

 

 

public object this[string columnName]

 

{

 

 

get

 

 

 

 

 

{

 

 

if (data.ContainsKey(columnName))

 

{

 

 

return data[columnName];

 

}

 

 

return null;

 

}

 

 

set

 

 

 

 

 

{

 

 

if (!data.ContainsKey(columnName))

 

{

data.Add(columnName,

 

value);

 

OnPropertyChanged(columnName);

}

 

 

else

 

 

 

 

 

{

 

 

if (data[columnName] != value)

 

{

data[columnName] =

 

value;

 

OnPropertyChanged(columnName);

}

}

}

}

 

 

private void OnPropertyChanged(string propertyName)

 

{

 

 

if (PropertyChanged != null)

 

{

PropertyChanged(

 

this, new PropertyChangedEventArgs(propertyName));

 

}

}

 

 

 

#region

 

 

INotifyPropertyChanged Members

 

 

 

public event PropertyChangedEventHandler PropertyChanged;

 

 

 

 

#endregion

 

 

 

 

}

 

 

Tags
GridView
Asked by
Madhan
Top achievements
Rank 1
Answers by
Madhan
Top achievements
Rank 1
Share this question
or