2

我正在尝试在 Gridview 中以百分比格式显示值。现在我可以获得百分比值及其符号,但我需要的是范围。我通过具有 0% 到 100% 的下拉列表存储 % 值,例如,如果我从下拉列表中选择 20%,它应该以 20% 的形式存储在我的数据库中,并且应该在我的 GridView 中显示相同的值,但我得到了在数据库中为 2,在我的网格视图中为 200%。

我知道某处有一个小错误,但我无法弄清楚。我已将 Percnetage 的 DB 数据类型存储为 Decimal(8,0)。需要帮助的家伙。

这是我尝试过的

下拉列表 aspx 代码

<asp:DropDownList ID="DrpPercentageComplete" runat="server" BackColor="LightBlue"
 BorderColor="Black" BorderWidth="1px" Font-Bold="True" Font-Names="Verdana" 
 Font-Size="X-Small" DataTextFormatString="{0:0.# %}" ForColor="Black" Height="16px" 
                                                                onselectedindexchanged="DrpPercentageComplete_SelectedIndexChanged" 
 style="text-align: center" Width="135px">
  <asp:ListItem Value="Please Select"></asp:ListItem>
  <asp:ListItem Value="None"></asp:ListItem>
  <asp:ListItem Value="0%">0%</asp:ListItem>
  <asp:ListItem Value="10%">10%</asp:ListItem>
  <asp:ListItem Value="20%">20%</asp:ListItem>
  <asp:ListItem Value="30%">30%</asp:ListItem>
  <asp:ListItem Value="40%">40%</asp:ListItem>
  <asp:ListItem Value="50%">50%</asp:ListItem>
  <asp:ListItem Value="60%">60%</asp:ListItem>
  <asp:ListItem Value="70%">70%</asp:ListItem>
  <asp:ListItem Value="80%">80%</asp:ListItem>
  <asp:ListItem Value="90%">90%</asp:ListItem>
  <asp:ListItem Value="100%">100%</asp:ListItem>
  </asp:DropDownList>

百分比的 GridView TemplateField aspx 代码

 <asp:TemplateField HeaderText="% Complete">
 <ItemTemplate>
 <asp:Label ID="PercentageComplete" runat="server" Font-Names="Verdana" 
 Font-Size="X-Small" Height="30px" Text='<%# Eval("PercentageComplete","{0:0.#%}")%>'         Width="100px">
</asp:Label>
</ItemTemplate>
<FooterStyle BackColor="LightSlateGray"  />
<HeaderStyle BackColor="LightSlateGray" ForeColor="White" Width="109px" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>

aspx.cs 添加ddl值的代码

protected void BtnAdd_Click(object sender, EventArgs e)
    {
        MTMSDTO objc = new MTMSDTO();

            int Flag = 0;

            objc.TaskName = TxtTaskName.Text;
            objc.ClientName = DrpClientName.SelectedItem.Text;

            string date;
            date = Convert.ToDateTime(TxtBeginDate.Text).ToString("dd/MM/yyyy");

            DateTime dt = new DateTime();
            dt = Convert.ToDateTime(date);

            objc.BeginDate = dt;
            objc.DueDate = Convert.ToDateTime(TxtDueDate.Text);
            objc.Description = TxtDescription.Text;
            objc.AssignBy = LblAssignBy.Text;
            objc.AssignTo = DrpAssignTo.SelectedItem.Text;
            objc.Status = DrpStatus.SelectedItem.Text;
            objc.PercentageComplete = Convert.ToInt32(DrpPercentageComplete.Text);

            int X = obj.InsertTask(objc);
            {
                if (X >= 0)
                {
                    Flag = 1;
                }
                else
                {
                    Flag = 0;
                }
            }

            if (Flag == 1)
            {
                LblSuccess.Visible = true;
                LblSuccess.Text = "Data Added Successfully";
            }
            else
            {
                LblErr.Visible = true;
                LblErr.Text = "Failed To Add Data!!!";
            }
    }

 private Decimal _PercentageComplete;

    public Decimal PercentageComplete
    {
        get { return _PercentageComplete; }
        set { _PercentageComplete = value; }
    }

