views:

2956

answers:

4

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();

Thanks in advanced!!

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

    }
+4  A: 

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";
        }
    }
}
Ronald Wildenberg
Can you maybe give me an example please?
Etienne
I'll give it a try..
Ronald Wildenberg
Thanks for all the help, i made some changes and it worked!! COOL!!
Etienne
A: 

You could add a field in the SQL Statement


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);

And make that new field part of the BoundColumn in the markup.
Pardon my SQL syntax but I hope you get the idea.

EDIT: Do not use the syntax = '" + Session["CreditorNumber"] + "'.
See sql injection attack and how to avoid it using parameterized SQL.

shahkalpesh
Thanks for the help, it did work after i fixed some syntax. But i went with another option above.
Etienne
+1  A: 

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;
}
ranomore
+1  A: 

Without using a function. The ternary statement is in VB. If you have to nest another ternary statement to really test for 2 then it'd be easier to go with rwwilden's solution.

<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>
rvarcher