问题描述
这是一个问题,并没有从我身上解开,我在MS Sqlserver-2012的桌子上有一列 person_image varbinary(max),现在问题是我必须将此列绑定到我在windows窗体中的datagridview,实际上在将此二进制信息绑定到Datagridview之前,需要将其转换为图像,然后,我必须将它们绑定到datagridview,如同在其列中的图像所示,我也尝试使用datagridviewimagecolumn ...但它与破碎的图像绑定,实际上并没有真实的图像..还有另外一点要记住..我没有图像的路径,我在我的桌子上只有二进制图像在Back-结束..实际上我可以得到图像的路径...但这是唯一要做的事情...我在绑定之前无法将它们(二进制)转换为图像吗?我试图解决这个问题长达2天..但是在不同网站上找到的代码在修改我的整个应用程序代码方面变得越来越复杂......
这是我的代码ows并且还抛出一个异常,提到对象引用没有设置为实例在试用块上
Here is a problem which doesn't un-sticks from me ,,i have a column of person_image varbinary(max)at my table in MS Sqlserver-2012,, now the thing is i have to bind this column to my datagridview in windows forms, actually while before binding this binary information to Datagridview ,It's needs to be converted into images and then, i have to bind them to datagridview as shows as images in it's column,,i also tried with datagridviewimagecolumn...but it was binding with broken images,not actually with real images..here is another point to remember..i don't have the paths of images,i have only binary images at my table in Back-End..Infact i could get the paths of images...but is that is the only thing to do...did i can't convert them(binary) in to images before binding..?I am trying to fix this for long 2 days..but what the code found in different sites was getting complicated in the way of modifying my whole application code...
Here is the code which i follows and also it was throwing an exception that mentioning an object reference was not set to an instance at try block
public void BindDataGrid()
{
string bindQuery = "select * from employee";
_cmd = new SqlCommand();
_cmd.CommandText = bindQuery;
_cmd.Connection = _con;
_con.Open();
_da = new SqlDataAdapter(_cmd);
_ds = new DataSet();
DataGridViewImageColumn dgi = new DataGridViewImageColumn();
dgi.Name = "image";
dgi.HeaderText = "Person_image";
dgi.ImageLayout = DataGridViewImageCellLayout.Zoom;
try
{
dgi.Image = bytearraytoimage((byte[])_ds.Tables["employee"].Rows[1]["image"]);
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
dataGridView1.Columns.Insert(0,dgi);
_con.Close();
_da.Fill(_ds);
dataGridView1.DataSource = _ds.Tables[0];
}
private Image bytearraytoimage(byte[] b)
{
MemoryStream ms=new MemoryStream(b);
Image img=Image.FromStream(ms);
return img;
}
private void Form1_Load(object sender, EventArgs e)
{
BindDataGrid();
}
我会如此感恩,如果我有一个聪明的代码来实现这个任务....任何建议..请... ....继续...
I will be so thankful,, if i have a smart code to achieve this task....any suggestions..please....go-on...
推荐答案
这篇关于如何将varbinary数据类型绑定到winforms中的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!