5 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 27 Aug 2013, 11:03 AM
Hi Kjell,
I'm not sure about your requirement,I guess, during the Update of radgrid, you wanted to check if a column text has changed or the attachment column has file uploaded in it,then only save it to database.Please try the below code snippet.If this doesn't help,please elaborate about your requirement.
ASPX:
C#:
Thanks,
Princy
I'm not sure about your requirement,I guess, during the Update of radgrid, you wanted to check if a column text has changed or the attachment column has file uploaded in it,then only save it to database.Please try the below code snippet.If this doesn't help,please elaborate about your requirement.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
AllowSorting
=
"true"
AllowPaging
=
"true"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
>
<
MasterTableView
DataKeyNames
=
"ID"
>
<
Columns
>
<
telerik:GridEditCommandColumn
HeaderStyle-Width
=
"30px"
>
</
telerik:GridEditCommandColumn
>
<
telerik:GridBoundColumn
DataField
=
"orderid"
HeaderText
=
"orderid"
ReadOnly
=
"true"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"shipname"
HeaderText
=
"shipname"
UniqueName
=
"shipname"
>
</
telerik:GridBoundColumn
>
<
telerik:GridAttachmentColumn
MaxFileSize
=
"1048576"
EditFormHeaderTextFormat
=
"Upload File:"
HeaderText
=
"Download"
UniqueName
=
"AttachmentColumn"
AttachmentDataField
=
"BinaryData"
>
</
telerik:GridAttachmentColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C#:
protected
void
RadGrid1_UpdateCommand(
object
sender, Telerik.Web.UI.GridCommandEventArgs e)
{
string
oldval =
string
.Empty;
string
newval =
string
.Empty;
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
Hashtable newValues =
new
Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, edititem);
oldval = edititem.SavedOldValues[
"shipname"
].ToString();
newval = newValues[
"shipname"
].ToString();
RadUpload upload = (edititem.EditManager.GetColumnEditor(
"AttachmentColumn"
)
as
GridAttachmentColumnEditor).RadUploadControl;
int
filecount= upload.UploadedFiles.Count;
if
((oldval != newval)&&(filecount>0))
{
//Your code to update
}
else
{
//Give your required alert message
}
}
}
Thanks,
Princy
0
Kjell
Top achievements
Rank 1
Iron
answered on 27 Aug 2013, 11:17 AM
I use the "Grid - Attachment Column" on http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/gridattachmentcolumn/defaultvb.aspx
and SqlDataSource1_Updated
and SqlDataSource1_Updated
Protected Sub SqlDataSource1_Updated(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
UpdateInsertFileData("UPDATE [FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID", fileId)
End Sub
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
If e.CommandName = RadGrid.DownloadAttachmentCommandName Then
Dim args As GridDownloadAttachmentCommandEventArgs = TryCast(e, GridDownloadAttachmentCommandEventArgs)
Dim fileName As String = args.FileName
Dim attachmentId As Integer = DirectCast(args.AttachmentKeyValues("ID"), Integer)
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("TelerikConnectionString35").ConnectionString)
Dim comm As New SqlCommand("SELECT [BinaryData] FROM [FileData] WHERE [ID] = @ID", conn)
comm.Parameters.Add(New SqlParameter("@ID", attachmentId))
Dim adapter As New SqlDataAdapter(comm)
Dim data As New DataSet()
adapter.Fill(data)
Dim binaryData As Byte() = DirectCast(data.Tables(0).Rows(0)("BinaryData"), Byte())
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("content-disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(binaryData)
Response.[End]()
End If
End If
If e.CommandName = RadGrid.UpdateCommandName OrElse e.CommandName = RadGrid.PerformInsertCommandName Then
Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
If Not (TypeOf item Is GridEditFormInsertItem) Then
fileId = DirectCast(item.GetDataKeyValue("ID"), Integer)
End If
Dim fileName As String = (TryCast(item.EditManager.GetColumnEditor("FileName"), GridTextBoxColumnEditor)).Text
fileData = (TryCast(item.EditManager.GetColumnEditor("AttachmentColumn"), GridAttachmentColumnEditor)).UploadedFileContent
Dim uploadDate As DateTime? = (TryCast(item.EditManager.GetColumnEditor("UploadDate"), GridDateTimeColumnEditor)).PickerControl.SelectedDate
Dim uploadedBy As String = (TryCast(item.EditManager.GetColumnEditor("UploadedBy"), GridTextBoxColumnEditor)).TextBoxControl.Text
If fileData.Length = 0 OrElse fileName.Trim() = String.Empty Then
e.Canceled = True
RadGrid1.Controls.Add(New LiteralControl("<
b
style
=
'color:red;'
>No file uploaded. Action canceled.</
b
>"))
End If
If Not uploadDate.HasValue Or String.IsNullOrEmpty(uploadedBy.Trim()) Then
e.Canceled = True
RadGrid1.Controls.Add(New LiteralControl("<
b
style
=
'color:red;'
>Please, fill in all the fields.</
b
>"))
End If
End Sub
0
Hello Kjell,
I am afraid that I am still not able to completely understand your requirement. The code which you provided will cancel the insert command if some of the the uploaded by or upload date is empty. If that is your requirement you could use the same approach. If it is not than, could you please explain in details what you are trying to achieve in order to give you more to the point answer?
Regards,
Kostadin
Telerik
I am afraid that I am still not able to completely understand your requirement. The code which you provided will cancel the insert command if some of the the uploaded by or upload date is empty. If that is your requirement you could use the same approach. If it is not than, could you please explain in details what you are trying to achieve in order to give you more to the point answer?
Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Kjell
Top achievements
Rank 1
Iron
answered on 30 Aug 2013, 06:46 AM
Excuse my bad English, the same problem I find in this thread, (http://www.telerik.com/community/forums/aspnet-ajax/grid/gridattachmentcolumn-on-edit.aspx) but have a problem when I convert the code to vb.
Have the same problem and have to convert the code to VB on converter.telerik.com.
But my problem is when I update a field without uploading a new file, I get the message that the file can not be read when I click on the link to open it after the update.
Create new record works, but not to update without having to upload a new file.
Have the same problem and have to convert the code to VB on converter.telerik.com.
But my problem is when I update a field without uploading a new file, I get the message that the file can not be read when I click on the link to open it after the update.
Create new record works, but not to update without having to upload a new file.
0
Hi Kjell,
One of my colleagues already answered your question in the other forum post, so please continue the conversation there if any further questions arise.
Regards,
Kostadin
Telerik
One of my colleagues already answered your question in the other forum post, so please continue the conversation there if any further questions arise.
Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.