簡體   English   中英

將存儲的圖像顯示到datagridview中

[英]Display the stored image into the datagridview

我正在使用Visual Studio 2017和MS Access來存儲數據庫。 我已經在數據庫中也存儲了數據和圖像位置,但是我無法在datagridview中顯示圖像。 我該如何解決?

我已經嘗試過將代碼存儲在數據庫中的代碼

string img_path = "\\image\\" + pwd + ".jpg";

try
{
    connection.Open();

    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "insert into  Temporaryanimaldata (dartadate, dartano, cardno, animalname, huliya, age, layakoplace, color, situtation, animalowner, adress, contact, waliname, walidate, picture) values('" + dartadatetext.Text + "','" + dartanotext.Text + "','" + cardnotext.Text + "','" + animalnametext.Text + "','" + huliyatext.Text + "','" + agetext.Text + "','" + layakoplacetext.Text + "','" + colortext.Text + "','" + health.ToString() + "','" + animalownertext.Text + "','" + addresstext.Text + "','" + contacttext.Text + "','" + walinametext.Text + "','" + walidatetext.Text + "','" + img_path.ToString() + "')";

    command.ExecuteNonQuery();
    connection.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Error" + ex);
}

MessageBox.Show("Successful");

我已經在圖片框旁邊嘗試過此代碼

wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));

DialogResult result = openFileDialog1.ShowDialog();

//openFileDialog1.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|BMP Files (*.bmp)|*.bmp|GIF Files (*.gif)|*.gif";
openFileDialog1.Filter = "Image Files (*.png;*.jpg;*.gif;*.bpn)|*.png;*.jpg;*.gif;*.bpn";

if (result == DialogResult.OK)
{
    pictureBox1.ImageLocation = openFileDialog1.FileName;

    // copy image in specific folder
    File.Copy(openFileDialog1.FileName, wanted_path + "\\image\\" + pwd + ".jpg");
}

我嘗試使用此代碼在datagridview中顯示數據

int i = 0;

       4);

connection.Open();

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from Temporaryanimaldata";

command.ExecuteNonQuery();

connection.Close();

OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow item in dt.Rows)
{
    int n = dataGridView1.Rows.Add();
    dataGridView1.Rows[n].Cells[0].Value = item[0].ToString();
    dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
    dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
    dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
    dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();
    dataGridView1.Rows[n].Cells[5].Value = item[5].ToString();
    dataGridView1.Rows[n].Cells[6].Value = item[6].ToString();
    dataGridView1.Rows[n].Cells[7].Value = item[7].ToString();
    dataGridView1.Rows[n].Cells[8].Value = item[8].ToString();
    dataGridView1.Rows[n].Cells[9].Value = item[9].ToString();
    dataGridView1.Rows[n].Cells[10].Value = item[10].ToString();
    dataGridView1.Rows[n].Cells[11].Value = item[11].ToString();
    dataGridView1.Rows[n].Cells[12].Value = item[12].ToString();
    dataGridView1.Rows[n].Cells[13].Value = item[13].ToString();
    dataGridView1.Rows[n].Cells[14].Value = item[14].ToString();
}

您無需遍歷數據表的數據行。

刪除foreach語句。

您可以像這樣將DataTable分配為DataGridViews數據源:

dataGridView1.DataSource = dt;

分配數據源后,datagridview將具有數據表中的所有列和行。

請參閱此博客文章以獲取更多示例: https : //www.c-sharpcorner.com/UploadFile/deveshomar/ways-to-bind-datagridview-in-window-forms-C-Sharp/

暫無
暫無

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

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