I am using a GRadGridView("CustomerRadGrid") which is initially bounded to a empty ObservableCollection(CustomerCollection) and RadButton. The User should be able to add Rows to the Grid and Add Button ("AddCustomerBttn") adds all rows with user entered values to the ObservableCollection(CustomerCollection). I was able to get the value of the Fisrt Cell with this code.
string custName = row.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName == "CustomerName").FirstOrDefault().Value.ToString();
How to get value of GridViewDataColumn(CheckBox) in ViewModel?
View
<telerik:RadGridView ItemsSource="{Binding CustomerCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
CanUserInsertRows="True" ShowInsertRow="True"
Name="CustomerRadGrid">
<telerik:GridViewMaskedTextBoxColumn UniqueName="CustomerName" Header="Name" DataMemberBinding="{Binding FirstName}" MaskType="None"/>
<telerik:GridViewDataColumn UniqueName="IsMail">
<telerik:GridViewDataColumn.Header>
<StackPanel>
<TextBlock Text="Mailing and Shipping Address Same?" />
</StackPanel>
</telerik:GridViewDataColumn.Header>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Center">
<CheckBox IsChecked="{Binding Path=IsAddressesSame, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/ >
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView>
<telerik:RadButton Name="AddCustomerBttn" Content="Add"
Command="{Binding AddCustomerCommand
CommandParameter="{Binding ElementName=CustomerRadGrid}"/>
Model
public class Customer : ViewModel.ViewModelBase
{
string name;
public string Name //Product
{
get { return name; }
set { name = value; NotifyPropertyChanged("Name"); }
}
bool isAddressesSame;
public bool IsAddressesSame
{
get { return IsAddressesSame; }
set { IsAddressesSame = value;NotifyPropertyChanged("IsAddressesSame "); }
}
}
}
ViewModel:
ObservableCollection<Customer> customerCollection = new ObservableCollection<Customer>();
public ObservableCollection<Customer> CustomerCollection
{
get { return customerCollection; }
set { customerCollection = value; NotifyPropertyChanged("CustomerCollection"); }
}
#region AddCustomerCommand //Add
public ICommand AddCustomerCommand { get; private set; }
public HomeViewModel()
{
AddCustomerCommand = new RelayCommand(Execute_AddCustomerCommand, CanExecute_AddCustomerCommand);
}
public void Execute_AddCustomerCommand (object parameter)
{
RadGridView rGridView = (RadGridView)parameter;
IList<GridViewRow> gViewRows = rGridView.ChildrenOfType<GridViewRow>().ToList();
foreach (GridViewRow row in gViewRows)
{
string custName = row.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName == "CustomerName").FirstOrDefault().Value.ToString();
bool addressSame ;//Get Value from the .
//How to get value from GridViewDataColumn CheckBox(True if checked, False if unchecked)
Customer temp = new Customer()
{
Name = custName,
IsAddressSame = addressSame
};
CustomerCollection.Add(temp);
}
}
public bool CanExecute_AddCustomerCommand (object parameter)
{
return true;
}
#endregion
Thank you,
-Prathibha