天天看點

winform:對dataGridView綁定的泛型List<T> 的簡單CRUD

  1.  建立對象類,為所有成員封裝字段,然後重載該類;
  2. 根據已有的對象類(類型參數)建立一個長度可以變化的List數組,并綁定資料源;
  3. 設定dataGridView的column屬性:對應四個對象類建立相應列并用Name屬性進行綁定;
  4. button_onclick事件點選處理;

     public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        List<Person> L1 = new List<Person>();

        private List<Person> L11 = new List<Person>();

        public List<Person> L111

            get { return L11; }

            set { L11 = value; }

         //internal

        private void Form1_Load(object sender, EventArgs e)

            //學号,姓名,年齡,電話

            L111.Add(new Person("1", "王尼瑪", 4, "466666666666"));

            L111.Add(new Person("2", "溫尼瑪", 5, "4444444"));

            L111.Add(new Person("3", "诏尼瑪", 4, "4444444444"));

            L111.Add(new Person("4", "倫尼瑪", 4, "4555555"));

            //this.dataGridView1.DataSource = L1;

            LoadData(L111);

        public void LoadData(List<Person> L1)

            this.dataGridView1.DataSource = new BindingList<Person>(L1);

        //Add

        private void button2_Click(object sender, EventArgs e)

            Add a1 = new Add(this);

            a1.Show();

        //删除

        private void button4_Click(object sender, EventArgs e)

            if (dataGridView1.SelectedRows.Count <= 0)

            {

                return;

            }

            int ind = this.dataGridView1.CurrentRow.Index;

            L111.RemoveAt(ind);

        //退出

        private void button6_Click(object sender, EventArgs e)

            Application.Exit();

        //修改

        private void button3_Click(object sender, EventArgs e)

            //Edit e1 = new Edit(this);

            //e1.Show();

            Person per = this.dataGridView1.CurrentRow.DataBoundItem as Person;

            Add ad = new Add(this, per);

            ad.ShowDialog();

            ;

        //重新整理

        private void button5_Click(object sender, EventArgs e)

        //查詢

        private void button1_Click(object sender, EventArgs e)

            List<Person> L2 = new List<Person>();

            foreach (Person item in L11)

                if(item.Name.Contains(textBox1.Text))

                {

                    L2.Add(item);

                }

            LoadData(L2);

        private void textBox1_TextChanged(object sender, EventArgs e)

    }

  public class Person

        public Person() { }

        public Person(string no, string name, int age, string phone) {

            this.No = no;

            this.Name = name;

            this.Age = age;

            this.Phone = phone;

        //學号

        private string no;

        public string No

            get { return no; }

            set { no = value; }

        //    姓名

        private string name;

        public string Name

            get { return name; }

            set { name = value; }

        //    年齡

        private int age;

        public int Age

            get { return age; }

            set { age = value; }

        //    電話

        private string phone;

        public string Phone

            get { return phone; }

            set { phone = value; }

 public partial class Add : Form

        public Add()

        Form1 f;

        public Add(Form1 f)

            this.f = f;

            button1.Text = "Add";

        //學号,姓名,年齡,電話

        int index;

        //Edit

        public Add(Form1 f, Person per)

            textBox1.Text = per.No;

            textBox2.Text = per.Name;

            textBox3.Text = per.Age.ToString();

            textBox4.Text = per.Phone;

            button1.Text = "Edit";

            index = f.dataGridView1.CurrentRow.Index;

            //非空驗證

            if (textBox1.Text == string.Empty || textBox2.Text == string.Empty || textBox3.Text == string.Empty || textBox4.Text == string.Empty)

                MessageBox.Show("親,記得輸入資料哦");

            else

                //驗證學号是否重複

                foreach (Person item in f.L111)

                    if (item.No == textBox1.Text)

                    {

                        if (button1.Text == "Edit" && item.No != textBox1.Tag.ToString().Trim() || button1.Text == "Add")

                        {

                            MessageBox.Show("學号已存在!");

                            return;

                        }

                    }

                //修改資料源并重新綁定到DataGridView

                Person p1 = new Person(textBox1.Text.Trim(), textBox2.Text.Trim(), int.Parse(textBox3.Text.Trim()), textBox4.Text.Trim());

                if (button1.Text == "Add")

                    f.L111.Add(p1);

                else

                    f.L111[index] = p1;

                    textBox1.Tag = p1.No;

                f.LoadData(f.L111);

                //f.LoadData(L11);

        private void textBox4_TextChanged(object sender, EventArgs e)

        private void Add_Load(object sender, EventArgs e)

            this.Close();

    } 

對應的界面:

winform:對dataGridView綁定的泛型List&lt;T&gt; 的簡單CRUD

還有就是新增和修改調用的界面的都是Add。是以會看到在Add開頭那裡有幾個重載,然後會根據不同的 button1.Text執行相對應的操作。

還有一種是用winform 對dictionary進行操作。

dataGridView資料綁定是this.dataGridView.DataSource = new BindingList<T>(); 

 還有定義一個數組去儲存某一列的值,比如下面這兩句:

            string content = File.ReadAllText(Application.StartupPath + "//User.txt");

            string[] values = Regex.Split(content, "\r\n");

其他列可以用随機數随便搞定,其他的都差不多。 

最後感謝一下我那個外星人同學,版權歸他所有~