Changing values ​​inside my gridview on loading using ASP.NET

I have a GridView that is populated with data from an SQL database, very simple. Now I want to replace the values ​​in my single column, like this ......

If the value of c04_oprogrs is 1, then display Take in a GridView.

If the value of c04_oprogrs is 2, then display Available in the GridView.

What code changes need to be made 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> 

Loading my page

 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 DECISION

 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
source share
5 answers

You can use a RowDataBound . Using this event, you can change the contents of certain columns before the grid is displayed.

To clarify a bit. First, you add a template column with a label in your mesh view (see also answer from runomore):

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

Then you implement the RowDataBound event (I did not check 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"; } } } 
+8
source

You can use the template column and call the function in your code.

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

then in your code behind

 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
source

Without using the function. The triple statement is in VB. If you need to attach another ternary statement to really check for 2, then it would be easier to go with rpmilden'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> 
+2
source

You can add a field to 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);
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 this new BoundColumn field part in the markup.
Forgive my SQL syntax, but I hope you get this idea.

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

+1
source

This is the best effective way. Make a case in the sql server view, for example: -

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

All Articles