需要C#与datagridview相关的帮助与picturebox [英] Need help in C# related to datagridview with picturebox

查看:84
本文介绍了需要C#与datagridview相关的帮助与picturebox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[这是C#我制作的GUI] [1]





[1]:https:// i。 stack.imgur.com/z6nut.png





i需要帮助数据在datagridview中显示

但如何从datagridview获取所选行的数据到我的图片框

当我点击datagridview的任何一行时,datagridview的图片显示在我的图片框中看到了

但是我不知道如何让它尝试了很多...请帮助

i使用sqlexpress创建的tabel emp并像pic和datatype一样提交图像



这是我的代码...

[this is the GUI of C# i made][1]


[1]: https://i.stack.imgur.com/z6nut.png


i need help the data is showing in the datagridview
but how to fetch the data of selected row from datagridview to my picturebox
when i click any row of datagridview the picture from datagridview show be seen
in the my picturebox but i dont know how to get it tried a lot ... pls need help
i used the sqlexpress created tabel emp and filed like pic and datatype as image

this is the code...of mine

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;

namespace image_upload_retrieve
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=imagestore; Integrated Security=True;");

SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;

string imgLoc = "";

private void image_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
imgLoc = dlg.FileName.ToString();
picEmp.ImageLocation = imgLoc;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

private void imagesaving_Click(object sender, EventArgs e)
{
try
{
byte[] img = null;
FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);
string sql = "INSERT INTO emp values(@img)";
if (con.State != ConnectionState.Open)
{
con.Open();
cmd = new SqlCommand(sql,con);
cmd.Parameters.Add(new SqlParameter("@img",img));
int x = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SAVED SUCESSFULLY");
}
}
catch (Exception ex1)
{
con.Close();
MessageBox.Show(ex1.Message);
}
}

private void displayimage_Click(object sender, EventArgs e)
{
string qry = "select * from emp";
cmd = new SqlCommand(qry,con);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
//dataGridView1.RowTemplate.Height = 500;

foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Height = 300;
}

DataGridViewImageColumn image = new DataGridViewImageColumn();
image = (DataGridViewImageColumn)dataGridView1.Columns[0];
image.ImageLayout = DataGridViewImageCellLayout.Stretch;
}


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

//picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();
????????
What kind of code should I write here to on click of datagridview
Cell the image should be automatically shown on the picturebox

}

private void button1_Click(object sender, EventArgs e)
{
if (picEmp.Image != null)
{
Image img = picEmp.Image;
picEmp.Image = null;
img.Dispose();
}

}
}
}





我尝试了什么:



此代码的问题



What I have tried:

problem with this code

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

//picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();
????????
    i above code i have separaete button display on click of it the database pictures are loaded to my datagridview now from datagrid cell click event i want to show up image on my picturebox

What kind of code should I write here to on click of datagridview
Cell the image should be automatically shown on the picturebox

}

推荐答案

使用DataGridViewCellEventArgs参数:它为您提供有关单击了哪个单元格的信息。

DataGridViewCellEventArgs Class( System.Windows.Forms) [ ^ ] - 特别是你想要RowIndex(可能还有ColumnIndex)属性,它告诉你单击了哪个单元格。



从那里和DataGridView Rows属性,您可以将图像数据作为字节数组访问,并将其加载到图片框中:

Uue the DataGridViewCellEventArgs parameter: it gives you information on which cell was clicked.
DataGridViewCellEventArgs Class (System.Windows.Forms)[^] - specifically you want the RowIndex (and possibly ColumnIndex) property, which tells you which cell was clicked on.

From that and the DataGridView Rows property you can access the image data as a byte array, and load that into your picture box:
DataGridViewRow row = myDataGridView.Rows[e.RowIndex];
byte[] data = row.Cells[e.ColumnIndex].Value as byte[];
if (data != null)
    {
    MemoryStream ms = new MemoryStream(data);
    myPictureBox.Image = Image.FromStream(ms);
    }


这篇关于需要C#与datagridview相关的帮助与picturebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