0
votes

Referring to my previous Question : DataGrid itemsSource from Joined Tables in SQL Database using LINQ

i create this data Grid :

<DataGrid x:Name="DataGridRegisteredUsers" IsReadOnly="True" AutoGenerateColumns="False"
            HorizontalContentAlignment="Stretch" Margin="0,0,-1,-1"
            FlowDirection="RightToLeft" Opacity="0.9" FontFamily="B Nazanin" FontSize="15" RowDetailsVisibilityMode="Visible">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=UserID}" IsReadOnly="True"
                            Header="UserID" />
        <DataGridTextColumn Binding="{Binding Path=FirstName}"
                            Header="First Name" />
        <DataGridTextColumn Binding="{Binding Path=LastName}"
                            Header="Last Name" />
        <DataGridTextColumn Binding="{Binding Path=EmploymentID}"
                            Header="Employment ID" />
        <DataGridTextColumn Binding="{Binding Path=Status}"
                            Header="Status" />                                                  
    </DataGrid.Columns>
</DataGrid>

but now i need to collect UserIDs from Selected Items to do this query and delete them from database :

foreach (var userID in DataGridSelectedUserIDs)
{
    var findQ = (from f in dbEntities.RelCUs
        where f.UserID == userID && f.CourseID == currCourse.CourseID
        select f);
    foreach (var relCu in findQ)
    {
        dbEntities.RelCUs.Remove(relCu);
        dbEntities.SaveChanges();
    }
}

i have The CourseID, but i don't know how to get the UserIDs! my idea is to add a checkbox column and get checked items UserID but i don't know how. i searched but possible solution like these didn't worked for me: How to delete selected rows (using checkbox) in wpf datagrid

Get data of specific cell from selected row in DataGrid WPF C#

cause i don't want to add another field like "IsSelected" to table.

thanks alot:)

3

3 Answers

1
votes

I believe that your problem is that you have modelled your code classes on your database tables too closely. If a database table has a foreign key in it, then your class should have an object of they type specified by the foreign key.

For example, if this were your database:

User              Course
------            ------
Id                Id
Name              Name
CourseId

Your classes should look like this:

User              Course
------            ------
Id                Id
Name              Name
Course

So if you wanted to know the Id of the Course that a User is on in code, you could just check this: User.Course.Id.

Of course, this wasn't supposed to have a direct correlation to your code, but you should be able to get enough of an idea from it to help you.

0
votes

I collect the UserIDs from DataGrid with this Method,

string Input = id.Item.ToString();
            string[] OutPut = Input.Split(new[] {',', '=', '}', '{'}, StringSplitOptions.RemoveEmptyEntries);
            foreach (var s in OutPut)
            {
                MessageBox.Show(s);
            }

You can find anything you need from datagrid with these tokens.

I Know it's Totally HardCoding, But after trying several Unsuccessful ways, it came to my mind and worked.

0
votes

Another Method:

            List<int> IDs = (from id in DataGridRegisteredUsers.SelectedCells
            select id.Item.ToString()
            into Input
            select Input.Split(new[] {',', '=', '}', '{'}, StringSplitOptions.RemoveEmptyEntries)
            into OutPut
            select Convert.ToInt32(OutPut[1])).ToList();

        foreach (int userID in IDs)
        {
            dbEntities = new BASUEntities();
            RelCU findQ = (from f in dbEntities.RelCUs
                where f.UserID == userID && f.CourseID == currCourse.CourseID
                select f).SingleOrDefault();
            dbEntities.RelCUs.Remove(findQ);
            dbEntities.SaveChanges();
        }