Link to home
Create AccountLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

format value in gridview

I am displaying data using a gridview.  I'm using the following stored procedure to return the data:

CREATE PROCEDURE [dbo].[spGetCustomerServiceMetrics]

AS
   SELECT metric_id,  description, metric =
      CASE metric_id
      WHEN 1 THEN CAST(metric AS VARCHAR(2)) + '%'
      WHEN 2 THEN CAST(metric AS VARCHAR(10)) + ' seconds'
      ELSE CAST(metric AS VARCHAR(10))
      END
FROM dbo.customer_service_metric

The datatype of field "metric" is real.  I append a '%' and 'seconds' when the data displays on the page.

When the gridview is in edit mode, the "metric" field still displays the '%' and 'seconds' in the columns.

How can I remove the '%' and 'seconds' from the values displayed when the gridview is in edit mode?  When I update the values, the database requires that the values be of datatype real.  I want the symbols to display normally when the gridview isn't in edit mode.  But, I want to remove the symbols when the gridview is in edit mode.

Here is the gridview code in my aspx page:


<asp:GridView 
            ID="GridView1"
            AutoGenerateColumns="False" 
            runat="server" 
            OnRowEditing="GridView1_RowEditing" 
            OnRowCancelingEdit="GridView1_RowCancelingEdit" 
            Width="850px" 
            OnRowUpdating="GridView1_RowUpdating" 
            OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <asp:Label ID="lblDescription" Text='<%# Eval("description") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDescription" Text='<%# Eval("description") %>' runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ControlStyle Width="300px" />
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle Width="300px" VerticalAlign="Top" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Metric">
                    <ItemTemplate>
                        <asp:Label ID="lblMetric" Text='<%# Eval("metric") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtMetric" Text='<%# Eval("metric") %>' runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ControlStyle Width="300px" />
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle Width="300px" VerticalAlign="Top" />
                </asp:TemplateField>
                <asp:CommandField 
                    ButtonType="Button" 
                    ShowEditButton="True" >
                    <ItemStyle VerticalAlign="Top" />
                </asp:CommandField>
            </Columns>
        </asp:GridView>

Open in new window

Avatar of renjurdevan
renjurdevan
Flag of India image

Do append "%" value in ASP.Net like

  <ItemTemplate>
                        <asp:Label ID="lblMetric" Text='<%# Eval("metric") %> + "%"' runat="server"></asp:Label>
                    </ItemTemplate>

and avoid it from StoredProcedure like
SELECT metric_id,  description, metric =
      CASE metric_id
      WHEN 1 THEN CAST(metric AS VARCHAR(2))
      WHEN 2 THEN CAST(metric AS VARCHAR(10))
      ELSE CAST(metric AS VARCHAR(10))
      END


May sort out the problem

Regards
Renju
<ItemTemplate>
                        <asp:Label ID="lblMetric" Text='<%# Eval("metric") %>  "%"' runat="server"></asp:Label>
                    </ItemTemplate>

No need or "+"


Avatar of -Dman100-

ASKER

I cannot add the '%' within the page gridview code because I only want the '%' to display for the first record.  And 'seconds' to the next record.  All the other records display the data without having '%' or 'seconds' appended to the value.  This is why I have the CASE statement in the stored procedure.

If it been so Handle RowDataBound of grid view and get values over there and append "%" for particular row!!

Regards
Renju
Hi Renju,

So, there is no way to remove the appended suffix from the value without modifying the stored procedure?

Thanks for your help.
Link will help you

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

**************

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string metric= DataBinder.Eval(e.Row.DataItem, "metric") as
string;
if (metric!= null && metric.Length > 0)
{
// Do additional checking like is first row ... if so

 e.Row.Cells[1].Text = metric + "%";


}
else
{
e.Row.Cells[1].Text = metric ;
}
}
}
Other wise RowEditing button should be handled like

void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
  {

    String metricRow = CustomersGridView.Rows[e.NewEditIndex].Cells[1].Text;

   
    if (metricRow == "1")
    {
          //    eleminate %
}
    else
    {
      //
    }
   
  }
Hi renjurdevan,

I am attempting to use the RowEditing event to remove the '%' .  I'm not getting any errors, but the '%' symbol is not removed.  Can you let me know where I've made my mistake?

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView();

        String s = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
        s.Replace("%", "");
    }
ASKER CERTIFIED SOLUTION
Avatar of renjurdevan
renjurdevan
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hmmm, well, that caused the field to go blank.  It removed the value instead of just removing the '%' symbol.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView();

        string s = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
        GridView1.Rows[e.NewEditIndex].Cells[1].Text = s.Replace("%", "");
    }

I truly appreciate the help.  Thank you.
Regards.