簡體   English   中英

如何使用 sql 和 c# 在 datagridview 中加載圖像

[英]How to load images in datagridview with sql and c#

我正在用 C# 開發一個應用程序我有一個名為 Resuktats 的表的數據庫,其中包含 6 個字段:

logo_imageA -> teamA -> scoreA -> scoreB -> teamB -> logoB

字段 logo_Image 將圖像路徑存儲在我的硬盤中

記錄的一個例子是:

C:\imageA.png -- teamA -- 2 -- 3 -- teamB -- C:\imageB.png

我想知道如何填充顯示每個團隊的名稱和結果的數據網格視圖,尤其是 - 我的數據庫 sql 中每個團隊的徽標圖像。

我正在嘗試這個代碼:

        private void button1_Click(object sender, EventArgs e)
    {

        int number_of_rows = dgv_calendrier.RowCount;
        for (int i = 0; i < number_of_rows; i++)
        {
            //DataGridViewImageColumn dataGridViewColumn = new DataGridViewImageColumn();

                if (dgv_calendrier.Rows[i].Cells[1].Value.ToString() == "true")
                dgv_calendrier.Rows[i].Cells["logo_dom"].Value = 


    }

}

執行我的 sql 查詢的打印屏幕:

要自己解決問題,請將其划分為子問題:

  • 如果你有一個 system.Drawing.Image,你能在你的 datagridView 中顯示它嗎?
  • 如果您有圖像文件的名稱,您能否將該文件作為 System.Drawing.Image 加載,從而將其顯示在您的數據網格視圖中?

第一個問題通過搜索如何在數據網格視圖中顯示圖像解決如何:在Windows窗體DataGridView控件的單元格中顯示圖像

第二個: Image.FromFile(string)

首先,您可以將所有數據從 sql server 加載到 datagrid 視圖。

    DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
    dgv_calendrier.Columns.Add(imageCol);             //Andd new image columns to grid view

    int number_of_rows = dgv_calendrier.RowCount;
    for (int i = 0; i < number_of_rows; i++)
    {
            if (dgv_calendrier.Rows[i].Cells[1].Value.ToString() == "true")

            Bitmap img = new Bitmap(@" dgv_calendrier.Rows[i].Cells["logo_dom"].Value.ToString()"); // get iamge location and conver to bit map.

            dgv_calendrier.Rows.Add();
            dgv_calendrier.Rows[i].Cells[7].Value = img; //set you newly added colomun

     }

希望這會有所幫助

暫無
暫無

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

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