在c#中单击按钮时显示包含sql数据的MessageBox [英] Show MessageBox that contain sql data when button clicked in c#
问题描述
我正在制作一个程序,将我的数据显示为图像和标签.这是我的程序未点击时的示例:
I'm making a program that showing my data into image and label. Here's an example of my program when not clicked:
未点击时:
点击时
问题是当我点击其中一张图片时.如何将该图像的 id(我的 SQL 中的id_movie")显示到 MessageBox ViewModel 类中.
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; }
}
点击类
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");
}
}
推荐答案
如果只需要数据库的数据,可以在初始化ClickedCommand<时将
dt
作为参数/代码>.
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);
}
}
并且在 Execute
方法中,您可以访问 vModel
或 Library
字段,具体取决于您作为参数提供的内容并显示数据.
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]);
}
}
这篇关于在c#中单击按钮时显示包含sql数据的MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!