关于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);
        }
    }
}

你可能感兴趣的:(datagridview)