Haritha
Haritha

Reputation: 1508

How to select a DataGrid row in WPF with MVVM when the DataGrid is bound to a DataTable

I have successfully bound a DataTable to a DataGrid control in WPF with MVVM. (I have defined the DataTable in the viewmodel.)

Then I have defined a DataRowView type property and bound to the SelectedItem property of the DataGrid control.

I can get the selected item through that. But I tried to set the selected item but I couldn't find a way to do it. Can somebody help me to figure it out.

The view

<Window x:Class="Pivot.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:Pivot.ViewModels"
    Title="MainWindow" Height="350" Width="525">
<Grid>

    <Grid.DataContext>
        <vm:MainViewModel />
    </Grid.DataContext>

    <DataGrid 
        ItemsSource="{Binding SizeQuantityTable}"
        AutoGenerateColumns="True"
        SelectedValue="{Binding SelectedRow}" 
        Margin="0,0,0,120" />
</Grid>
</Window>

The View Model

public class MainViewModel : ViewModelBase
{
    #region Declarations

    private DataTable sizeQuantityTable;
    private DataRowView selectedRow;

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the size quantity table.
    /// </summary>
    /// <value>The size quantity table.</value>
    public DataTable SizeQuantityTable
    {
        get
        {
            return sizeQuantityTable;
        }
        set
        {
            sizeQuantityTable = value;
            NotifyPropertyChanged("SizeQuantityTable");
        }
    }

    /// <summary>
    /// Gets or sets the selected row.
    /// </summary>
    /// <value>The selected row.</value>
    public DataRowView SelectedRow
    {
        get
        {
            return selectedRow;
        }
        set
        {
            selectedRow = value;
            NotifyPropertyChanged("SelectedRow");
        }
    }

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="MainViewModel"/> class.
    /// </summary>
    public MainViewModel()
    {
        this.SizeQuantityTable = new DataTable();

        DataColumn sizeQuantityColumn = new DataColumn();
        sizeQuantityColumn.ColumnName = "Size Quantity";
        this.SizeQuantityTable.Columns.Add(sizeQuantityColumn);

        DataColumn sColumn = new DataColumn();
        sColumn.ColumnName = "S";
        this.SizeQuantityTable.Columns.Add(sColumn);

        DataColumn mColumn = new DataColumn();
        mColumn.ColumnName = "M";
        this.SizeQuantityTable.Columns.Add(mColumn);

        DataRow row1 = this.SizeQuantityTable.NewRow();
        row1[sizeQuantityColumn] = "Blue";
        row1[sColumn] = "12";
        row1[mColumn] = "15";
        this.SizeQuantityTable.Rows.Add(row1);

        DataRow row2 = this.SizeQuantityTable.NewRow();
        row2[sizeQuantityColumn] = "Red";
        row2[sColumn] = "18";
        row2[mColumn] = "21";
        this.SizeQuantityTable.Rows.Add(row2);

        DataRow row3 = this.SizeQuantityTable.NewRow();
        row3[sizeQuantityColumn] = "Green";
        row3[sColumn] = "24";
        row3[mColumn] = "27";
        this.SizeQuantityTable.Rows.Add(row3);

        DataRow row4 = this.SizeQuantityTable.NewRow();
        row4[sizeQuantityColumn] = "Yellow";
        row4[sColumn] = "30";
        row4[mColumn] = "33";
        this.SizeQuantityTable.Rows.Add(row4);

    }

    #endregion
}

Upvotes: 0

Views: 10064

Answers (2)

digitally_inspired
digitally_inspired

Reputation: 777

Long pending question??. I made this modification in XAML and with it on button click I am able to change grid selection based on textbox row value.

<Grid>

        <Grid.DataContext>
            <vm:MainViewModel x:Name="Model"/>
        </Grid.DataContext>

        <DataGrid  
        ItemsSource="{Binding SizeQuantityTable}"
        AutoGenerateColumns="True"
        SelectedIndex="{Binding SelectedRow, Mode=TwoWay}" 
        Margin="0,0,0,120" />
        <Button Content="Button" Height="53" HorizontalAlignment="Left" Margin="121,214,0,0" Name="button1" VerticalAlignment="Top" Width="118" Click="button1_Click" />
        <TextBox Height="21" HorizontalAlignment="Left" Margin="272,218,0,0" Name="textBox1" VerticalAlignment="Top" Width="114" Text="1" />
    </Grid>

 /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
//I know this is not the correct way to do it in MVVM but I am am taking time from my work... so need to be quick.. :)
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Model.SelectedRow = int.Parse(textBox1.Text);
        }
    }



  private int selectedRow;

/// <summary>
        /// Gets or sets the selected row.
        /// </summary>
        /// <value>The selected row.</value>
        public int SelectedRow
        {
            get
            {
                return selectedRow;
            }
            set
            {
                selectedRow = value;
                OnPropertyChanged("SelectedRow");
            }
        }

Try this. It should work...

Upvotes: 0

RonakThakkar
RonakThakkar

Reputation: 903

If i understand correctly, you want to select grid row programatically from MainViewModel class. If that is the need then try below code. SelectRow is the method in MainViewModel. This might not be complete solution but some idea for you requirement.

public void SelectRow(int rowIndex)
{
     SelectedRow = SizeQuantityTable.DefaultView[rowIndex];
}

Upvotes: 3

Related Questions