GridCommandEventArgs 的项没有数据值。

huangapple go评论45阅读模式
英文:

GridCommandEventArgs items have no data values

问题

我有一个RadGrid,需要在其中添加、更新和删除数据。我已经使功能能够运行,但我还需要在数据被添加/更新之前对其进行一些检查,包括基于其他数据设置字段。我还希望记录所做的操作。

我的插入功能正在运行。RadGrid如下所示:

<telerik:RadGrid ID="CarrierRadGrid" runat="server" AllowFilteringByColumn="True" AllowPaging="False" Width="100%" Height="800px" PageSize="20" da
                AllowSorting="True" DataSourceID="CarrierData" ShowStatusBar="True"
                OnInsertCommand="CarrierRadGrid_InsertCommand" OnItemInserted="CarrierRadGrid_ItemInserted" AllowAutomaticInserts="true"
                OnUpdateCommand="CarrierRadGrid_UpdateCommand" OnItemUpdated="CarrierRadGrid_ItemUpdated" AllowAutomaticUpdates="true"
                OnDeleteCommand="CarrierRadGrid_DeleteCommand" OnItemDeleted="CarrierRadGrid_ItemDeleted" AllowAutomaticDeletes="true"
                Skin="Telerik" AutoGenerateColumns="False" AutoGenerateEditColumn="false" AutoGenerateDeleteColumn="false">
                <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="false">
                    <Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="true" FrozenColumnsCount="5" />
                    <Selecting AllowRowSelect="true" />
                    <Resizing AllowColumnResize="true" AllowResizeToFit="true" />
                    <ClientEvents OnRowDeleting="void 0" />
                </ClientSettings>
                <GroupingSettings CaseSensitive="false" />
                <ExportSettings HideStructureColumns="true">
                </ExportSettings>
                <HeaderContextMenu EnableAutoScroll="False">
                </HeaderContextMenu>
                <MasterTableView DataKeyNames="CarrierScacID" DataSourceID="CarrierData" AllowAutomaticInserts="true" AllowAutomaticDeletes="True" AllowAutomaticUpdates="true"
                    CommandItemDisplay="Top" HierarchyLoadMode="ServerOnDemand" EditMode="InPlace">
                    <CommandItemSettings
                        ShowExportToWordButton="false"
                        ShowExportToCsvButton="false"
                        ShowExportToPdfButton="false"
                        ShowAddNewRecordButton="true">
                    </CommandItemSettings>
                    <Columns>
                        <!-- 列定义 -->
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>

在代码后台,我有以下内容:

Protected Sub CarrierRadGrid_InsertCommand(sender As Object, e As GridCommandEventArgs)
    e = SetCommentsField(e)
End Sub

Protected Sub CarrierRadGrid_ItemInserted(sender As Object, e As GridInsertedEventArgs)
    Dim msg As String = String.Format("New carrier (Carrier Code: {0}) added to the database.", e.Item.Cells(6).Text)
    Log(msg)
    MessageBox(msg)
End Sub

Private Shared Function SetCommentsField(e As GridCommandEventArgs) As GridCommandEventArgs
    If String.IsNullOrEmpty(e.Item.Cells(18).Text) Then
        Dim update As String = e.Item.Cells(17).Text
        If Not String.IsNullOrEmpty(update) Then
            e.Item.Cells(18).Text = String.Format("Modified by {0}", update)
        End If
    End If
    Return e
End Function

但是在调试时,我发现所有的e.Item.CellsText属性都是"\&amp;nbsp;"。然而,添加到数据库的记录中包含我放入字段的数据,这是应该的。

可能是什么缺失?或者多了什么?

英文:

I have a RadGrid with which I need to add, update, and delete data. I've gotten the functionality to work, but I also need to do some checking on the data before it gets added/updated, including setting a field based on other data. I also want to log what was done.

My Insert functionality is working. The RadGrid is as follows:

            &lt;telerik:RadGrid ID=&quot;CarrierRadGrid&quot; runat=&quot;server&quot; AllowFilteringByColumn=&quot;True&quot; AllowPaging=&quot;False&quot; Width=&quot;100%&quot; Height=&quot;800px&quot; PageSize=&quot;20&quot; da
