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

How to get value of GridViewDataColumn(CheckBox) in ViewModel?

1 Answer 360 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nemo
Top achievements
Rank 1
Nemo asked on 10 Aug 2012, 08:26 PM

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

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 13 Aug 2012, 05:38 AM
Hi Prathibha,

You could work with the data item instead. For example you can get the value like so:

foreach (GridViewRow row in gViewRows)
{
    bool addressSame= row.Item.IsAddressSame;
}


Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Nemo
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or