显示的MessageBox包含SQL数据时,按钮在C#中点击(Show MessageBox tha

2019-10-29 18:32发布

我做的是显示为图像和标签我的数据的程序。 下面是我的程序的一个例子不是点击后:

如果没有被点击:

单击时

现在的问题是,当我点击这些图片中的一个。 如何显示的ID图像的(在我的SQL“id_movie”)到一个消息视图模型类。

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; }
}

单击类

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");
    }
}

Answer 1:

如果你只需要对数据库中的数据,你可以给dt初始化时,作为参数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);
    }
}

而在Execute方法,你访问vModelLibrary领域,这取决于你给什么作为参数,并呈现这些数据。

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]);
    }
}


文章来源: Show MessageBox that contain sql data when button clicked in c#