Monday, 8 April 2013

My Learnings with DataGridView in WinForms - Part 1


Introduction 


This is the first of the three posts on this topic. The Paint event is called when DataGridView needs repainting on account of changes to the grid. One of the requirements of a project was to display a No records available message when there are no records.  Rather than having checking record count each time records are fetched from database, its better to check condition only when grid is drawn. In this post I will explain how to use the Paint event to do this.

The following example illustrates how to use this event to customize the grid.

1.       Open Visual Studio and create a new Windows Forms project.

 

2.       Rename Form1 to Dashboard.



3.       Open Dashboard form in Designer mode.

4.       From Toolbox drag the following to the form:

a.       DataGridView control

b.      labelForEmptyRows .



 

5.       Click the arrow on DataGridView to define column header and column types. 



6.       In the Add columns window, set the column name, type(button column, text column etc.) and header text

 

 
7.       The final layout would be as seen in the following screenshot

 

8.       To populate the grid with some data, add the following code to the form.

public Dashboard()
        {
            InitializeComponent();  
            IList<CourseEntity> CourseList = PopulateGrid();
                   foreach(CourseEntity courseEntity in CourseList)
                dgvDashboard.Rows.Add(courseEntity.Id, courseEntity.CourseName,courseEntity.Status);     
        } 

        public IList<CourseEntity> PopulateGrid()
        {
            List<CourseEntity> courseList = new List<CourseEntity>();
            courseList.Add(new CourseEntity { Id = "1", CourseName = "Asp.NET MVC3", Status = "Available" });
           courseList.Add(new CourseEntity { Id = "2", CourseName = "Asp.NET MVC4", Status = "Available" });
    courseList.Add(new CourseEntity { Id = "3", CourseName = "C#", Status = "Available" });
    courseList.Add(new CourseEntity { Id = "4", CourseName = "Metro Apps", Status = "Available" });
            //return courseList.Cast<CourseEntity>().ToList();
            return courseList;
        }

       
               public class CourseEntity
 {
        public string Id { get; set; }
        public string CourseName { get; set; }
        public string Status { get; set; }
 }

 
9.       The expected output will be like this.



 
     10.       Oops!! No text on buttons? And why one extra row??
      11.       For providing text on buttons in Borrow Column. Go to edit columns in datagridview.
 
Set the properties as shown. 

12.       For removing this extra row at bottom, set “AllowUserToAddRows” property of datagridview to false.

13.       Now, comment out the code in Dashboard() so that no records are populated to grid.
public Dashboard()
        {
            InitializeComponent();    
            //IList<CourseEntity> CourseList = PopulateGrid();
            //foreach(CourseEntity courseEntity in CourseList)
            //    dgvDashboard.Rows.Add(courseEntity.Id, courseEntity.CourseName, courseEntity.Status);  
  }
14.       In the paint event of DataGridView add the following code.
 private void dgvDashboard_Paint(object sender, PaintEventArgs e)   {
          if (dgvDashboard.Rows.Count == 0)
                lblEmptyRows.Visible = true;
       else
            lblEmptyRows.Visible = false;
}
15.       See the output now.

Conclusion

 We learnt how we can leverage the Paint event of DataGridView, how to design a DataGridView view with different column types and AllowUserToAddRows Property.
In next part I will cover how to make images as buttons in DataGridView and define click events for them. Stay tuned J
 

Thursday, 18 October 2012

Setting up Build environment in TFS 2010

Managing source control in a relatively large team is difficult, and consequence is, build errors might occur. So when other team members take the latest version they might end up with build errors etc.
One can think of setting up a build system, if he is looking for one or more of following requirements
-          People are not allowed to check-in if solution is not compiling
-          If code analysis issues are not fixed
-          Emails can be triggered for people whose check-in broke the build
-          Projects alert on check-in/build success/build failure to the team
-          Scheduling build to be generated at fixed intervals.
Assumption
-          Person configuring the build system must have Admin previleges on the TFS Project.
Configuring the build
1.       Open visual studio as Administrator.
2.       Connect to Team explorer and select the project for which you want to setup build system.




3.       Right Click on Builds -> New Build Definition.
4.       New Build Definition Wizard appears as shown below.



5.       Enter the Build Definition Name.
 
6.       Select Trigger option from left side pane. Select the type of build option suitable for your project. For example if you select Continuous integration each check-in will be build whereas if we setup gated check-in only if changes are building successfully.




7.       Now what projects to need to be build? Select process tab from left pane and configure Projects To Build



You can click on ellipsis button to browse and add projects to build.

8.       Now where this build information will get stored? For this we should setup a build drop folder on TFS server. Please contact your TFS administrator to create a drop folder in TFS.
You can specify the drop folder location as shown in snapshot below. 




 
9.     Now the TFS is configured. But how team members will know that a build has succeeded or not? For this configure email-ids of team members in Project Alerts option.

Team Menu-> Project Alerts





So the build system is configured now. This avoids the overhead of checking history whose check-in caused the build to fail or if code analysis is done or not. Running test cases again and again with each build. Setting up build definition also provides options to run all dlls with *tests while generating a build. So if any test case fails build will be failed and team members will be notified.


Sounds interesting?? So start using it right away J