DataSet部分示例

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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值