Popular Posts

Dec 12, 2010

Using List View (C#)

How to show data in list view using Layer Architecture and OOP Approach.To day i am making this for my blog with easy steps.

Step1: Open a new project.



Step2: Make a project i named it ShowData.



Step3: Delete From1.cs and Program.cs files

Step4: Add a new Class named it EntryPoint.cs

Step5: Add a new folder named it UI and add new windows form named it FirstUI.cs from add new item

Step6: Add more folder and named it as DAL, BLL, Database

Step7:Now go to your EntryPoint.cs and add this code

public static void Main(string[] args)
       {
           Application.Run(new FirstUI());
       }

Step8: Add one more folder to DAL (Data Access Layer) And named it DAO(Data Access Object)

Step9: Add a class and named it as Student.cs


 

Step10: Type the variable and encapsulate them. 




After encapsulating all fields your code will look like this:

public class Student
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string rollNo;

        public string RollNo
        {
            get { return rollNo; }
            set { rollNo = value; }
        }
        private string gpa;

        public string Gpa
        {
            get { return gpa; }
            set { gpa = value; }
        }
        private string dept;

        public string Dept
        {
            get { return dept; }
            set { dept = value; }
        }
        private string reg;

        public string Reg
        {
            get { return reg; }
            set { reg = value; }
        }

    }



Step11:Add new class to Database folder and named it DBConnection.cs
Step12:Now i connect to the SQLServer.Your code might be like this:
    public class DBConnection
    {
       private SqlConnection sqlConn = null;
        private string connectionString = null;
        public DBConnection()
        {
            try
            {
                connectionString = @"server=localhost\SQLEXPRESS; Integrated Security=SSPI; database=ViperStudentDB";
                sqlConn = new SqlConnection(connectionString);

            }
            catch (IOException ioObj)
            {
                throw ioObj;
            }
        }

        public SqlConnection GetConnection
        {
            get
            {
                return sqlConn;
            }

        }
    }

Step13:Now i add another new class called MethodStudent.cs to Database folder;




Step14: Now we add a method that will select all the student of the database.

public class MethodStudent
    {
        DBConnection dbConnectionObj = null;
        public List<Student> GetStudentInfo()
        {
            List<Student> studentListObj = new List<Student>();
            string sqlString = "SELECT * FROM t_studentInfo";
            SqlConnection sqlConn = dbConnectionObj.GetConnection;
            sqlConn.Open();
            SqlCommand comObj = new SqlCommand(sqlString,sqlConn);
            SqlDataReader readerObj = comObj.ExecuteReader();
            while (readerObj.Read())
            {
                Student studentObj = new Student();
                studentObj.Name = readerObj[0].ToString();
                studentObj.RollNo = readerObj[1].ToString();
                studentObj.Gpa = readerObj[2].ToString();
                studentObj.Dept = readerObj[3].ToString();
                studentObj.Reg = readerObj[4].ToString();
                studentListObj.Add(studentObj);
            }
            sqlConn.Close();
            return studentListObj;
        }
    }

Step15:Add one more class to the BLL(Business Logic Layer) folder.To add new class right click and select new item.
Type the code

   public class StudentBLL
    {
       public List<Student> GetStudentInfo()
       {
           MethodStudent metodObj = new MethodStudent();
           return metodObj.GetStudentInfo();
       }
    }

Step16:Add a listView and a button to the windows Form.and named them as studentListView and the button will be studentButton.

Step17:Select your listView and go to the propertyes and made some changes.You can add column header following the steps.

Click on the corner of the listview,click on edit columns, click on add button.



Step18:Now double click on the button and type the code so that you can see the data to the listView.


  StudentBLL studentBllObj = null;
        public FirstUI()
        {
            studentBllObj = new StudentBLL();
            InitializeComponent();
        }

        private void studentButton_Click(object sender, EventArgs e)
        {
            foreach (Student studentObj in studentBllObj.GetStudentInfo())
            {
                ListViewItem listViewObj = new ListViewItem(studentObj.Name);
                listViewObj.SubItems.Add(studentObj.RollNo);
                listViewObj.SubItems.Add(studentObj.Gpa);
                listViewObj.SubItems.Add(studentObj.Dept);
                listViewObj.SubItems.Add(studentObj.Reg);
                studnetListView.Items.Add(listViewObj);
            }

        }


Step20:Now run the project and click on the button you can see the result:

No comments:

Post a Comment