AllowSorting=&quot;True&quot; DataSourceID=&quot;CarrierData&quot; ShowStatusBar=&quot;True&quot;
OnInsertCommand=&quot;CarrierRadGrid_InsertCommand&quot; OnItemInserted=&quot;CarrierRadGrid_ItemInserted&quot; AllowAutomaticInserts=&quot;true&quot;
OnUpdateCommand=&quot;CarrierRadGrid_UpdateCommand&quot; OnItemUpdated=&quot;CarrierRadGrid_ItemUpdated&quot; AllowAutomaticUpdates=&quot;true&quot;
OnDeleteCommand=&quot;CarrierRadGrid_DeleteCommand&quot; OnItemDeleted=&quot;CarrierRadGrid_ItemDeleted&quot; AllowAutomaticDeletes=&quot;true&quot;
Skin=&quot;Telerik&quot; AutoGenerateColumns=&quot;False&quot; AutoGenerateEditColumn=&quot;false&quot; AutoGenerateDeleteColumn=&quot;false&quot;&gt;
&lt;ClientSettings EnableRowHoverStyle=&quot;true&quot; EnablePostBackOnRowClick=&quot;false&quot;&gt;
&lt;Scrolling AllowScroll=&quot;true&quot; UseStaticHeaders=&quot;true&quot; SaveScrollPosition=&quot;true&quot; FrozenColumnsCount=&quot;5&quot; /&gt;
&lt;Selecting AllowRowSelect=&quot;true&quot; /&gt;
&lt;Resizing AllowColumnResize=&quot;true&quot; AllowResizeToFit=&quot;true&quot; /&gt;
&lt;ClientEvents OnRowDeleting=&quot;void 0&quot; /&gt;
&lt;/ClientSettings&gt;
&lt;GroupingSettings CaseSensitive=&quot;false&quot; /&gt;
&lt;ExportSettings HideStructureColumns=&quot;true&quot;&gt;
&lt;/ExportSettings&gt;
&lt;HeaderContextMenu EnableAutoScroll=&quot;False&quot;&gt;
&lt;/HeaderContextMenu&gt;
&lt;MasterTableView DataKeyNames=&quot;CarrierScacID&quot; DataSourceID=&quot;CarrierData&quot; AllowAutomaticInserts=&quot;true&quot; AllowAutomaticDeletes=&quot;True&quot; AllowAutomaticUpdates=&quot;true&quot;
CommandItemDisplay=&quot;Top&quot; HierarchyLoadMode=&quot;ServerOnDemand&quot; EditMode=&quot;InPlace&quot;&gt;
&lt;CommandItemSettings
ShowExportToWordButton=&quot;false&quot;
ShowExportToCsvButton=&quot;false&quot;
ShowExportToPdfButton=&quot;false&quot;
ShowAddNewRecordButton=&quot;true&quot;&gt;
&lt;/CommandItemSettings&gt;
&lt;Columns&gt;
&lt;telerik:GridEditCommandColumn ButtonType=&quot;ImageButton&quot; CancelText=&quot;Cancel&quot; EditText=&quot;Edit&quot; InsertText=&quot;Add&quot;
UpdateText=&quot;Update&quot; HeaderStyle-Width=&quot;30px&quot; UniqueName=&quot;CarrierEditButton&quot; /&gt;
&lt;telerik:GridClientDeleteColumn ButtonType=&quot;ImageButton&quot; Text=&quot;Delete&quot; HeaderStyle-Width=&quot;30px&quot; UniqueName=&quot;CarrierDeleteButton&quot;
ConfirmTextFields=&quot;CarrierScac&quot; ConfirmTextFormatString=&quot;Are you sure you want to delete carrier {0}?&quot; CommandName=&quot;Delete&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;CarrierScacID&quot; HeaderText=&quot;CarrierScacID&quot; UniqueName=&quot;CarrierScacID&quot; ReadOnly=&quot;True&quot; Display=&quot;false&quot; /&gt;
&lt;telerik:GridTemplateColumn&gt;
&lt;EditItemTemplate&gt;
&lt;telerik:RadLabel runat=&quot;server&quot; ID=&quot;CarrierScac&quot; Text=&#39;&lt;% #Bind(&quot;CarrierScac&quot;) %&gt;&#39; ViewStateMode=&quot;Disabled&quot; Enabled=&quot;false&quot; /&gt;
&lt;/EditItemTemplate&gt;
&lt;InsertItemTemplate&gt;
&lt;telerik:RadTextBox ID=&quot;CarrierScac&quot; runat=&quot;server&quot; Text=&#39;&lt;% #Bind(&quot;CarrierScac&quot;) %&gt;&#39; /&gt;
&lt;/InsertItemTemplate&gt;
&lt;ItemTemplate&gt;
&lt;telerik:RadLabel runat=&quot;server&quot; ID=&quot;CarrierScac&quot; Text=&#39;&lt;% #Bind(&quot;CarrierScac&quot;) %&gt;&#39; /&gt;
&lt;/ItemTemplate&gt;
&lt;/telerik:GridTemplateColumn&gt;
&lt;telerik:GridBoundColumn DataField=&quot;CarrierScac&quot; HeaderText=&quot;SCAC&quot; UniqueName=&quot;CarrierScac&quot; FilterControlWidth=&quot;40px&quot; HeaderStyle-Width=&quot;75px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;CarrierName&quot; HeaderText=&quot;Carrier Name&quot; UniqueName=&quot;CarrierName&quot; ItemStyle-Wrap=&quot;false&quot;
HeaderStyle-Width=&quot;200px&quot; FilterControlWidth=&quot;160px&quot; FilterControlAltText=&quot;Filter by Carrier Name&quot; SortExpression=&quot;CarrierName&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;Address&quot; HeaderText=&quot;Address&quot; UniqueName=&quot;Address&quot; ItemStyle-Wrap=&quot;false&quot;
HeaderStyle-Width=&quot;200px&quot; FilterControlWidth=&quot;160px&quot; FilterControlAltText=&quot;Filter by Address&quot; SortExpression=&quot;Address&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;City&quot; HeaderText=&quot;City&quot; UniqueName=&quot;City&quot; HeaderStyle-Width=&quot;100px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;State&quot; HeaderText=&quot;State&quot; UniqueName=&quot;State&quot; FilterControlWidth=&quot;30px&quot; HeaderStyle-Width=&quot;65px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;ZIP&quot; HeaderText=&quot;ZIP&quot; UniqueName=&quot;ZIP&quot; FilterControlWidth=&quot;55px&quot; HeaderStyle-Width=&quot;90px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;Country&quot; HeaderText=&quot;Country&quot; UniqueName=&quot;Country&quot; FilterControlWidth=&quot;55px&quot; HeaderStyle-Width=&quot;90px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;Contact&quot; HeaderText=&quot;Contact&quot; UniqueName=&quot;Contact&quot; ItemStyle-Wrap=&quot;false&quot; HeaderStyle-Width=&quot;110px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;Phone&quot; HeaderText=&quot;Phone&quot; UniqueName=&quot;Phone&quot; ItemStyle-Wrap=&quot;false&quot; HeaderStyle-Width=&quot;115px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;Fax&quot; HeaderText=&quot;Fax&quot; UniqueName=&quot;Fax&quot; ItemStyle-Wrap=&quot;false&quot; HeaderStyle-Width=&quot;115px&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;User&quot; HeaderText=&quot;User&quot; UniqueName=&quot;User&quot; HeaderStyle-Width=&quot;120px&quot; ReadOnly=&quot;true&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;Update&quot; HeaderText=&quot;Update&quot; UniqueName=&quot;Update&quot; ItemStyle-Wrap=&quot;false&quot; HeaderStyle-Width=&quot;230px&quot; ReadOnly=&quot;true&quot; /&gt;
&lt;telerik:GridBoundColumn DataField=&quot;Comments&quot; HeaderText=&quot;Comments&quot; UniqueName=&quot;Comments&quot; ItemStyle-Wrap=&quot;false&quot; HeaderStyle-Width=&quot;230px&quot; ReadOnly=&quot;true&quot; /&gt;
&lt;/Columns&gt;
&lt;/MasterTableView&gt;
&lt;/telerik:RadGrid&gt;

In code-behind, I have the following:

    Protected Sub CarrierRadGrid_InsertCommand(sender As Object, e As GridCommandEventArgs)
e = SetCommentsField(e)
End Sub
Protected Sub CarrierRadGrid_ItemInserted(sender As Object, e As GridInsertedEventArgs)
Dim msg As String = String.Format(&quot;New carrier (Carrier Code: {0}) added to the database.&quot;, e.Item.Cells(6).Text)
Log(msg)
MessageBox(msg)
End Sub
Private Shared Function SetCommentsField(e As GridCommandEventArgs) As GridCommandEventArgs
If String.IsNullOrEmpty(e.Item.Cells(18).Text) Then
Dim update As String = e.Item.Cells(17).Text
If Not String.IsNullOrEmpty(update) Then
e.Item.Cells(18).Text = String.Format(&quot;Modified by {0}&quot;, update)
End If
End If
Return e
End Function

But when debugging, I find all of the e.Item.Cells Text properties are "&amp;nbsp;". Yet the record added to the database has the data I put into the fields, as it should.

What could be missing? Or extra?

EDIT: Including the whole RadGrid, instead of only the part I thought was relevant.

答案1

得分: 1

以下是两种访问单元格数据的选项。

  1. 使用散列表/字典访问新插入的数据。它仅从EditItems中检索数据,因此不包括只读行。
  2. 将您的editItem转换为数据项,然后访问当前单元格,您会发现它始终是&amp;nbsp;,因为数据尚未放入其中。输入的数据只存在于编辑项中。
    Protected Sub CarrierRadGrid_InsertCommand(sender As Object, e As GridCommandEventArgs)
        &#39;1
        Dim insertValues As New Hashtable()
        CType(e.Item, GridEditableItem).ExtractValues(insertValues)
        Dim dd = insertValues(&quot;CarrierScac&quot;)
        
        &#39;2
        Dim item As GridDataItem = CType(e.Item, GridDataItem)
        Dim itemD = item(&quot;CarrierScac&quot;).Text
        e = SetCommentsField(e)
    End Sub

