1

i have a DataGrid that fill by multiple tables by Entity-FrameWork by this Code:

var Qselect = (from w in db.Workshops
                           join wt in db.WorkshopTypes on w.WorkshopTypeId equals wt.WorkshopTypeId
                           join tt in db.TransferTypes on w.WorkshopTransfer equals tt.TransferTypeId
                           where w.WorkshopDate.Contains("1395/05/01")
                           orderby w.WorkshopDate
                           select new
                           {
                               WorkshopId = w.WorkshopId,
                               WorkshopDate = w.WorkshopDate,
                               WorkshopTypeId = wt.WorkshopTypeName,
                               WorkshopDetails = w.WorkshopDetails,
                               FusionId = w.FusionId,
                               WorkshopTransfer = tt.TransferTypeName,
                               WorkshopWeight = w.WorkshopWeight
                           }).Take(10);
            grdWorkshop.ItemsSource = Qselect.ToList();

and i Create my DataGrid By this Code

<DataGrid x:Name="grdWorkshop" AutoGenerateColumns="False" >
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="id" Width="50" Binding="{Binding WorkshopId}" />
                                <DataGridTextColumn Header="Date" Width="140" Binding="{Binding WorkshopDate }"/>
                                <DataGridTextColumn Header="Type" Width="110" Binding="{Binding WorkshopTypeId }"/>
                                <DataGridTextColumn Header="Detail" Width="220" Binding="{Binding WorkshopDetails }"/>
                                <DataGridTextColumn Header="Series" Width="80" Binding="{Binding FusionId }"/>
                                <DataGridTextColumn Header="Transfer" Width="130" Binding="{Binding WorkshopTransfer }"/>
                                <DataGridTextColumn Header="Weight" Width="100" Binding="{Binding WorkshopWeight }"/>
                            </DataGrid.Columns>
                            <DataGrid.ContextMenu>
                                <ContextMenu >
                                    <MenuItem Header="Add" Click="AddWorkshopItem" />
                                    <MenuItem Header="Edit" Click="EditWorkshopItem" />
                                    <MenuItem Header="Delete" Click="DeleteWorkshopItem" />
                                </ContextMenu>
                            </DataGrid.ContextMenu>
                        </DataGrid>

Now i want to Edit Or Delete My Rows by Context Menu And Then Do this by Stored procedures. Now I Want to Get The id of selected row in Data Grid To Delete or edit Rows. How I Can Get This?? I am using Entity-FrameWork And Full Row SelectionUnit.

i see this code and was work. but in my project does not work. i think it because of i join more than 1 table.

int id = (grdWorkshop.SelectedItem as Workshop).WorkshopId;

Thanks.

yasser93
  • 53
  • 1
  • 11

1 Answers1

0

Finally i get my answer

class CWorkshop
{
    public decimal? WorkshopWeight { get; set; }
    public int WorkshopId { get; set; }
    public string WorkshopDate { get; set; }
    public string WorkshopTypeId { get; set; }
    public string WorkshopDetails { get; set; }
    public int? FusionId { get; set; }
    public string WorkshopTransfer { get; set; }
}

i changed my query by this class like this. of course with full privet variables and properties.

var Qselect = (from w in db.Workshops
                               join wt in db.WorkshopTypes on w.WorkshopTypeId equals wt.WorkshopTypeId
                               join tt in db.TransferTypes on w.WorkshopTransfer equals tt.TransferTypeId
                               where w.WorkshopDate.Contains("1395/05/01")
                               orderby w.WorkshopDate
                               select new CWorkshop()
                               {
                                   WorkshopId = w.WorkshopId,
                                   WorkshopDate = w.WorkshopDate,
                                   WorkshopTypeId = wt.WorkshopTypeName,
                                   WorkshopDetails = w.WorkshopDetails,
                                   FusionId = w.FusionId,
                                   WorkshopTransfer = tt.TransferTypeName,
                                   WorkshopWeight = w.WorkshopWeight
                               }).Take(10);

then get my data by this code. it was so easy

int id = (grdWorkshop.SelectedItem as CWorkshop).WorkshopId;

i hope it was useful.

yasser93
  • 53
  • 1
  • 11