using Chloe; using DNSD_DB; using NewLife.Data; using NewLife.Reflection; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using NDSD_TouchSocket; using NewLife; using TouchSocket.Core; namespace NDSD_Screwdriver { public partial class FrmSetting : Form { public FrmSetting() { InitializeComponent(); var body = "00 03 08 E0 04 01 50 B8 C1 39 3A".ToHex(); var ssss = CRC16.ToModbus(body).ToHex(" "); SqlLiteTool.CreateTable(AppTool.GetDb()); Init(); } public void Init() { dataGridView1.AutoGenerateColumns = false; var ctx = SqlLiteTool.GetDb(AppTool.GetDb()); var list= ctx.Query().Where(x => x.IsEnable == true).ToList(); dataGridView1.DataSource = new BindingList(list);//DataGridView的行可以添加删除(只有允许添加行、删除行) dataGridView1.AllowUserToAddRows = true; dataGridView1.AllowUserToDeleteRows = true; } private DataTable GetTable() { DataTable table = new DataTable(); table.Columns.Add("DisplayMember",typeof(string)); table.Columns.Add("ValueMember", typeof(string)); var dr=table.NewRow(); dr[0] = "绿色"; dr[1] = "1"; table.Rows.Add(dr); dr = table.NewRow(); dr[0] = "黄色"; dr[1] = "2"; table.Rows.Add(dr); dr = table.NewRow(); dr[0] = "红色"; dr[1] = "3"; table.Rows.Add(dr); return table; } private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { e.Row.Cells[0].Value = "0"; e.Row.Cells["D1"].Value = "0"; e.Row.Cells["D2"].Value = "0"; e.Row.Cells["D3"].Value = "0"; } private void btnSave_Click(object sender, EventArgs e) { List list = new List(); foreach (DataGridViewRow row in dataGridView1.Rows) { // 检查行的类型,确保不是新行或者标题行 if (!row.IsNewRow && row.Cells[0].Value != null) { RfidSetting rfidSetting = new RfidSetting(); // 获取当前行的值,并添加到列表中 string rfid = row.Cells[0].Value.ToString(); string green = row.Cells[1].Value.ToString(); string yellow = row.Cells[2].Value.ToString(); string red = row.Cells[3].Value.ToString(); if (string.IsNullOrEmpty(rfid)) { MessageBox.Show("请输入rfid标签"); return; } rfidSetting.IsEnable = true; rfidSetting.CreateDateTime = DateTime.Now; rfidSetting.RfidNo = rfid; if (string.IsNullOrEmpty(green)) { MessageBox.Show("请输入绿灯"); return; } else { if (!IsInRange(green.ToInt())) { MessageBox.Show("绿灯不在有效的范围"); return; } } rfidSetting.Green = green; if (string.IsNullOrEmpty(yellow)) { MessageBox.Show("请输入黄灯"); return; } else { if (!IsInRange(yellow.ToInt())) { MessageBox.Show("绿灯不在有效的范围"); return; } } rfidSetting.Yellow = yellow; if (string.IsNullOrEmpty(red)) { MessageBox.Show("请输入红灯"); return; } else { if (!IsInRange(red.ToInt())) { MessageBox.Show("绿灯不在有效的范围"); return; } } rfidSetting.Red = red; list.Add(rfidSetting); } } if (list.Count > 0) { var ctx = SqlLiteTool.GetDb(AppTool.GetDb()); ctx.Update(a => a.IsEnable == true, a => new RfidSetting() { IsEnable = false }); ctx.InsertRange(list); } MessageBox.Show("保存成功"); } bool IsInRange(int x) { int a = 1; int b = 16; return x >= a && x <= b; } private void button1_Click(object sender, EventArgs e) { byte [] bytes=new byte[]{0x00,0x03,0x00,0x1C,0x00,0x04,0x84,0x1E}; } } }