如何为列gridview中的结果提供别名。 c#adio.net

时间:2017-07-18 08:25:31

标签: c# asp.net gridview

我有使用C#的SQL数据库。我正在填充GridView

我想根据条件为列提供别名。例如,如果我的列中的SQL结果显示为"order not placed",我想将其更改为"Awaiting confirmation",如果它显示为"Emailed",我想将其别名为"order confirmed for collection"

如何在C#代码中执行此操作?

using (SqlDataReader rdr = cmd.ExecuteReader())
{
    DataTable table = new DataTable();
    table.Columns.Add("Current Status");
    table.Columns.Add("ID");
    while (rdr.Read())
    {
        //linking column with data
        DataRow dataRow = table.NewRow();
        dataRow["Current Status"] = rdr["Current Status"];
        dataRow["ID"] = rdr["ID_MSG_SLIP"];
        table.Rows.Add(dataRow);
    }

    GridView1.DataSource = table;
    GridView1.DataBind();

2 个答案:

答案 0 :(得分:0)

在循环搜索结果时,您可以轻松评估当前状态,然后根据您的逻辑进行更改。

我认为“当前状态”是您所指的列,因此这应该有效:

while (rdr.Read())
{
    string currentStatus = (string)rdr["Current Status"];
    if(currentStatus == "Emailed")
        currentStatus = "order confirmed for collection";
    else if(currentStatus == "order not placed")
        currentStatus = "Awaiting confirmation";

    DataRow dataRow = table.NewRow();
    dataRow["Current Status"] = currentStatus;
    dataRow["ID"] = rdr["ID_MSG_SLIP"];
    table.Rows.Add(dataRow);
}

注意:您似乎与字符串中的外壳不一致,您可能需要查看它以使其正常工作。

答案 1 :(得分:0)

希望这会奏效:

在GridView中添加此<asp:TemplateField HeaderText="Durum"> <ItemTemplate> // Bind your column from database in Eval <asp:Label ID="lblSqlValue" Visible="false" runat="server" Text='<% #Eval("sqlColumnName") %>'></asp:Label> // this will show the Alias on the database column value <asp:Label ID="lblAlias" runat="server" Text=""></asp:Label> </ItemTemplate> 代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string lblSqlValue = ((Label)e.Row.FindControl("lblSqlValue")).Text;
        Label lblAlias = e.Row.FindControl("lblAlias") as Label;

        if (lblSqlValue=="order not placed")
            lblAlias.Text = "Awaiting confirmation";
        else if(lblSqlValue=="Emailed")
            lblAlias.Text = "order confirmed for collection";
        else
            lblAlias.Text = "No Value";
    }
}

代码背后:

OnRowDataBound

注意:不要忘记在GrindView中添加<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >,例如<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="#e6e6e6" android:orientation="vertical" android:padding="1dp"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#868585" android:padding="0dp" android:text="Basic Information" android:textColor="#ffffff" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="9" app:srcCompat="@drawable/navigation" /> <TextView android:id="@+id/textView7" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Address" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> </LinearLayout> </LinearLayout> </ScrollView>