简体   繁体   中英

Display the stored image into the datagridview

I am using Visual Studio 2017 and MS Access for storing a database. I already store data also image location on database but i can't display the image in datagridview. How can I fix it?

I have tried this code for storing a image in database

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");

I have tried this code beside the picturebox

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");
}

I tried this code for display data in 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();
}

You don't need to loop through data rows of the data table.

Remove the foreach statement.

You can just assign DataTable as DataGridViews data source like this:

dataGridView1.DataSource = dt;

Once you assign data source datagridview will have all the columns and rows that are inside the datatable.

Look at this blog post for more examples: https://www.c-sharpcorner.com/UploadFile/deveshomar/ways-to-bind-datagridview-in-window-forms-C-Sharp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM