使用ASP.NET在加载时更改GridView中的值

时间:2009-04-16 06:52:37

标签: c# asp.net gridview

我有一个GridView,它可以很容易地填充来自SQL数据库的数据。 现在我想替换我的一列中的值,就像这样......

如果c04_oprogrs值为1,则在GridView中显示 Take

如果c04_oprogrs值为2,则在GridView中显示可用

我必须更改代码才能更改代码以显示新值。

我的网格

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                Height="281px" Width="940px" 
                Font-Size="X-Small" AllowPaging="True" 
                onpageindexchanging="GridView1_PageIndexChanging">
                <Columns>
                    <asp:BoundField DataField="c04_oprogrs" HeaderText="Order Progress" 
                        SortExpression="c04_oprogrs" />
                    <asp:BoundField DataField="c04_orderno" HeaderText="Order No." 
                        SortExpression="c04_orderno" />
                    <asp:BoundField DataField="c04_orddate" HeaderText="Date of Order" 
                        SortExpression="c04_orddate" DataFormatString="{0:d/MM/yyyy}" />
                    <asp:BoundField DataField="c04_ordval" HeaderText="Order Value" 
                        SortExpression="c04_ordval" DataFormatString="{0:R#,###,###.00}" />
                    <asp:BoundField DataField="c04_delval" HeaderText="Delivered Value" 
                        SortExpression="c04_delval" DataFormatString="{0:R#,###,###.00}" />
                    <asp:BoundField DataField="c04_invval" HeaderText="Invoice Value" 
                        SortExpression="c04_invval" DataFormatString="{0:R#,###,###.00}" />
                    <asp:BoundField DataField="c04_orddesc" HeaderText="Order Description" 
                        SortExpression="c04_orddesc" >
                        <ControlStyle Width="300px" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>

我的页面加载

SqlConnection myConnection;
        DataSet dataSet = new DataSet();
        SqlDataAdapter adapter;

        //making my connection
        myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMRASConnectionString"].ConnectionString);

        adapter = new SqlDataAdapter("Select TOP 40 c04_credno, c04_orderno, c04_orddate, c04_ordval, c04_delval, c04_invval, c04_oprogrs, c04_orddesc FROM C04ORDS WHERE c04_credno = '" + Session["CreditorNumber"] + "'AND c04_oprogrs <> 9 ORDER BY c04_orddate DESC", myConnection);

        adapter.Fill(dataSet, "MyData");

        GridView1.DataSource = dataSet;
        Session["DataSource"] = dataSet;
        GridView1.DataBind();

艾蒂安

修改

我的最终解决方案

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Find the value in the c04_oprogrs column. You'll have to use

            string value = e.Row.Cells[0].Text;

            if (value == "1")
            {
                e.Row.Cells[0].Text = "Take";
            }
            else if (value == "2")
            {
                e.Row.Cells[0].Text = "Available";
            }
        }

    }

5 个答案:

答案 0 :(得分:8)

您可以使用RowDataBound事件。使用此事件,您可以在呈现网格之前更改特定列的内容。

澄清一点。首先,在网格视图中添加带有标签的模板列(另请参阅ranomore的答案):

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

接下来,您实现RowDataBound事件(我没有检查下面的代码,因此它可能包含一些语法错误):

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        // Find the value in the c04_oprogrs column. You'll have to use
        // some trial and error here to find the right control. The line
        // below may provide the desired value but I'm not entirely sure.
        string value = e.Row.Cells[0].Text;

        // Next find the label in the template field.
        Label myLabel = (Label) e.Row.FindControl("myLabel");
        if (value == "1")
        {
            myLabel.Text = "Take";
        }
        else if (value == "2")
        {
            myLabel.Text = "Available";
        }
    }
}

答案 1 :(得分:2)

您可以使用模板列并在后面的代码中调用函数。

    <asp:TemplateField>
        <ItemTemplate>
                       <asp:Label runat="server" Text='<%#FieldDisplay(Eval("c04_oprogrs")) %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

然后在你的代码后面做

protected string FieldDisplay(int c04_oprogrs)
{
 string rtn = "DefaultValue";
 if (c04_oprogrs == 1)
 {
    rtn = "Take";
 }
 else if (c04_oprogrs == 2)
 {
    rtn = "Available";
 }
 return rtn;
}

答案 2 :(得分:2)

不使用功能。三元语句在VB中。如果你必须嵌套另一个三元语句以真正测试2,那么使用rwwilden的解决方案会更容易。

<asp:TemplateField HeaderText="Order Progress" SortExpression="c04_oprogrs" >
    <ItemTemplate>
        <asp:Label runat="server" ID="Label1" Text='<%# IIF(CInt(Eval("c04_oprogrs")) = 1, "Take", "Available") %>' />
    </ItemTemplate>
</asp:TemplateField>

答案 3 :(得分:1)

您可以在SQL语句中添加一个字段


adapter = new SqlDataAdapter("Select TOP 40 c04_credno, c04_orderno, c04_orddate,  
c04_ordval, c04_delval, c04_invval, c04_oprogrs, c04_orddesc ,  
CASE c04_oprogrs WHEN 1 THEN "Take" WHEN 2 THEN "Available" ELSE "DontKnow" END AS  
Status FROM C04ORDS WHERE c04_credno = '" + Session["CreditorNumber"] + "'AND  
c04_oprogrs  9 ORDER BY c04_orddate DESC", myConnection);

并在标记中将新字段作为BoundColumn的一部分 请原谅我的SQL语法,但我希望你明白这一点。

编辑:请勿使用语法 = '" + Session["CreditorNumber"] + "' 请参阅sql注入攻击以及如何使用参数化SQL来避免它。

答案 4 :(得分:0)

这是最有效的方式。 在sql server视图中创建一个案例,如: -

select case <columnname> 
       when 1 then 'available' 
       when 0 then 'not available' 
       end as <columnname> 
from <tablename>