【问题标题】:Manipulate SQL values before displaying on GridView在 GridView 上显示之前操作 SQL 值
【发布时间】:2014-04-04 07:14:13
【问题描述】:

N,V,C,D 是我的 GridView 绑定列中的变量。

当我在 GridView 中显示此表时,我希望改为显示 New,Verified,Cancelled,Deleted

我的 GridView 调用我的数据库中运行选择查询的过程。我是否需要更改查询或添加 GridView 功能?我不想更改我的数据库值本身。

我该怎么做?

这是我现在的绑定字段:

<asp:BoundField 
    DataField="Status"
    HeaderText="Status"
    SortExpression="Status" />

【问题讨论】:

  • @huMptyduMpty 我不想更改我的列标题。我想替换它检索到的数据。

标签: c# asp.net sql gridview


【解决方案1】:

这在 C# 中应该是可能的 - 至少如果您只想显示值而不是编辑它们。在 CellFormatting 事件中,您可以简单地更改要显示的值。

private void gridview_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  if (e.Value.equals("N")) e.Value = "New";
  else if(e.Value.equals("V")) e.Value = "Verified";
  else if(e.Value.equals("C")) e.Value = "Cancelled";
  else if(e.Value.equals("D")) e.Value = "Deleted";
}

我现在手头没有 C#,所以可能有错别字。试试看吧。

【讨论】:

  • 我在哪里调用这个事件?
  • 你不调用事件。事件发生。您编写一个方法来对与控件关联的事件作出反应。您的 gridview 没有 CellFormatting 事件吗?你用什么gridview?一个 DataGridView?
  • 我只使用gridview。&lt;asp:GridView ID="Gv_IndentsForIp" runat="server"
  • 啊,好吧。看来,对于 ASP GridView,您将使用 RowDataBound 事件。我没有这方面的经验。我希望 Sajeetharan 的回答对您有所帮助。
  • 我使用了绑定字段,而那些似乎没有 ID 标签。这就是为什么我对此有一些问题。
【解决方案2】:

在您的查询中,

Select 
      YourFields
      case 
      when YourConditionField= 'N' then 'New' 
      when YourConditionField= 'V' then 'Verified'
      when YourConditionField= 'C' then 'Cancelled'
      when YourConditionField= 'D' then 'Deleted'
      end,
from table

GridView 中的第一个,

您可以使用 RowDataBound 事件,您需要将带有标签的模板列添加到您的网格视图中

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="labelResult" runat="server" />
    </ItemTemplate>
</asp:TemplateField>


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
       string value = e.Row.Cells[0].Text;
       Next find the label in the template field.
        Label myLabel = (Label) e.Row.FindControl("myLabel");
        if (value == "N")
        {
            myLabel.Text = "New";
        }
        else if (value == "V")
        {
            myLabel.Text = "Verified";
        }
        else if (value == "C")
        {
            myLabel.Text = "Cancelled";
        }
        else if (value == "D")
        {
            myLabel.Text = "Deleted";
        }
    }
}

【讨论】:

  • 我使用绑定字段,而不是模板字段。我该如何进行?
【解决方案3】:
   <asp:BoundField HeaderText="NEW" DataField="N" ></asp:BoundField>

【讨论】:

  • 是的,您可以根据需要添加任意数量的绑定字段,并将标题文本保留为您喜欢的列名。和 DataField 作为来自 DB 输出的列名。
【解决方案4】:

首先,我将GridViewControl的status列从默认的BoundField修改为ItemTemplate

(我删除了自动生成的 EditItemTemplate 标签)

 <asp:Label ID="lblStatus"  
                 runat="server" 
                 Text='<%# GetLabelText(Eval("status")) %>'>
 </asp:Label>

然后在我的 CS 文件中,我添加了以下代码:

public string GetLabelText(object dataItem)
    {
    string text = "";
    string val = dataItem as string;
    switch (val)
        {
        case "N":   text = "New"; 
             break;
        case "V":   text = "Verified";
            break;
        case "F":   text = "Fulfilled";
            break;
        case "C":   text = "Cancelled";
            break;
        }
    return text;
    }

这就像一个魅力。谢谢你们的帮助,伙计们!

【讨论】:

    猜你喜欢
    • 2011-02-14
    • 1970-01-01
    • 2021-10-21
    • 2012-05-25
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多