This is a migrated thread and some comments may be shown as answers.

Working with Image Columns in the Grid

2 Answers 144 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremy Pryor
Top achievements
Rank 1
Jeremy Pryor asked on 21 Dec 2009, 11:08 PM
I have been looking at creating an control similar to the one found in the demos for the grid view.  What I need is help determining how to store an image in my SQL Server database, retrieve it as a part of an employee record, display it as in the demo, but have the ability to click on a button (possibly a column header) and then change the image.  We have formal and informal shots of every employee and want to be able to toggle between the 2.  Please help.  I can't make a lot of since from the online demo, because I can't seem to find the Photo datasource the code is referencing and where that comes from.

Thanks,

JP

2 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 24 Dec 2009, 02:51 PM
Hello Jeremy Pryor,

You can use this code snippet for your scenario:

private int rowIndex = -1;
 
void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
{
    if (((GridViewDataColumn)e.Column).UniqueName == "Photo")
    {
        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*";
        if (DialogResult.OK == openDialog.ShowDialog())
        {
            Image image = Image.FromFile(openDialog.FileName);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            this.radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = ms.ToArray();
        }
 
        rowIndex = e.RowIndex;
        return;
    }
 
    rowIndex = -1;
}
 
void saveButton_Click(object sender, EventArgs e)
{
    if (rowIndex != -1)
    {
        this.employeesTableAdapter.Update(((DataRowView)this.radGridView1.Rows[rowIndex].DataBoundItem).Row as Telerik.Examples.WinControls.DataSources.NorthwindDataSet.EmployeesRow);
    }
}

I hope this helps.

Kind regards,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jeremy Pryor
Top achievements
Rank 1
answered on 28 Dec 2009, 04:25 PM
Thank you so much for your help!  This sheds some great light on a lot of my questions!  :-)
Tags
GridView
Asked by
Jeremy Pryor
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Jeremy Pryor
Top achievements
Rank 1
Share this question
or