DataSet部分示例

来源:互联网 发布:淘宝卖干果要什么手续 编辑:程序博客网 时间:2024/04/30 03:45

1)创建一个WinForm应用程序
2)在窗体上添加一个DataGridView(dataGridView1)
3)在窗体上添加三个文本框(textBox1,textBox2,textBox3)
4)添加四个Button(button1,button2[标题为delete],button3[标题为remove],button3)
5)添加一个图片框(pictureBox1)

 

 


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 DataSetSample3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet("学生数据集");
        DataTable table = new DataTable("学生");
        private void Form1_Load(object sender, EventArgs e)
        {
            DataColumn dc = table.Columns.Add("序号", typeof(int));
            dc.AutoIncrement = true;
            dc.AutoIncrementSeed = 1;
            dc.AutoIncrementStep = 1;
            dc.ReadOnly = true;
            table.Columns.Add("学号", typeof(string));
            table.Columns["学号"].Unique = true;
            table.Columns.Add("姓名",
                System.Type.GetType("System.String"));
            table.Columns.Add("年龄",typeof(int));
            table.Columns["年龄"].DefaultValue = 20;
            table.Columns.Add("年龄2", typeof(int));
            table.Columns["年龄2"].Expression = "年龄*2";
            table.Columns["年龄2"].ReadOnly = true;
            table.Columns.Add("照片", typeof(byte[]));
            //table.Columns.Add("照片",
            //    System.Type.GetType("System.Byte[]"));
            table.PrimaryKey = new DataColumn[]{
                table.Columns["学号"]};
            ds.Tables.Add(table);
            dataGridView1.DataSource = table;
        }

        //双击可以编辑照片
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            if (openDlg.ShowDialog() == DialogResult.OK)
                pictureBox1.Image = Image.FromFile(openDlg.FileName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataRow row = table.NewRow();
            row["学号"] = textBox1.Text;
            row["姓名"] = textBox2.Text;
            row["年龄"] = int.Parse(textBox3.Text);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
            row["照片"] = ms.ToArray();
            table.Rows.Add(row);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            table.Rows[0].Delete();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            table.Rows.Remove(table.Rows[0]);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataRow[] rows = table.Select("年龄>=20", "年龄 ASC");
            for (int i = 0; i < rows.Length; i++)
                rows[i]["年龄"] = 0;
        }
    }
}

原创粉丝点击