Friday, 31 May 2013

How to make Students Records with DataGridview

How to make Students Records with DataGridview 

1)      Develop an application that allows user to add or remove student information to/from Master list by fulfilling following requirements:
a.      Provide following four input fields for student information (Detail portion):
Student Name, Registration Id, Phone Number, Gender (use ComboBox).
b.      A button to add student record to Master portion (in point c below).
c.       A DataGridView control to keep the list of records added (Master).
d.      Use the DataGridView in read-only mode.
e.      Add four columns to the DataGridView for the data to be added as a complete record.
f.        Use a NumericUpDown control to select the row number desired to be deleted.
g.      Use a button to delete the desired row.


2)      Use a DataGridView similar to the one in task 1 (with four columns) to update records by fulfilling following requirements:
a.      Use a class to keep student information that uses the same 4 fields in task 1 plus two additional fields (Major and Address).
b.      Use the DataGridView in read-only mode but populate it programmatically with 10 records using the objects instantiated using student information class mentioned in step (a).
c.       Upon selecting a row in DataGridView, populate all six fields in Detail portion.
d.      Use a button to update the changes done in Detail portion to the selected record in the list.
e.      Test your application.
(submit both tasks to the lab instructor for this lab)

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lab4_2
{
  
    public partial class Form1 : Form
    {
        obj[] obj1 = new obj[10];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
            obj1[0] = new obj("mohsin", "120", "123345", "male","computer","garden");
            obj1[1]= new  obj("ahmer","150","7715243","male","accounting","dalmia");
            obj1[2] = new obj("farhan", "7520", "4564566", "male", "dld", "jauhar");
            obj1[3] = new obj("khan", "620", "156467", "male", "oop", "jauhar");
            obj1[4] = new obj("zuleikha", "520420", "56146", "female", "sciecne", "karsaz");
            obj1[5] = new obj("bukhtawar", "578020", "56046", "female", "eng", "nazimabad");
            obj1[6] = new obj("zahra", "920", "564057756", "female", "eng", "gulshan");
            obj1[7] = new obj("selena", "320", "3456046", "female", "psychology", "malir");
            obj1[8] = new obj("kate", "420", "756440576", "female", "economics", "awami-markaz");
            obj1[9] = new obj("jojo", "1210", "564600", "male", "computer", "dalmia");
            gridload();
         
        }
        private void gridload()
        {
            dataGridView1.Rows.Add(obj1[0].name, obj1[0].reg, obj1[0].phone, obj1[0].gender);
            dataGridView1.Rows.Add(obj1[1].name, obj1[1].reg, obj1[1].phone, obj1[1].gender);
            dataGridView1.Rows.Add(obj1[2].name, obj1[2].reg, obj1[2].phone, obj1[2].gender);
            dataGridView1.Rows.Add(obj1[3].name, obj1[3].reg, obj1[3].phone, obj1[3].gender);
            dataGridView1.Rows.Add(obj1[4].name, obj1[4].reg, obj1[4].phone, obj1[4].gender);
            dataGridView1.Rows.Add(obj1[5].name, obj1[5].reg, obj1[5].phone, obj1[5].gender);
            dataGridView1.Rows.Add(obj1[6].name, obj1[6].reg, obj1[6].phone, obj1[6].gender);
            dataGridView1.Rows.Add(obj1[7].name, obj1[7].reg, obj1[7].phone, obj1[7].gender);
            dataGridView1.Rows.Add(obj1[8].name, obj1[8].reg, obj1[8].phone, obj1[8].gender);
            dataGridView1.Rows.Add(obj1[9].name, obj1[9].reg, obj1[9].phone, obj1[9].gender);
            fill();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            fill();
        }
        public void fill()
        {

            if (dataGridView1.CurrentRow.Index == 1)
            {
                textBox1.Text = obj1[1].name;
                textBox2.Text = obj1[1].reg;
                textBox3.Text = obj1[1].phone;
                textBox4.Text = obj1[1].gender;
                textBox5.Text = obj1[1].major;
                textBox6.Text = obj1[1].add;
            }
            else if (dataGridView1.CurrentRow.Index == 2)
            {
                textBox1.Text = obj1[2].name;
                textBox2.Text = obj1[2].reg;
                textBox3.Text = obj1[2].phone;
                textBox4.Text = obj1[2].gender;
                textBox5.Text = obj1[2].major;
                textBox6.Text = obj1[2].add;
            }
            else if (dataGridView1.CurrentRow.Index == 3)
            {
                textBox1.Text = obj1[3].name;
                textBox2.Text = obj1[3].reg;
                textBox3.Text = obj1[3].phone;
                textBox4.Text = obj1[3].gender;
                textBox5.Text = obj1[3].major;
                textBox6.Text = obj1[3].add;
            }
            else if (dataGridView1.CurrentRow.Index == 4)
            {
                textBox1.Text = obj1[4].name;
                textBox2.Text = obj1[4].reg;
                textBox3.Text = obj1[4].phone;
                textBox4.Text = obj1[4].gender;
                textBox5.Text = obj1[4].major;
                textBox6.Text = obj1[4].add;
            }
            else if (dataGridView1.CurrentRow.Index == 0)
            {
                textBox1.Text = obj1[0].name;
                textBox2.Text = obj1[0].reg;
                textBox3.Text = obj1[0].phone;
                textBox4.Text = obj1[0].gender;
                textBox5.Text = obj1[0].major;
                textBox6.Text = obj1[0].add;

            }
            else if (dataGridView1.CurrentRow.Index == 5)
            {
                textBox1.Text = obj1[5].name;
                textBox2.Text = obj1[5].reg;
                textBox3.Text = obj1[5].phone;
                textBox4.Text = obj1[5].gender;
                textBox5.Text = obj1[5].major;
                textBox6.Text = obj1[5].add;
            }
            else if (dataGridView1.CurrentRow.Index == 6)
            {
                textBox1.Text = obj1[6].name;
                textBox2.Text = obj1[6].reg;
                textBox3.Text = obj1[6].phone;
                textBox4.Text = obj1[6].gender;
                textBox5.Text = obj1[6].major;
                textBox6.Text = obj1[6].add;
            }
            else if (dataGridView1.CurrentRow.Index == 7)
            {
                textBox1.Text = obj1[7].name;
                textBox2.Text = obj1[7].reg;
                textBox3.Text = obj1[7].phone;
                textBox4.Text = obj1[7].gender;
                textBox5.Text = obj1[7].major;
                textBox6.Text = obj1[7].add;
            }
            else if (dataGridView1.CurrentRow.Index == 8)
            {
                textBox1.Text = obj1[8].name;
                textBox2.Text = obj1[8].reg;
                textBox3.Text = obj1[8].phone;
                textBox4.Text = obj1[8].gender;
                textBox5.Text = obj1[8].major;
                textBox6.Text = obj1[8].add;
            }
            else if (dataGridView1.CurrentRow.Index == 9)
            {
                textBox1.Text = obj1[9].name;
                textBox2.Text = obj1[9].reg;
                textBox3.Text = obj1[9].phone;
                textBox4.Text = obj1[9].gender;
                textBox5.Text = obj1[9].major;
                textBox6.Text = obj1[9].add;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow.Index == 1)
            {
                obj1[1].name= textBox1.Text;
                obj1[1].reg = textBox2.Text;
                obj1[1].phone=textBox3.Text;
                obj1[1].gender=textBox4.Text;
                obj1[1].major= textBox5.Text;
                obj1[1].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 1)
            {
                obj1[1].name = textBox1.Text;
                obj1[1].reg = textBox2.Text;
                obj1[1].phone = textBox3.Text;
                obj1[1].gender = textBox4.Text;
                obj1[1].major = textBox5.Text;
                obj1[1].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 2)
            {
                obj1[2].name = textBox1.Text;
                obj1[2].reg = textBox2.Text;
                obj1[2].phone = textBox3.Text;
                obj1[2].gender = textBox4.Text;
                obj1[2].major = textBox5.Text;
                obj1[2].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 3)
            {
                obj1[3].name = textBox1.Text;
                obj1[3].reg = textBox2.Text;
                obj1[3].phone = textBox3.Text;
                obj1[3].gender = textBox4.Text;
                obj1[3].major = textBox5.Text;
                obj1[3].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 0)
            {
                obj1[0].name = textBox1.Text;
                obj1[0].reg = textBox2.Text;
                obj1[0].phone = textBox3.Text;
                obj1[0].gender = textBox4.Text;
                obj1[0].major = textBox5.Text;
                obj1[0].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 4)
            {
                obj1[4].name = textBox1.Text;
                obj1[4].reg = textBox2.Text;
                obj1[4].phone = textBox3.Text;
                obj1[4].gender = textBox4.Text;
                obj1[4].major = textBox5.Text;
                obj1[4].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 5)
            {
                obj1[5].name = textBox1.Text;
                obj1[5].reg = textBox2.Text;
                obj1[5].phone = textBox3.Text;
                obj1[5].gender = textBox4.Text;
                obj1[5].major = textBox5.Text;
                obj1[5].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 6)
            {
                obj1[6].name = textBox1.Text;
                obj1[6].reg = textBox2.Text;
                obj1[6].phone = textBox3.Text;
                obj1[6].gender = textBox4.Text;
                obj1[6].major = textBox5.Text;
                obj1[6].add = textBox6.Text;
            }
            else if (dataGridView1.CurrentRow.Index == 7)
            {
                obj1[7].name = textBox1.Text;
                obj1[7].reg = textBox2.Text;
                obj1[7].phone = textBox3.Text;
                obj1[7].gender = textBox4.Text;
                obj1[1].major = textBox5.Text;
                obj1[1].add = textBox6.Text;
            }
             else if (dataGridView1.CurrentRow.Index == 8)
            {
                obj1[8].name = textBox1.Text;
                obj1[8].reg = textBox2.Text;
                obj1[8].phone = textBox3.Text;
                obj1[8].gender = textBox4.Text;
                obj1[8].major = textBox5.Text;
                obj1[8].add = textBox6.Text;
            }
             else if (dataGridView1.CurrentRow.Index == 9)
            {
                obj1[9].name = textBox1.Text;
                obj1[9].reg = textBox2.Text;
                obj1[9].phone = textBox3.Text;
                obj1[9].gender = textBox4.Text;
                obj1[9].major = textBox5.Text;
                obj1[9].add = textBox6.Text;
            }
            dataGridView1.Rows.Clear();
            gridload();
        }

       
    }
     class obj
    {
        public string name, reg, phone, gender, major, add;
        public obj(string p1,string p2, string p3, string p4, string p5,string p6)
        {
            name= p1;
            reg = p2;
            phone = p3;
            gender = p4;
            major = p5;
            add = p6;
        }
    };

}


Students-Records-with-DataGridview.png



No comments:

Post a Comment