上述代码应该满足您的需求,不过为了增加一些细节:

Telerik使用&amp;nbsp;来表示空/Null单元格文本字段,这在这里是有道理的,因为您试图获取的单元格文本不存在。它确实是空的,您正在访问它。然而,您正在访问的字段是只读的,似乎没有默认值。您在其他地方设置了吗?我在下面包含了一个函数,它经常用于检查String.IsNullOrEmpty&amp;nbsp;,以节省一些逻辑。我建议将它放在一个安全的地方,并根据需要使用,因为RadGrid中的空单元格实际上永远不会是string.empty

    Public Function RadGridCellIsNullOrEmpty(value As String) As Boolean
        value = value.Trim()
        Return (String.IsNullOrEmpty(value) OrElse String.IsNullOrWhiteSpace(value) OrElse value.Contains(&quot;&amp;nbsp;&quot;))
    End Function

    Private Shared Function SetCommentsField(byRef e As GridCommandEventArgs) As GridCommandEventArgs

        If RadGridCellIsNullOrEmpty(e.Item.Cells(18).Text) Then
            Dim update As String = e.Item.Cells(17).Text
            If Not RadGridCellIsNullOrEmpty(update) Then
                e.Item.Cells(18).Text = String.Format(&quot;Modified by {0}&quot;, update)
            End If
        End If
        Return e
    End Function
英文:

Below are two options to access the cell data.

  1. This accesses the newly inserted data using a hashtable/dictionary. It only retrieves data from EditItems so read-only rows are not included
  2. This converts your editItem to a data item and accesses the current cell which you will find is consistently &amp;nbsp; because the data has not been put there yet. THe data entered only exists in the edit items.
    Protected Sub CarrierRadGrid_InsertCommand(sender As Object, e As GridCommandEventArgs)
        &#39;1
        Dim insertValues As New Hashtable()
        CType(e.Item, GridEditableItem).ExtractValues(insertValues)
        Dim dd = insertValues(&quot;CarrierScac&quot;)
        
        &#39;2
        Dim item As GridDataItem = CType(e.Item, GridDataItem)
        Dim itemD = item(&quot;CarrierScac&quot;).Text
        e = SetCommentsField(e)
    End Sub

The above should get you what you need I suspect, however to add some color

Telerik uses &amp;nbsp; to denote an empty/null cell text field which makes sense here because the cell text you are trying to grab does not exist. It IS empty and you ARE accessing it. The fields you are accessing, however, are read only and there does not appear to be a default value. Are you setting that elsewhere? I've included a function below that checks for String.IsNullOrEmpty and &amp;nbsp; that we use frequently to spare some logic. I'd put it in a safe place and consume it as needed because empty cells in a radgrid will never actually be string.empty.

    Public Function RadGridCellIsNullOrEmpty(value As String) As Boolean
        value = value.Trim()
        Return (String.IsNullOrEmpty(value) OrElse String.IsNullOrWhiteSpace(value) OrElse value.Contains(&quot;&amp;nbsp;&quot;))
    End Function

    Private Shared Function SetCommentsField(byRef e As GridCommandEventArgs) As GridCommandEventArgs

        If RadGridCellIsNullOrEmpty(e.Item.Cells(18).Text) Then
            Dim update As String = e.Item.Cells(17).Text
            If Not RadGridCellIsNullOrEmpty(update) Then
                e.Item.Cells(18).Text = String.Format(&quot;Modified by {0}&quot;, update)
            End If
        End If
        Return e
    End Function

huangapple
  • 本文由 发表于 2023年2月10日 05:02:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定