检索 ItemDataBound 中的 RadGrid 单元格值 - “输入字符串的格式不正确。”

发布于 2024-12-29 17:00:07 字数 2696 浏览 1 评论 0原文

我试图根据 radGrid 中两个单元格值的比较来设置 CssClass。两个单元格均采用货币 {0:c} 格式,因此当我比较它们时,文本字符串中有一个 $ 符号。我知道如何解析字符串以删除 $ 符号,这就是我正在做的事情。但是,有没有办法在格式化之前获取单元格的原始文本,这样我就不会出现此错误?

这是我的代码:

ASPX:

                        <telerik:RadGrid ID="rgCISPartsInfo" DataSourceID="dsCISItem" AutoGenerateColumns="False"
                        GridLines="None" AllowPaging="False" OnItemDataBound="rgCISPartsInfo_ItemDataBound" runat="server" Width="676px">
                        <MasterTableView AllowPaging="False" DataKeyNames="SalesOrderItemId">
                            <Columns>
                                <telerik:GridBoundColumn HeaderText="COST" DataField="AverageCostPrice" AllowSorting="false"
                                    DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" ItemStyle-HorizontalAlign="right"
                                    HeaderStyle-Width="80px">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ExtendedCost" HeaderText="X COST" DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" 
                                HeaderStyle-Width="80px" ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Weight" HeaderText="WT" HeaderStyle-HorizontalAlign="right" HeaderStyle-Width="80px" 
                                ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>

C#:

protected void rgCISPartsInfo_ItemDataBound(object sender, GridItemEventArgs e)
{
    try
    {
        // only access item if not header or footer cell
        if ((e.Item.ItemType == GridItemType.Item) || (e.Item.ItemType == GridItemType.AlternatingItem))
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            GridColumn column = rgCISPartsInfo.MasterTableView.GetColumn("AverageCostPrice");

            decimal cost = Convert.ToDecimal(dataItem["AverageCostPrice"].Text);
            decimal price = Convert.ToDecimal(dataItem["Price"].Text);
            if (cost > price)
            {
                dataItem.CssClass = "Grid_Red_Row";
                throw new Exception("Item Cost is greater than price.");
            }
        }
    }
    catch (Exception ex)
    {
        GlobalHelper.ShowErrorMessage(this.Page.Master, ex.Message);
    }
}

更新: 所选的解决方案是我问题的答案,但我最终决定简单地解析字符串中的 $ 符号,因为这是最简单的解决方案。

I am trying to set a CssClass based on a the comparison of two cell values in my radGrid. Both cells are formatted for currency {0:c}, so that when I compare them, I have a $ sign in the text string. I know how to parse a string to remove the $ sign, which is what I am doing. However, is there a way to get the raw text of the cell prior to formatting, so that I will not have this error?

Here is my code:

ASPX:

                        <telerik:RadGrid ID="rgCISPartsInfo" DataSourceID="dsCISItem" AutoGenerateColumns="False"
                        GridLines="None" AllowPaging="False" OnItemDataBound="rgCISPartsInfo_ItemDataBound" runat="server" Width="676px">
                        <MasterTableView AllowPaging="False" DataKeyNames="SalesOrderItemId">
                            <Columns>
                                <telerik:GridBoundColumn HeaderText="COST" DataField="AverageCostPrice" AllowSorting="false"
                                    DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" ItemStyle-HorizontalAlign="right"
                                    HeaderStyle-Width="80px">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ExtendedCost" HeaderText="X COST" DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" 
                                HeaderStyle-Width="80px" ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Weight" HeaderText="WT" HeaderStyle-HorizontalAlign="right" HeaderStyle-Width="80px" 
                                ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>

C#:

protected void rgCISPartsInfo_ItemDataBound(object sender, GridItemEventArgs e)
{
    try
    {
        // only access item if not header or footer cell
        if ((e.Item.ItemType == GridItemType.Item) || (e.Item.ItemType == GridItemType.AlternatingItem))
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            GridColumn column = rgCISPartsInfo.MasterTableView.GetColumn("AverageCostPrice");

            decimal cost = Convert.ToDecimal(dataItem["AverageCostPrice"].Text);
            decimal price = Convert.ToDecimal(dataItem["Price"].Text);
            if (cost > price)
            {
                dataItem.CssClass = "Grid_Red_Row";
                throw new Exception("Item Cost is greater than price.");
            }
        }
    }
    catch (Exception ex)
    {
        GlobalHelper.ShowErrorMessage(this.Page.Master, ex.Message);
    }
}

UPDATE:
The selected solution is the answer to my question, but I ultimately decided to simply parse the string for the $ sign, as it was the simplest solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡紫姑娘! 2025-01-05 17:00:07

我将添加使用 visible="fasle" 归档的 RadGridDataBound 和不同的唯一名称,并从后面的代码访问它。

 <telerik:RadGrid ID="rgCISPartsInfo" DataSourceID="dsCISItem" AutoGenerateColumns="False"
                        GridLines="None" AllowPaging="False" OnItemDataBound="rgCISPartsInfo_ItemDataBound" runat="server" Width="676px">
                        <MasterTableView AllowPaging="False" DataKeyNames="SalesOrderItemId">
                            <Columns>
                                <telerik:GridBoundColumn HeaderText="COST" DataField="AverageCostPrice" AllowSorting="false"
                                    DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" ItemStyle-HorizontalAlign="right"
                                    HeaderStyle-Width="80px">
                                </telerik:GridBoundColumn>
                                       <telerik:GridBoundColumn HeaderText="COST" DataField="AverageCostPrice" Visiable="false" uniqueName="AverageCostPrice1"  DataField="AverageCostPrice" >
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ExtendedCost" HeaderText="X COST" DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" 
                                HeaderStyle-Width="80px" ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Weight" HeaderText="WT" HeaderStyle-HorizontalAlign="right" HeaderStyle-Width="80px" 
                                ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>

C#

  decimal cost = Convert.ToDecimal(dataItem["AverageCostPrice1"].Text);

PS:
我刚刚的另一个想法是在 ItemDataBound 事件期间分配 DataFormatString。这样你就会得到原始字符串。

I would add RadGridDataBound filed with visible="fasle" and different unique name and access it from the code behind.

 <telerik:RadGrid ID="rgCISPartsInfo" DataSourceID="dsCISItem" AutoGenerateColumns="False"
                        GridLines="None" AllowPaging="False" OnItemDataBound="rgCISPartsInfo_ItemDataBound" runat="server" Width="676px">
                        <MasterTableView AllowPaging="False" DataKeyNames="SalesOrderItemId">
                            <Columns>
                                <telerik:GridBoundColumn HeaderText="COST" DataField="AverageCostPrice" AllowSorting="false"
                                    DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" ItemStyle-HorizontalAlign="right"
                                    HeaderStyle-Width="80px">
                                </telerik:GridBoundColumn>
                                       <telerik:GridBoundColumn HeaderText="COST" DataField="AverageCostPrice" Visiable="false" uniqueName="AverageCostPrice1"  DataField="AverageCostPrice" >
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ExtendedCost" HeaderText="X COST" DataFormatString="{0:c}" HeaderStyle-HorizontalAlign="right" 
                                HeaderStyle-Width="80px" ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Weight" HeaderText="WT" HeaderStyle-HorizontalAlign="right" HeaderStyle-Width="80px" 
                                ItemStyle-HorizontalAlign="right"></telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>

C#

  decimal cost = Convert.ToDecimal(dataItem["AverageCostPrice1"].Text);

PS:
Another idea I just had is to assign the DataFormatString durring the ItemDataBound event. this way you'll have the raw string.

心安伴我暖 2025-01-05 17:00:07

一种选择是不在标记中使用格式字符串,而是在 ItemDataBound 事件中使用。

One option is to not use format string in the markup and do it in the ItemDataBound event instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文