1

I have a datagrid of rows which contain data read from a web server and values I want to write into a webserver. I write the values in getting the user to input a number into the appropriate column and click an adjacent text box;

    <DataGrid x:Name="datagridDERControl" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FF322D2D" Height="382" Margin="10,78,10,10" Width="972" ItemsSource="{Binding Path=NFDataSource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.Columns>

            <DataGridTemplateColumn Width="100" Header="Write Set Point">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Width="100" Text="{Binding Path=WriteSetPoint, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Width="100" Header="Global Trip">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Name="buttonGlobalTrip" Width="100" Click="buttonGlobalTrip_Click"></Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid

How do I extract the specific textbox string per row to use in my view model.

3
  • 1
    What is VMMV? Do you mean MVVM? Commented Jun 9, 2014 at 9:23
  • Yes MVVM, sorry about that! Commented Jun 9, 2014 at 9:47
  • You should find an 'edit' button under your question. It might be an idea to correct your error and perhaps include some additional information that is relevant to your question. Commented Jun 9, 2014 at 10:09

2 Answers 2

1

It's always difficult to answer a question where the relevant details have been omitted by the question author. However, I shall try!

You have data bound (presumably) a collection property named NFDataSource to your DataGrid.ItemsSource property. That is the collection that represents the data in your DataGrid, so to 'extract' a specific value, you need to look into your data items in your collection.

One handy property in the DataGrid class is the SelectedItem property. this enables you to data bind an object (of the same type as those in your NFDataSource collection) to this property, which accesses the data object behind the row that is currently selected in the UI:

<DataGrid ItemsSource="{Binding NFDataSource}" SelectedItem="{Binding SelectedItem}" />

Now you can utilise your SelectedItem property to access the values from the selected row in the DataGrid:

string someValue = SelectedItem.SomeProperty;
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry about the omission of details, would you have liked some of the view model source code as well? After I have bound the selected item to a property in my view model how do I access a specific cell? Is the cell the selected item (the textbox in this case)? Or is it the row, in which case I#d specify the column name? Thankd
No, the view model is irrelevant here... just think about it... what might have been useful to include? Perhaps it would have helped to know what type of object was in your collection for a start? The column (or cell) values come from the various properties of the item object. So, as I showed you in my example, to access some cell value, you could do something like this: string someValue = SelectedItem.SomeProperty;. Of course, if your collection is not of the type of a custom class, then maybe you don't have properties to access... I don't know... remember, you never told us.
0

As you tagged this with MVVM and databinding, I'll assume you're using these and have just got muddled.

"I have a datagrid of rows which contain data read from a web server and values I want to write into a webserver."

So your viewmodel has a property, which is a collection of a custom class, that represents data fetched from a webservers.

"I write the values in getting the user to input a number into the appropriate column and click an adjacent text box"

So this VM property is two-way bound to a datagrid, so that each item in the collection represents 'one row', and the properties on those items represent your 'columns'. The user can make changes to the UI displayed values, and because of the two way databinding the VM property is also updated.

"How do I extract the specific textbox string per row to use in my view model."

Why do you need the specific textbox string, if it is databound to a property (or rather to a property on a class contained in a collection) in your VM anyway? If you've set up your VM this way, and are using databinding, you rarely need to worry about UI specific things such as which row in a datagrid is clicked.

As Sheridan points out though, you can also bind to properties on the datagrid such as SelectedItem so that you can perform additional operations beyond just reading/writing data. SelectedItem for your datagrid will be of the type that populates your VM collection, so will have the appropriate properties.

For example, if your VM collection is an IQueryable<Person> and that is bound to the ItemsSource of the datagrid then the SelectedItem will be of type Person. You could then have a VM property called SelectedPerson which is bound to that SelectedItem, and access things like SelectedPerson.Name etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.