public int InsertTask(MTMSDTO M)
    {
        DBAccess db = new DBAccess();

        SqlParameter objParam = new SqlParameter("@TaskID", M.TaskID);
        objParam.Direction = ParameterDirection.Output;

        db.Parameters.Add(new SqlParameter("@Task_Name", M.TaskName));
        db.Parameters.Add(new SqlParameter("@Client_Name", M.ClientName));
        db.Parameters.Add(new SqlParameter("@Begin_Date", M.BeginDate));
        db.Parameters.Add(new SqlParameter("@Due_Date", M.DueDate));
        db.Parameters.Add(new SqlParameter("@Description_A", M.Description));
        db.Parameters.Add(new SqlParameter("@Assign_By", M.AssignBy));
        db.Parameters.Add(new SqlParameter("@Assign_To", M.AssignTo));
        db.Parameters.Add(new SqlParameter("@Status_S", M.Status));
        db.Parameters.Add(new SqlParameter("@Percentage_Complete", M.PercentageComplete));
        db.Parameters.Add(objParam);

        int retval = db.ExecuteNonQuery("InsertTask");

        if (retval >= 1)
        {
            return int.Parse(objParam.Value.ToString());
        }
        else
        {
            return -1;
        }
    }

要插入的存储过程

ALTER PROCEDURE [dbo].[InsertTask]


   @Task_Name nvarchar(50),
   @Client_Name nvarchar(50),
   @Begin_Date date,
   @Due_Date date,
   @Description_A nvarchar(50),
   @Assign_By nvarchar(50),
   @Assign_To nvarchar(50),
   @Status_S nvarchar(50),
   @Percentage_Complete decimal(18,0),

   @TaskID bigint OUTPUT

   As

   Insert into dbo.Task
   (
    TaskName, 
    ClientName, 
    BeginDate,
    DueDate,
    Description,
    AssignBy,
    AssignTo,
    Status,
    PercentageComplete
   )

    Values
   (
    @Task_Name, 
    @Client_Name, 
    @Begin_Date,
    @Due_Date,
    @Description_A,
    @Assign_By,
    @Assign_To,
    @Status_S,
    @Percentage_Complete
   )

   Select @TaskID = SCOPE_IDENTITY()
4

3 回答 3

3

这是在数据库中存储百分比值的最佳方式。

它是decimal(p, s)

http://msdn.microsoft.com/en-us/library/ms187746.aspx

于 2013-08-09T06:58:18.703 回答
2

您可以将数据库中的值保存为 Decimal/varchar 而无需任何符号 ie(%)。
并在 gridview 上使用在您的 DB 值之后string.Concat添加%符号

代码如下:

<asp:Label ID="PercentageComplete" runat="server" Font-Names="Verdana" 
 Text='<%# string.Concat(Eval("PercentageComplete"),"%") %>'>

编辑:

编辑您的 DropdownList 标记,如下面的代码所示,因为您正在尝试将其转换为int32您无法使用%的 . 您也可能有十进制值,因此将其转换为十进制

<asp:ListItem Value="0">0%</asp:ListItem>
<asp:ListItem Value="10">10%</asp:ListItem>
<asp:ListItem Value="20">20%</asp:ListItem>
    .............
    ............
<asp:ListItem Value="100">100</asp:ListItem>

代码背后:

objc.PercentageComplete = Convert.ToDecimal(DrpPercentageComplete.SelectedValue);
于 2013-08-09T07:13:24.933 回答
0

您应该在 db 中将没有任何符号的值保存为小数。当您需要 UI 中的格式时,请校准 ToString 并使用正确的百分比格式:

1 ("P", en-US) -> 100.00 %

1 ("P", fr-FR) -> 100,00 %

-0.39678 ("P1", en-US) -> -39.7 %

-0.39678 ("P1", fr-FR) -> -39,7 %

http://msdn.microsoft.com/es-es/library/dwhawy9k.aspx http://msdn.microsoft.com/es-es/library/dwhawy9k.aspx#PFormatString

于 2013-08-09T07:02:29.907 回答