question

JakubBukowski-0290 avatar image
0 Votes"
JakubBukowski-0290 asked RoyLi-MSFT answered

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

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?





windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RoyLi-MSFT avatar image
0 Votes"
RoyLi-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.