簡體   English   中英

從數據庫到DataGridView的C#圖像

[英]C# Image from Database to DataGridView

我真的是C#的新手,所以有很多問題。 我有一個帶有SQL數據庫的Windows窗體,當我在DataGridVie選擇user 1時,我想查看在PictureBoxTextBoxes保存的其余PictureBox 我知道我的問題是什么,但不知道如何解決。

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 當我也想獲取保存的圖像時,出現錯誤。 我知道我必須將列更改為image列或類似的列,但是到目前為止,我發現沒有任何解決方案。

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的行是從1到1,我應該手動創建它們,但是隨后出現了更多錯誤。

任何幫助將不勝感激。 謝謝。

我相信您的問題是您不打算讓propertype嘗試以下操作:

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

如果您的代碼沒有其他問題,那么應該可以使用

您是否正在使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM