【发布时间】:2016-01-26 20:21:12
【问题描述】:
我真的是 C# 新手,所以我遇到了很多问题。我有一个带有 SQL 数据库的 Windows 窗体,当我在 DataGridVie 中选择 user 1 时,我希望看到我保存的图片以及 PictureBox 和 TextBoxes 中的其余数据。我知道我的问题是什么,但不知道如何解决。
void Load_table()
{
try
{
string myConnectionstring = "datasource=localhost;port=3306; username=root;password=root";
MySqlConnection myConnection = new MySqlConnection(myConnectionstring);
MySqlCommand myCommand = new MySqlCommand("SELECT idBenutzerdaten, name, vorname, straße, hausnr, plz, wohnort, telenr, personr, schlossnr, picture FROM testdb.benutzerdaten;", myConnection);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = myCommand;
myDataTable = new DataTable();
myDataAdapter.Fill(myDataTable);
BindingSource mySource = new BindingSource();
mySource.DataSource = myDataTable;
dataGridView.DataSource = mySource;
myDataAdapter.Update(myDataTable);
}
catch (Exception fehler)
{
MessageBox.Show("Es gibt ein Problem mit der Datenbank\n" + fehler.Message, "Datenbankfehler ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
这是从数据库中获取数据到DataGridView 的代码。当我也想获取保存的图像时,出现错误。我知道我必须将列更改为图像列或类似的内容,但到目前为止我没有找到任何适合我的东西。
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
panel.Enabled = false;
DataGridViewCell cell = null;
foreach (DataGridViewCell selectedCell in dataGridView.SelectedCells)
{
cell = selectedCell;
break;
}
if (cell != null)
{
DataGridViewRow row = cell.OwningRow;
tbx_id.Text = row.Cells[0].Value.ToString();
tbx_name.Text = row.Cells[1].Value.ToString();
tbx_vorname.Text = row.Cells[2].Value.ToString();
tbx_strasse.Text = row.Cells[3].Value.ToString();
tbx_hausnr.Text = row.Cells[4].Value.ToString();
tbx_plz.Text = row.Cells[5].Value.ToString();
tbx_wohnort.Text = row.Cells[6].Value.ToString();
tbx_telenr.Text = row.Cells[7].Value.ToString();
tbx_perso.Text = row.Cells[8].Value.ToString();
tbx_schlossnr.Text = row.Cells[9].Value.ToString();
}
}
来自 datagridview 的事件。不确定在此处添加什么以及如何添加。但一定是这样的
imagebox.image = row.Cell[10].Value.Image();
我猜问题是datagridview的行是从数据库中一对一来的,我应该手动创建它们,但后来我得到了更多的错误。
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: c# mysql winforms datagridview