0

i am new to wpf mvvm.for getting value from data grid on selection,i have created a property that is of object datatype.when am clicking on data grid row i get set of values containing first name,last-name,city,state,pin,mobileno,mail id,employee id etc.i need to retrieve employee id from that for updation purpose..but i don't know how to retrieve those values from that property declared in object datatype..please help me....\

this is my view model

public object selectedEmployee;
public object SelectedEmployee
{
    get
    {
        return selectedEmployee;
    }
    set
    {
        selectedEmployee = value;
        OnPropertyChanged("SelectedEmployee");
    }
}

this is my xaml code

<DataGrid Grid.Row="2" x:Name="grdEmployee" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding EmployeeDatatable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" AutoGenerateColumns="False"  IsReadOnly="True"  HorizontalAlignment="Center" Margin="59,0,86,12" Width="492" CanUserAddRows="False"  
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Emp_id}" Header="Employee ID"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding LastName}" Header="LastName"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Age}" Header="Age"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding ZipCode}" Header="ZipCode"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding PhoneNumber}" Header="PhoneNumber"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding MobileNumber}" Header="MobileNumber"></DataGridTextColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Name="btnEdit" Content="Edit"></Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>

    </DataGrid>

1 Answer 1

0

Dude, you must have put a custom object of some type (that has those properties) into your object variable. All you have to do is to cast the object back to its original type and then you will be able to access the properties again. Because I don't know what type it is, for this example, I am going to imagine that you have a class named Employee that has these properties. This is how you cast an object:

Employee employee = (Employee)yourRowObject;
// Now you can access the properties. For example...
string fullName = string.Concat(employee.Firstname, " ", employee.LastName);
int employeeId = employee.Id;
1
  • i got another way to get access to the column values of datagrid..anyway thank you for your help... Sep 18, 2013 at 8:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.