Sunday, August 11, 2019

How to apply formatting on Gridview based on condition Like Excel conditional formatting options

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for store/fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok. 
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >


Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish. 

Step-5: Add a Webpage and Design for show data with conditional formatting.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code 
  1. <h3>Sales Report Month Wise</h3>
  2. <p>&nbsp;</p>
  3. <div>
  4. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" GridLines="Both" OnRowDataBound="GridView1_RowDataBound">
  5. <Columns>
  6. <asp:BoundField HeaderText="Month" DataField="MonthName" />
  7. <asp:BoundField HeaderText="Sales Total Amount" DataField="SalesAmount" />
  8. </Columns>
  9. </asp:GridView>
  10. </div>

Step-6: Write code into page load event for show data.

Write below code into Page_Load event for show data from database. 

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateSalesData();
  6. }
  7. }

   And here is the functioin PopulateSalesData

  1. private void PopulateSalesData()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. GridView1.DataSource = dc.SalesReports.ToList();
  6. GridView1.DataBind();
  7. }
  8. }

Step-7: Write code for apply conditional formatting on Gridview

Write below code into event for import Data from csv to database. 

  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3. // Write Code here for apply Formatting conditionally
  4.  
  5. if (e.Row.RowType == DataControlRowType.DataRow)
  6. {
  7. decimal value = (decimal)DataBinder.Eval(e.Row.DataItem, "SalesAmount");
  8. if (value < 60000)
  9. {
  10. // for apply to entire row
  11. foreach (TableCell cell in e.Row.Cells)
  12. {
  13. cell.BackColor = Color.FromName("#f480a1");
  14. }
  15. // for only one cell
  16. //e.Row.Cells[1].BackColor = Color.FromName("#f480a1");
  17. }
  18. else if ((value >= 60000) && (value < 90000))
  19. {
  20. // for apply to entire row
  21. foreach (TableCell cell in e.Row.Cells)
  22. {
  23. cell.BackColor = Color.FromName("#ffe89f");
  24. }
  25. // for only one cell
  26. //e.Row.Cells[1].BackColor = Color.FromName("#ffe89f");
  27. }
  28. else if(value >=90000)
  29. {
  30. // for apply to entire row
  31. foreach (TableCell cell in e.Row.Cells)
  32. {
  33. cell.BackColor = Color.FromName("#00b050");
  34. }
  35. // for only one cell
  36. //e.Row.Cells[1].BackColor = Color.FromName("#00b050");
  37. }
  38. }
  39. }

Step-8: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...