如何在gridview中更新二进制图像。下面将详细解释该问题 [英] How do I update a binary image in gridview .the problem is explained in details below

查看:57
本文介绍了如何在gridview中更新二进制图像。下面将详细解释该问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



作为C#的初学者,我正在尝试网格视图的不同可能性,我陷入了局面。任何人都有助于解决这个问题?


i已将二进制图像存储在数据库中并检索到网格视图,到目前为止一直很好,

当我试图更新网格视图中的内容:

在我的网格视图中有五个字段(productid,productname,price,productpic,description)



对于文本:TextBox productname =(TextBox)GridView1.Rows [e.RowIndex] .Cells [1] .Controls [0];



当它到来时到图像字段是productpic我卡住了,我不知道哪个代码适用于更新二进制编码图像



有人可以帮我找到解决方案吗? />


我尝试了什么:



string images =data:Image / png; base64,+ Convert.ToBase64String((byte [])Eval(productpic));





图片images = GridVi ew1.Rows [e.RowIndex] .Cells [3] .Controls [0];

解决方案
你需要有一个文件上传控制 EditItemTemplat e以便您可以浏览新图像。然后您可以更新数据库中的图像,就像更新数据库中的普通数据一样。我们的想法是获取保存图像的行的现有 ID ,然后使用新图像更新它(更新路径或图像本身〜取决于如何你存储图像)。



例如你的 GridView 会有这样的东西:

 <   asp:TemplateField     HeaderText   = 图片 >  
< ItemTemplate >
< asp:Image runat = server ID = Image1 / >
< / ItemTemplate >
< EditItemTemplate >
< asp:FileUpload ID = FileUpload1 runat = server / >
< / EditItemTemplate >
< / asp:TemplateField >





自您将图像存储为二进制文件以显示它们,然后您不必访问图像本身更新它。相反,您只需要更新数据库中的数据并重新绑定 GridView 以反映更改。下面是我很多年前写了一个例子:的常见问题:将图像从数据库显示到GridView控件 [ ^ ]



这是一个简单的例子:

  private   void 更新( int  ID,字符串名称,十进制价格, string  description, byte  [] image){
using (SqlConnection connection = new SqlConnection( 您的CO NNECTION STRING HERE)){
string sql = UPDATE YourTableName SET Name = @ Name,Price = @Price,Description = @ Description,Image = @Image WHERE ID = @ID;
使用(SqlCommand cmd = new SqlCommand(sql,connection)){
cmd.Parameters.AddWithValue( @ ID,ID);
cmd.Parameters.AddWithValue( @ Name,name);
cmd.Parameters.AddWithValue( @ Price,price);
cmd.Parameters.AddWithValue( @ Description,description);
cmd.Parameters.AddWithValue( @ Image,image);
cmd.ExecuteNonQuery();
}
}
}

受保护 void GridView1_RowEditing( object sender,GridViewEditEventArgs e)
{
// 转向编辑模式
GridView1.EditIndex = e.NewEditIndex;

// 调用重新绑定GridView的方法以反映所做的更改
BindGridView();
}

