【问题标题】:C# Image from Database to DataGridViewC# 图像从数据库到 DataGridView
【发布时间】:2016-01-26 20:21:12
【问题描述】:

我真的是 C# 新手,所以我遇到了很多问题。我有一个带有 SQL 数据库的 Windows 窗体,当我在 DataGridVie 中选择 user 1 时,我希望看到我保存的图片以及 PictureBoxTextBoxes 中的其余数据。我知道我的问题是什么,但不知道如何解决。

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


    【解决方案1】:

    您是否使用 blob 来存储图像?就投吧

    imagebox.image =  byteArrayToImage(row.Cell[10].Value));
    public Image byteArrayToImage(byte[] byteArrayIn)
    {
         MemoryStream ms = new MemoryStream(byteArrayIn);
         Image returnImage = Image.FromStream(ms);
         return returnImage;
    }
    

    【讨论】:

    • 您好,谢谢,这应该适用于第二个问题,但是当我从我的 1 个表单登录到数据库并转到第 2 个时,Void load_table 会导致错误 -> fs5.directupload.net/images/160126/ahwdbzq7.jpg 或者我得到了您的答案错误是您为 void Load_table() 提供的代码?
    【解决方案2】:

    我相信你的问题是你不是为了得到属性 试试这个:

    imagebox.image = ((DataGridViewImageColumn)row.Cell[10]).Image(); 
    // or try this :
    imagebox.image = (Image)row.Cell[10].Value;
    

    如果您的代码没有任何其他问题,那么您应该可以使用它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多