关于DataGridView的一些操作

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

namespace led
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.Close();
            }
            this.serialPort1.PortName = this.cbSerial.Text;

            this.serialPort1.Open();

            System.Drawing.Bitmap bmp = new Bitmap(this.tbCurrent.Width, this.tbCurrent.Height);
            this.tbCurrent.DrawToBitmap(bmp,new Rectangle(0,0,bmp.Width,bmp.Height));
            Until.SendBmp(this.serialPort1, bmp);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillAllData(0);   
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {           
            this.tbContent.Text = this.dgvData["content", e.RowIndex].Value.ToString();
            this.tbCurrent.Text = this.dgvData["content", e.RowIndex].Value.ToString();
            this.tbCurrentSortid.Text = this.dgvData["sortid", e.RowIndex].Value.ToString();
            id = e.RowIndex;
        }
        private int id;       

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.tbContent.Text == "")
            {
                MessageBox.Show("输入内容不能为空");
                return;
            }
            int count = Until.GetLineCount(tbContent);
            string[] temp = new string[50];
            for (int i = 0; i < count; i++)
            {
                DB.insertLED(Until.GetLine(tbContent, i));
            }          
            FillAllData(this.dgvData.Rows.Count);
            datachange = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.tbContent.Text == "")
            {
                MessageBox.Show("输入内容不能为空");
                return;
            }
            int count = Until.GetLineCount(tbContent);
            if (count > 1)
            {
                MessageBox.Show("修改只限于1行,不能多于一行");
                return;
            }           
            if (DB.updateLEDcontent(tbContent.Text,dgvData["id",id].Value.ToString()) > 0)
            {
                FillAllData(id);
                datachange = true;
            }           
        }

        private void FillAllData(int selectid)
        {
            this.dgvData.DataSource = DB.selectLEDcontent();
            this.dgvData.Columns["id"].Visible = false;
            this.dgvData.Columns["content"].HeaderText = "显示内容";
            this.dgvData.Columns["content"].Width = 300;
            this.dgvData.Columns["sortid"].HeaderText = "序号";
            this.dgvData.Columns["inputtime"].HeaderText = "最近修改时间";
            this.dgvData.Columns["inputtime"].Width = 200;

            if (selectid > dgvData.Rows.Count - 1)
            {
                selectid = dgvData.Rows.Count - 1;
            }
            this.dgvData.CurrentCell = this.dgvData.Rows[selectid].Cells["content"];
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DB.updateLEDsortid(dgvData["id", id].Value.ToString(), this.tbCurrentSortid.Text, this.nudNewSortid.Value.ToString());
            FillAllData((int)this.nudNewSortid.Value);
            datachange = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DB.updateLEDsortidlast(dgvData["id", id].Value.ToString(), this.tbCurrentSortid.Text);
            FillAllData(this.dgvData.Rows.Count);
            datachange = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.timer1.Interval = (int)this.nudTime.Value * 1000;
            LoadData();
            this.timer1.Enabled = true;
        }

        private bool datachange = false;
        private string[] datatemp;
        private int datan = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (datachange)
            {
                LoadData();
                datan = 0;
                datachange = false;
            }
            try
            {
                this.tbCurrent.Text = datatemp[datan];               
            }
            catch
            {
                datan = 0;
                this.tbCurrent.Text = datatemp[datan];
            }
            datan++;
            //SendBySPort(this.textBox1);
        }

        private void LoadData()
        {
            datachange = true;
            datan = 0;
            DataTable dt = DB.selectLEDcontent();
            int i = 0;
            datatemp = new string[dt.Rows.Count];
            foreach (DataRow r in dt.Rows)
            {
                datatemp[i] = r["content"].ToString();
                i++;
            }
        }

        private void SendBySPort(TextBox tb)
        {
            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.Close();
            }
            this.serialPort1.PortName = this.cbSerial.Text;

            this.serialPort1.Open();

            System.Drawing.Bitmap bmp = new Bitmap(this.tbCurrent.Width, this.tbCurrent.Height);
            tb.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
            Until.SendBmp(this.serialPort1, bmp);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
        }

        private void btnDeleteContent_Click(object sender, EventArgs e)
        {
            DB.deleteLED(dgvData["id", id].Value.ToString());
            FillAllData(id);
        }
    }
}

转载于:https://www.cnblogs.com/raymond19840709/archive/2006/10/16/530452.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值