protected void GridView1_RowUpdating( object sender,GridViewUpdateEventArgs e)
{
// 从GridView访问已编辑的值

字符串 id = GridView1.Rows [e.RowIndex] .Cells [ 0 ]文本。 // ID
string name =((TextBox)GridView1.Rows [e.RowIndex] .Cells [ 1 ]。控件[ 0 ])文本。 // productname
string price =((TextBox)GridView1.Rows [e.RowIndex] .Cells [ 2 ]。控件[ 0 ])文本。 // price
FileUpload fileUpload =(FileUpload)GridView1.Rows [e.RowIndex]。单元格[ 3 ]。FindControl( FileUpload1); // FileUpload control
string description =((TextBox)GridView1.Rows [e.RowIndex] .Cells [ 4 ]。控件[ 0 ])文本。 // description

/ / 获取新上传的图像流
byte [] theImage = new byte [fileUpload.PostedFile.ContentLength];
HttpPostedFile Image = fileUpload.PostedFile;
Image.InputStream.Read(theImage, 0 ,( int )fileUpload.PostedFile.ContentLength );
int length = theImage.Length; // 获取图片的长度
string fileName = fileUpload.FileName.ToString(); // 获取已发布图片的文件名
string type = fileUpload.PostedFile.ContentType; // 获取已发布图片的类型
int size = fileUpload.PostedFile.ContentLength; // 获取字节大小
if (fileUpload.PostedFile!= null && fileUpload.PostedFile.FileName!=
{
// 调用更新方法
更新(id,name,price,description,theImage);
}

// 将状态恢复为只读
GridView1.EditIndex = -1;

// 调用重新绑定GridView的方法以反映所做的更改
BindGridView();
}


hello friends,

As a beginner in C# ,i am trying different possibilities of grid-view and i am stuck with situation.can anybody help to solve this?

i have stored a binary image in database and retrieved to the grid-view,so far so good,
when i have tried to update the content in the grid-view:
in my grid-view there are five fields (productid,productname,price,productpic,description)

For text: TextBox productname =(TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];

when it comes to image field that is productpic i have stuck,i don't know which code works for update a binary coded image

can anybody help me to find solution?

What I have tried:

string images = "data:Image/png;base64," + Convert.ToBase64String((byte[])Eval("productpic"));


Image images = GridView1.Rows[e.RowIndex].Cells[3].Controls[0];

解决方案

You need to to have a FileUpload control within EditItemTemplate so you can browse new image.You can then update the image from your database just like updating normal data in your database. The idea is to get the existing ID of the row that holds your image and then update it with the new image (either update the path or the image itself ~ that depends on how you store the images).

For example your GridView would have something like this:

<asp:TemplateField HeaderText="Image">
          <ItemTemplate>
                   <asp:Image  runat="server" ID="Image1" />
          </ItemTemplate>
          <EditItemTemplate>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
          </EditItemTemplate>
</asp:TemplateField>



Since you were storing the image as Binary file to display them, then you don't have to access the Image itself to update it. Instead, you just need to update the data from database and rebind your GridView to reflect the changes. Here's an example that I wrote many years ago: FAQ: Displaying Image from Database to GridView Control[^]

Here's a quick example:

private void Update(int ID, string name, decimal price, string description,byte[] image){
	using (SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")) {
            string sql = "UPDATE YourTableName SET Name = @Name, Price = @Price, Description = @Description, Image = @Image WHERE ID = @ID";
            using (SqlCommand cmd = new SqlCommand(sql, connection)) {
			    cmd.Parameters.AddWithValue("@ID", ID);
	            cmd.Parameters.AddWithValue("@Name", name);
			    cmd.Parameters.AddWithValue("@Price", price);
			    cmd.Parameters.AddWithValue("@Description", description);
			    cmd.Parameters.AddWithValue("@Image", image);
			    cmd.ExecuteNonQuery();
            }
 	}
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
	// turn to edit mode
        GridView1.EditIndex = e.NewEditIndex; 

	// Call the method for re-binding GridView to reflect changes made
        BindGridView(); 
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        //Accessing Edited values from the GridView

        string id = GridView1.Rows[e.RowIndex].Cells[0].Text; //ID
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; //productname
        string price = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; //price
        FileUpload fileUpload = (FileUpload)GridView1.Rows[e.RowIndex].Cells[3].FindControl("FileUpload1"); //FileUpload control
        string description = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text; //description

	//get newly uploaded image stream
	byte[] theImage = new byte[fileUpload.PostedFile.ContentLength];
        HttpPostedFile Image = fileUpload.PostedFile;
        Image.InputStream.Read(theImage, 0, (int)fileUpload.PostedFile.ContentLength);
        int length = theImage.Length; //get the length of the image
        string fileName = fileUpload.FileName.ToString(); //get the file name of the posted image
        string type = fileUpload.PostedFile.ContentType; //get the type of the posted image
        int size = fileUpload.PostedFile.ContentLength; //get the size in bytes that
        if (fileUpload.PostedFile != null && fileUpload.PostedFile.FileName != "")
        {
		// call update method
        	Update(id,name,price,description,theImage); 
        }

	//revert the state to read-only
        GridView1.EditIndex = -1;

	// Call the method for re-binding GridView to reflect changes made
        BindGridView(); 
}


这篇关于如何在gridview中更新二进制图像。下面将详细解释该问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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