Unable to cast object of type 'Ksiazki' to type 'System.Data.DataRowView'

Jakub Bukowski 1 Reputation point
2022-05-29T16:51:50.507+00:00

Hello, I have specific problem with function i want to implement.
So basically I got DataGrid that list filtered rows:

private void btnSzukajKsiazkeClick(object sender, RoutedEventArgs e)
{ DataGridKsiazki.ItemsSource=BazaDanych.WybierzKsiazki(textBoxKsiazka.Text); }

referring to:
public class Ksiazki
{
public int Identyfikator { get; set; }
public String Nazwa { get; set; }
public String Autor { get; set; }
public Ksiazki( int IdKsiazka, String NazwaKsiazka, String AutorKsiazka)
{
Identyfikator= IdKsiazka;
Nazwa = NazwaKsiazka;
Autor = AutorKsiazka;
}
}

    public static List<Ksiazki> WybierzKsiazki(String nazwaKsiazka)
        {
            List<Ksiazki> KsiazkiLista = new List<Ksiazki>();

            string pathToDB = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Biblioteka.db");

            using (SqliteConnection con = new SqliteConnection($"Filename={pathToDB}"))
            {
                con.Open();
                String CMD_Select = "SELECT idKsiazka,nazwaKsiazka,autorKsiazka FROM Ksiazki WHERE nazwaKsiazka='" +nazwaKsiazka+"' AND statusKsiazka=0;";
                SqliteCommand cmd_getRec = new SqliteCommand(CMD_Select, con);

                SqliteDataReader reader = cmd_getRec.ExecuteReader();
                while (reader.Read())
                {
                    KsiazkiLista.Add(new Ksiazki(reader.GetInt32(0), reader.GetString(1), reader.GetString(2)));
                }
                con.Close();
            }
            return KsiazkiLista;
        }

and there is everything good, but when i want to:

private void DataGridKsiazki_CurrentCellChanged(object sender, EventArgs e)
{

        btnWybierzKsiazke.Visibility = Visibility.Visible;

        DataRowView dataRowView = (DataRowView)DataGridKsiazki.SelectedItem;
        if (dataRowView != null)
        {
            int rowId = Convert.ToInt32(dataRowView.Row[0]);
        }

there appears error: Unable to cast object of type 'Ksiazki' to type 'System.Data.DataRowView'

that code is supposted to store id value of clicked row.
How can I fix it?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2022-05-30T03:04:17.533+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Just as the error messages mentioned, the DataGridKsiazki.SelectedItem object is a Ksiazki object and you can't convert it into a DataRowView object. So it should be like this:

      Ksiazki _ksiazki = (Ksiazki)DataGridKsiazki.SelectedItem;  
    

    I'd suggest you get the index of the Ksiazki object in the list if you are using data binding. That index value should be the same as the row index. Like:

    int index = KsiazkiLista.IndexOf(_ksiazki);  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments