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

radgrid with radupload

2 Answers 188 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Loyal User
Top achievements
Rank 1
Loyal User asked on 09 Sep 2013, 06:48 AM
hello , 

i want to create a radgrid with template column that have radbinaryimage as itemtemplate and radupload as edittemplate, 

uploaded images should be saved in db as byte[], 

how can i upload images and save them using item_command event ?? 

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Sep 2013, 06:33 AM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
       OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand">
       <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
           <Columns>
               <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
               </telerik:GridBoundColumn>
               <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
               </telerik:GridBoundColumn>
               <telerik:GridTemplateColumn>
                   <ItemTemplate>
                       <telerik:RadBinaryImage ID="RadBinaryImage1" runat="server" DataValue='<%# Eval("Picture") == null ? null : ((System.Data.Linq.Binary) Eval("Picture")).ToArray() %>' />
                   </ItemTemplate>
                   <EditItemTemplate>
                       <telerik:RadUpload ID="RadUpload1" runat="server">
                       </telerik:RadUpload>
                   </EditItemTemplate>
               </telerik:GridTemplateColumn>
               <telerik:GridEditCommandColumn>
               </telerik:GridEditCommandColumn>
           </Columns>
       </MasterTableView>
   </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
 
       using (DataClassesDataContext _context = new DataClassesDataContext())
       {
           RadGrid1.DataSource = _context.TablePictures.ToList();
       }
 
   }
 
 
 
   protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
   {
       using (DataClassesDataContext _context = new DataClassesDataContext())
       {
 
           GridEditableItem item = e.Item as GridEditableItem;
 
           TablePicture tp = _context.TablePictures.Where(i => i.ID == Convert.ToInt32(item.GetDataKeyValue("ID"))).FirstOrDefault();
            
           tp.Name = (item["Name"].Controls[0] as TextBox).Text;
 
           RadUpload RadUpload1 = item.FindControl("RadUpload1") as RadUpload;
           if (RadUpload1.UploadedFiles.Count > 0)
           {
               UploadedFile attachment = RadUpload1.UploadedFiles[0];
               byte[] attachmentBytes = new byte[attachment.InputStream.Length];
               attachment.InputStream.Read(attachmentBytes, 0, attachmentBytes.Length);
               tp.Picture = attachmentBytes;
           }
 
           _context.SubmitChanges();
       }
   }
   protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
   {
       GridEditableItem item = e.Item as GridEditableItem;
 
       TablePicture tp = new TablePicture();
       tp.ID = Convert.ToInt32((item["ID"].Controls[0] as TextBox).Text);
       tp.Name = (item["Name"].Controls[0] as TextBox).Text;
 
 
       RadUpload RadUpload1 = item.FindControl("RadUpload1") as RadUpload;
       if (RadUpload1.UploadedFiles.Count > 0)
       {
           UploadedFile attachment = RadUpload1.UploadedFiles[0];
           byte[] attachmentBytes = new byte[attachment.InputStream.Length];
           attachment.InputStream.Read(attachmentBytes, 0, attachmentBytes.Length);
           tp.Picture = attachmentBytes;
       }
       using (DataClassesDataContext _context = new DataClassesDataContext())
       {
           _context.TablePictures.InsertOnSubmit(tp);
           _context.SubmitChanges();
       }
   }



Note : i have used Linq2SQL for CRUD operation.

Thanks,
Jayesh Goyani
0
Loyal User
Top achievements
Rank 1
answered on 10 Sep 2013, 06:46 AM
Jayesh Goyanithanks a lot .. 
Tags
Grid
Asked by
Loyal User
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Loyal User
Top achievements
Rank 1
Share this question
or