0

I'm making a program that showing my data into image and label. Here's an example of my program when not clicked:

When not Clicked: Screen Shot

When Clicked Screen Shot

The question is when I click one of those images. How to show the id ("id_movie" in my SQL) of that image into a MessageBox ViewModel Class.

public class VModel
{
    public VModel()
    {
        Clicked = new ClickedCommand(this);
        DataTable dt = new DataTable();

        using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
        {
           MySqlDataAdapter adapter = new MySqlDataAdapter();
           adapter.SelectCommand = new MySqlCommand("Select * from movie_list", connection);
           adapter.Fill(dt);
        }
        Library = dt.DefaultView;
    }
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }
}

Click Class

internal class ClickedCommand : ICommand
{
    private VModel vModel;

    public ClickedCommand(VModel vModel)
    {
        this.vModel = vModel;
    }

    public event EventHandler CanExecuteChanged { add { } remove { } }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        MessageBox.Show("the id that got clicked");
    }
}
FredM
  • 454
  • 9
  • 20
Hans
  • 25
  • 1
  • 8

1 Answers1

1

If you just need the data of the database, you could give the dt as a parameter when initializing the ClickedCommand.

public class VModel
{
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }

    public VModel()
    {
        DataTable dt = new DataTable();

        using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("Select * from movie_list", connection);
            adapter.Fill(dt);
        }

        var Library = dt.DefaultView;
        // this = viewModel or Library as parameter
        var Clicked = new ClickedCommand(this);
    }
}

And in the Execute method you access the vModel or Library field, depending on what you gave as a parameter and present the data.

internal class ClickedCommand : ICommand
{
    private VModel _vModel;
    // private DataView _library;

    public ClickedCommand(VModel vModel)
    {
        _vModel = vModel;
        // _library = library;
    }

    public void Execute(object parameter)
    {
        int rowIndex;
        int.TryParse(((string[])parameter)[0], out rowIndex);
        var stringToSearch = ((string[]) parameter)[1];
        // however you access it here.
        MessageBox.Show(_vModel.Library[rowIndex][stringToSearch]);
        // MessageBox.Show(_library[rowIndex][stringToSearch]);
    }
}
FredM
  • 454
  • 9
  • 20
  • i tried your code and it says DataView does not contain GetData and no extention method of GetData. Beginner here – Hans Nov 23 '18 at 08:23
  • @Hans `GetData` isn't a real method, I just used it to demonstrate how you could do it. – FredM Nov 23 '18 at 08:25
  • so i should add a new method in VModel? – Hans Nov 23 '18 at 08:27
  • @Hans no. You have to check how to access the data of the `DataView` class. Look on SO how to achieve this, like [here](https://stackoverflow.com/questions/387334/how-to-get-a-value-from-a-column-in-a-dataview) for example. – FredM Nov 23 '18 at 08:32
  • can you add another example?i still dont understand – Hans Nov 23 '18 at 08:41
  • @Hans I've updated my answer. Now you need to pass in `parameter` the `rowIndex` and `stringToSearch`, when calling `Execute` method. – FredM Nov 23 '18 at 08:48
  • it says cannot apply indexing with [] to an expression of type object on rowindex and stringtosearch – Hans Nov 23 '18 at 09:18
  • @Hans updated again. I'm sorry, but I cannot provide you with more information. – FredM Nov 23 '18 at 10:22
  • @Hans you're welcome. If it answered your question, please accept it as an answer. ;) – FredM Nov 23 '18 at 10:26