需要有关datagridview的C#的帮助 [英] Need help for C# related to datagridview

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

问题描述

这是我的数据库名称emp

i有一个字段pic datatye是图像





i有创建了名为UPLOAD,SAVE,DISPLAY的按钮 - 点击UPLOAD按钮将图像上传到图片框。点击SAVE按钮,图像被存储到名为emp的数据库中。图像以< binary>的形式存储。 ...但是点击DISPLAY BUTTON,数据库中的图像显示在DATAGRIDVIEW ...



但是我在datagridview中需要帮助

点击datagridview的任何单元格,datagridview中的图像应显示在我的PICTUREBOX中





这是c#中上传图片的代码

尝试
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter =png文件(* .png)| * .png | jpg文件(*。jpg)| * .jpg |所有文件(*。*)| *。*;
if(dlg.ShowDialog()== DialogResult.OK)
{
imgLoc = dlg.FileName.ToString();
picEmp.ImageLocation = imgLoc;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}





这一个是图片的保存/插入代码

尝试
{
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(例外ex1)
{
con.Close();
MessageBox.Show(ex1.Message);
}





这是点击显示按钮时的显示代码,图像从数据库中提取到datagridview

 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 dataGridView1.Rows中的行)
{
row.Height = 300;
}

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





现在我需要代码如何显示从datagridview到我的图片框的图像onclick特定选定的行



这是代码,但不能正常工作请任何人向我提供正在运行的代码



 private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{

//picEmp.ImageLocation = dataGridView1.Rows [1] .Cells [0] .Value.ToString();


foreach(DataGridViewRow dataGridView1.Rows中的行)
{
DataGridViewRow = dataGridView1.Rows [3];
byte [] data = row.Cells [pic]。值为byte [];
if(data!= null)
{
MemoryStream ms = new MemoryStream(data);
picEmp.Image = Image.FromStream(ms);
}
}

}





我尝试过:



现在我需要代码如何显示图像从datagridview到我的图片框onclick特定选定行

这是代码但不能正常工作请任何人向我提供正在运行的代码

< pre> private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{

//picEmp.ImageLocation = dataGridView1.Rows [1] .Cells [0] .Value.ToString();


foreach(DataGridViewRow dataGridView1.Rows中的行)
{
DataGridViewRow = dataGridView1.Rows [3];
byte [] data = row.Cells [pic]。值为byte [];
if(data!= null)
{
MemoryStream ms = new MemoryStream(data);
picEmp.Image = Image.FromStream(ms);
}
}

}

解决方案

上次问这个问题时 - 今天早上 - 我给了你代码:需要帮助C#与带有图片框的datagridview相关 [ ^ ]

我看到你使用了一些代码,但忽略了选择行和单元格的东西...

here is my database name called emp
i have one field pic datatye is image


i have created buttons called UPLOAD ,SAVE , DISPLAY -- on click of UPLOAD button the image is uploaded to the picturebox. on click of SAVE button the image is stored into database called emp --> image is stored in form of <binary> ...But on click of DISPLAY BUTTON the image from database is shown in DATAGRIDVIEW ...

BUT i Need help in datagridview
on click of any cell of datagridview the image from datagridview should be shown up in my PICTUREBOX


this is code for upload image in c#

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



this one is the save/insert code for image

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



this is a display code on click of display button the image is fetched from database into datagridview

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;



now i need code how to show up the image from datagridview to my picturebox onclick of particular selected row

this is the code but not working properly pls anyone provide me the running code

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            //picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewRow = dataGridView1.Rows[3];
                byte[] data = row.Cells["pic"].Value as byte[];
                if (data != null)
                {
                    MemoryStream ms = new MemoryStream(data);
                    picEmp.Image = Image.FromStream(ms);
                }
            }
           
        }



What I have tried:

now i need code how to show up the image from datagridview to my picturebox onclick of particular selected row

this is the code but not working properly pls anyone provide me the running code 

<pre>private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            //picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewRow = dataGridView1.Rows[3];
                byte[] data = row.Cells["pic"].Value as byte[];
                if (data != null)
                {
                    MemoryStream ms = new MemoryStream(data);
                    picEmp.Image = Image.FromStream(ms);
                }
            }
           
        }

解决方案

When you asked this last time - this morning - I gave you the code: Need help in C# related to datagridview with picturebox[^]
I see you have used a bit of that code, but ignored the stuff that selected the row and cell ...


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

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