I have a GridView that gets populated with data from a SQL database, very easy. Now i want to replace values in my one column like so......
If c04_oprogrs value is a 1 then display Take in the GridView.
If c04_oprogrs value is 2 then display Available in the GridView.
What code changes must i make to change to my code to display the new values.
My Grid
<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>
My Page load
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();
Etienne
EDIT:
MY FINAL SOLUTION
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";
            }
        }
    }
                You can use the RowDataBound event for this. Using this event you can alter the content of specific columns before the grid is rendered.
To clarify a little. First you add a template column with a label to your grid view (see also the answer by ranomore):
<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="myLabel" runat="server" />
    </ItemTemplate>
</asp:TemplateField>
Next you implement the RowDataBound event (I haven't checked the code below, so it may contain some syntax errors):
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";
        }
    }
}
                        You could use a template column and call a function in your code behind.
    <asp:TemplateField>
        <ItemTemplate>
                       <asp:Label runat="server" Text='<%#FieldDisplay(Eval("c04_oprogrs")) %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
then in your code behind do
protected string FieldDisplay(int c04_oprogrs)
{
 string rtn = "DefaultValue";
 if (c04_oprogrs == 1)
 {
    rtn = "Take";
 }
 else if (c04_oprogrs == 2)
 {
    rtn = "Available";
 }
 return rtn;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With