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

Eti*_*nne 4 c# asp.net gridview

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

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

如果c04_oprogrs值为2,则在GridView中显示Available.

我必须更改哪些代码才能更改为我的代码以显示新值.

我的网格

<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>
Run Code Online (Sandbox Code Playgroud)

我的页面加载

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();
Run Code Online (Sandbox Code Playgroud)

艾蒂安

编辑:

我的最终解决方案

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";
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)

Ron*_*erg 8

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

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

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="myLabel" runat="server" />
    </ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)

接下来,您实现了该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";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)