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

Batchediting with GridAttachmentcolumn - not able to find file name

1 Answer 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rajshree
Top achievements
Rank 1
Rajshree asked on 08 Jul 2015, 02:50 AM

I am trying to use GridAttachemntColumn in grid with batcheditng. i tried to find out the file name in prior to store in db. but not able to do it.please find code snippet for further detail.

 

protected void gridProjectManagement_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
      {
          int s = e.Commands.Count();
          foreach (GridBatchEditingCommand command in e.Commands)
          {
 
              Hashtable newValues = command.NewValues;
              Hashtable oldValues = command.OldValues;
              //RadAsyncUpload rd = command.Item["Attachment"].Controls[0] as RadAsyncUpload;
              //string file = rd.UploadedFiles[0].FileName;
              //command.Item.EditManager.GetColumnEditor("Attachment")
              //var gridAttachmentColumnEditor = ((GridAttachmentColumnEditor)command.Item.EditManager.GetColumnEditor("Attachment"));
              //string file = gridAttachmentColumnEditor.RadUploadControl.UploadedFiles[0].GetName();
              string id = (string)newValues["ID"];
                  //Convert.ToInt64(newValues["ID"].ToString());
              
                 
              // string newFirstName = newValues["PerspectiveRating"].ToString();
              if (!string.IsNullOrEmpty(id))
              {
                  var OldeItem = ListActivityArchivalSupportPM.Where(x => x.ID == Convert.ToInt32(id)).FirstOrDefault();
               
                  using (var db = new EntitiesModel())
                  {
                      var p = db.ActivityArchivalSupportPMs.Where(x => x.ID == Convert.ToInt32(id)).FirstOrDefault();
                      if (newValues["Estimate"] != null)
                      {
                          p.Estimate = Convert.ToDecimal(newValues["Estimate"].ToString());
                      }
 
 
                      p.Activity = (string)newValues["Activity"];
                      p.EstimateAssumptions = (string)newValues["EstimateAssumptions"];
                      p.ApplicationID = AppName.Value;
                      if (newValues["FileName"] != null)
                      {
                          p.Attachment = (Byte[])newValues["FileName"];
                      }

<telerik:RadGrid ID="gridProjectManagement" GridLines="None" runat="server" AllowAutomaticDeletes="false"
            AllowAutomaticInserts="True" AllowSorting="true" AllowFilteringByColumn="true" FilterType="CheckList" EnableLinqExpressions="false"
            OnItemCreated="gridProjectManagement_ItemCreated" OnPreRender="gridProjectManagement_PreRender" AllowAutomaticUpdates="True"
            AutoGenerateColumns="False" OnBatchEditCommand="gridProjectManagement_BatchEditCommand" OnItemCommand="gridProjectManagement_ItemCommand" OnNeedDataSource="gridProjectManagement_NeedDataSource" OnItemDataBound="gridProjectManagement_ItemDataBound" >
           <ExportSettings ExportOnlyData="true" HideStructureColumns="true" OpenInNewWindow="true" IgnorePaging="true"><Excel Format="Html" /></ExportSettings>
             
          <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID"   Font-Size="Small" CommandItemSettings-ShowAddNewRecordButton="false" CommandItemSettings-ShowRefreshButton="false"
                HorizontalAlign="NotSet" EditMode="Batch" AutoGenerateColumns="False" AllowFilteringByColumn="True">
                                  <CommandItemSettings ShowExportToCsvButton="true" ShowExportToExcelButton="true" ShowAddNewRecordButton="true" ShowExportToPdfButton="true" ShowExportToWordButton="true" />
 
                <BatchEditingSettings EditType="Cell" OpenEditingEvent="Click" />
                  
<%--                <SortExpressions>
                    <telerik:GridSortExpression FieldName="PerspModel_ID" SortOrder="Descending" />
                </SortExpressions>--%>
                <Columns>
                      
                    <telerik:GridBoundColumn DataField="ID" ForceExtractValue="Always" Display="false"  HeaderText="ID"
                        UniqueName="ID">
                    </telerik:GridBoundColumn>
                    
                     <telerik:GridBoundColumn DataField="Activity" AllowSorting="true" SortExpression="Activity"  HeaderStyle-Width="150px" HeaderText="Activity"
                        UniqueName="Activity">
                    </telerik:GridBoundColumn>
                    
                     
 
                     
                     <telerik:GridNumericColumn DataField="Estimate" HeaderStyle-Width="80px" AllowFiltering="false" AllowSorting="false"   DataType="System.Decimal"  DecimalDigits="1"  HeaderText="Estimate(hours)"
                        SortExpression="Estimate" UniqueName="Estimate">
                    </telerik:GridNumericColumn>
                  
                     <telerik:GridTemplateColumn ColumnEditorID="EstimateAssumptions" ItemStyle-CssClass="breakWord120" UniqueName="EstimateAssumptions" ItemStyle-Width="100px" AllowFiltering="false" HeaderStyle-Width="150px"  HeaderText="Estimate Assumptions">
                        <ItemTemplate>
                        <%# Eval("EstimateAssumptions")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                             <telerik:RadTextBox ID="txtEditEstimateAssumptions"  Height="50px"  Wrap="true"  TextMode="MultiLine"  ToolTip='<%# Eval("EstimateAssumptions")%>' runat="server" Text='<%# Eval("EstimateAdjustmentRationale")%>'></telerik:RadTextBox>
                      
                             </EditItemTemplate>
                     </telerik:GridTemplateColumn>
                   
                   <telerik:GridAttachmentColumn 
                        EditFormHeaderTextFormat="Upload File:" HeaderStyle-Width="150px" HeaderText="Attachment" AttachmentDataField="Attachment"
                        AttachmentKeyFields="ID" FileNameTextField="FileName" DataTextField="FileName"
                        UniqueName="Attachment">
                    </telerik:GridAttachmentColumn>
 
                </Columns>
 
            </MasterTableView>
            <ClientSettings>
                <Scrolling AllowScroll="true" UseStaticHeaders="true" ScrollHeight="300px" SaveScrollPosition="true"></Scrolling>
                                 <%-- <ClientEvents OnBatchEditOpened="OnBatchEditOpened" OnBatchEditOpening="BatchEditOpening"   />--%>
 
               
          <%--  <ClientEvents OnCommand="gridCommand"></ClientEvents>--%>
        </ClientSettings>
        </telerik:RadGrid>

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 10 Jul 2015, 02:50 PM
Hello,

If I am correct you should be able to extract the file name using the following code.

C#:
protected void gridProjectManagement_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
{
    foreach (GridBatchEditingCommand command in e.Commands)
    {
 
        Hashtable newValues = command.NewValues;
        Hashtable oldValues = command.OldValues;
        RadAsyncUpload upload = (RadGrid1.MasterTableView.GetBatchEditorContainer("Attachment").Controls[0] as RadAsyncUpload) as RadAsyncUpload;
        foreach (UploadedFile file in upload.UploadedFiles)
        {
            byte[] result;
            using (var streamReader = new MemoryStream())
            {
                file.InputStream.CopyTo(streamReader);
                result = streamReader.ToArray();
            }
            if (Equality(result, (byte[])newValues["FileName"]))
            {
                string fileName = file.FileName;
            }
        }
    }
}
 
public bool Equality(byte[] a1, byte[] b1)
{
    int i;
    if (a1.Length == b1.Length)
    {
        i = 0;
        while (i < a1.Length && (a1[i] == b1[i])) //Earlier it was a1[i]!=b1[i]
        {
            i++;
        }
        if (i == a1.Length)
        {
            return true;
        }
    }
 
    return false;
}


Regards,
Angel Petrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Rajshree
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or