Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi,i joined 2x tables together and bound the items to my datagrideview(membersdataGrid),and thats worked but i can't reach id or anything else in my datagrid here is my code
C#
private void Load()
        {
             var st = (from s in db.RegFee_Tbl
                      join o in db.User_Tbl on s.NumberPhone equals o.NumberPhone
                      select new { s.ID, s.RegTime, s.NumberPhone, o.FatherName, o.Name}).ToList();

            membersdataGrid.ItemsSource = st;
            datagrid = membersdataGrid;
        }

i use above method to bind joined tables into my datagrid(membersdataGrid),and in my datagrid i have a button to delete selected row,in my buttuon click event i use this code to reach selected row id and delete that row:
C#
RegFee_Tbl bluser = new RegFee_Tbl();
int id = (memebersdataGrid.SelectedItem as RegFee_Tbl).ID;
bluser.Delete(id);


What I have tried:

C#
RegFee_Tbl bluser = new RegFee_Tbl();
int id = (memebersdataGrid.SelectedItem as ****).ID;
bluser.Delete(id);

**** is my problem i know i must use st(st is my query name for joining table together) here but when i using st i got this Erro > 'st' is a variable but is used like a type
Posted
Updated 6-Feb-23 4:54am
Comments
Richard MacCutchan 6-Feb-23 10:42am    
Show the actual line of code that causes the problem.

1 solution

You've created an anonymous type[^] to represent the projection. There is generally no way to cast that to an object and read its properties short of reflection.

One simple but fragile approach would be to use C#'s dynamic type[^], which will hide the reflection code from you:
C#
dynamic selectedItem = memebersdataGrid.SelectedItem;
int id = selectedItem.ID; // <- Reflection magic happens here...
The major down-side is that you will have no intellisense, and no compile-time checks. If you spell the property name wrong, or use the wrong case, you'll find out at runtime when you get a RuntimeBinderException thrown.

Rolling your own reflection won't be any better, and will potentially decrease the performance:
C#
object selectedItem = memebersdataGrid.SelectedItem;
Type itemType = selectedItem.GetType();
PropertyInfo prop = itemType.GetProperty("ID");
int id = (int)prop.GetValue(selectedItem, null);

The safer solution is to create a specific class to represent the projection, and use that instead:
C#
public class MemberProjection
{
    public int ID { get; set; }
    public DateTime? RegTime { get; set; }
    public string NumberPhone { get; set; }
    public string FatherName { get; set; }
    public string Name { get; set; }
}

...

private void Load()
{
     var st = (from s in db.RegFee_Tbl
              join o in db.User_Tbl on s.NumberPhone equals o.NumberPhone
              select new MemberProjection { ID = s.ID, RegTime = s.RegTime, NumberPhone = s.NumberPhone, FatherName = o.FatherName, Name = o.Name }).ToList();

    membersdataGrid.ItemsSource = st;
    datagrid = membersdataGrid;
}

...

int id = ((MemberProjection)memebersdataGrid.SelectedItem).ID;

NB: With any of these approaches, remember that the SelectedItem may be null. You'll want to check for that before trying to access one of its properties.
 
Share this answer
 
Comments
Max Speed 2022 6-Feb-23 12:57pm    
thank you so much sir,that's work, and again thank you for your attention and explain very good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900