此文件实现的Winform中DataGridView中的大部分功能:
合并表头;
读取单元格数据
添加一列下拉框
添加一列输入框
添加一行数据
隔行显示不同的颜色
// ***************************************************
using ControlFor.Common;
using ControlFor.DataModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ControlFor.Media
{
public partial class DirectiveRuleForm : Form
{
/// <summary>
/// 设备类型数据
/// </summary>
dynamic m_device_data;
/// <summary>
/// 功能类型数据
/// </summary>
dynamic m_function_data;
/// <summary>
/// 命令数据
/// </summary>
dynamic m_command_data;
/// <summary>
/// 父窗体
/// </summary>
AddEditThreeInOnePanelForm m_parent_form;
/// <summary>
/// 设备类型列表
/// </summary>
List<dynamic> m_list_deviceType;
/// <summary>
/// 功能类型列表
/// </summary>
List<dynamic> m_list_function;
/// <summary>
/// 发送指令列表
/// </summary>
List<List<dynamic>> m_list_send_command;
/// <summary>
/// 接受指令列表
/// </summary>
List<List<dynamic>> m_list_receive_command;
/// <summary>
/// 列表头
/// </summary>
List<String> colsHeaderText_V = new List<String>();
/// <summary>
/// 当前选中行
/// </summary>
public int m_current_select_row;
public DirectiveRuleForm()
{
InitializeComponent();
}
/// <summary>
/// 设置指令窗体
/// 作者:徐文文
/// </summary>
/// <param name="my_device_data">设备类型数据</param>
/// <param name="my_function_data">功能类型数据</param>
/// <param name="my_command_data">命令数据</param>
/// <param name="my_parent_form">父窗体</param>
public DirectiveRuleForm(dynamic my_device_data, dynamic my_function_data, dynamic my_command_data, AddEditThreeInOnePanelForm my_parent_form)
{
m_device_data = my_device_data;
m_function_data = my_function_data;
m_command_data = my_command_data;
m_parent_form = my_parent_form;
InitializeComponent();
initCheckTypeComboxFunc();
initDataFunc();
initColumnFunc();
// initDataSourceFunc();
putDataSouceToDataGridViewFunc();
}
/// <summary>
/// 初始化数据(解析数据)
/// 作者:徐文文
/// </summary>
public void initDataFunc()
{
try
{
m_list_deviceType = JsonConvert.DeserializeObject<List<dynamic>>(m_device_data.re.ToString());
m_list_function = JsonConvert.DeserializeObject<List<dynamic>>(m_function_data.re.ToString());
#region 解析数据
List<dynamic> my_list_send = JsonConvert.DeserializeObject<List<dynamic>>(m_command_data.re.sendList.ToString());
List<dynamic> my_list_receive = JsonConvert.DeserializeObject<List<dynamic>>(m_command_data.re.receiveList.ToString());
//发送命令的集合
List<List<dynamic>> my_list_send_command = new List<List<dynamic>>();
List<dynamic> my_list_send_single = new List<dynamic>();
//接受命令的集合
List<List<dynamic>> my_list_receive_command = new List<List<dynamic>>();
List<dynamic> my_list_receive_single = new List<dynamic>();
#region 解析发送数据段
for (int i = 0; i < my_list_send.Count; i++)
{
dynamic my_dy_send = my_list_send[i];
int my_int_command_order = int.Parse(my_dy_send.command_order != null ? my_dy_send.command_order.ToString() : "0");
if (i != my_list_send.Count - 1) //不等于最后一个
{
dynamic my_dy_send_next = my_list_send[i + 1];
int my_int_command_order_next = int.Parse(my_dy_send_next.command_order != null ? my_dy_send_next.command_order.ToString() : "0");
if (my_int_command_order > my_int_command_order_next) //一条指令添加结束
{
my_list_send_single.Add(my_dy_send);
my_list_send_command.Add(my_list_send_single);
my_list_send_single = new List<dynamic>();
}
else //添加在一条指令中
{
my_list_send_single.Add(my_dy_send);
}
}
else //最后一个
{
my_list_send_single.Add(my_dy_send);
my_list_send_command.Add(my_list_send_single);
my_list_send_single = new List<dynamic>();
}
}
Console.WriteLine(my_list_send_command.Count());
#endregion
#region 解析接受数据段
for (int i = 0; i < my_list_receive.Count; i++)
{
dynamic my_dy_receive = my_list_receive[i];
int my_int_command_order = int.Parse(my_dy_receive.command_order != null ? my_dy_receive.command_order.ToString() : "0");
if (i != my_list_receive.Count - 1) //不等于最后一个
{
dynamic my_dy_receive_next = my_list_receive[i + 1];
int my_int_command_order_next = int.Parse(my_dy_receive_next.command_order != null ? my_dy_receive_next.command_order.ToString() : "0");
if (my_int_command_order > my_int_command_order_next) //一条指令添加结束
{
my_list_receive_single.Add(my_dy_receive);
my_list_receive_command.Add(my_list_receive_single);
my_list_receive_single = new List<dynamic>();
}
else //添加在一条指令中
{
my_list_receive_single.Add(my_dy_receive);
}
}
else //最后一个
{
my_list_receive_single.Add(my_dy_receive);
my_list_receive_command.Add(my_list_receive_single);
my_list_receive_single = new List<dynamic>();
}
}
Console.WriteLine(my_list_receive_command.Count());
#endregion
m_list_send_command = my_list_send_command;
m_list_receive_command = my_list_receive_command;
#endregion
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "DirectiveRuleForm->initData");
}
}
/// <summary>
/// 初始化前面四列数据表头
/// </summary>
public void initFirstForuthFunc()
{
try
{
#region 删除
//DataGridViewButtonColumn my_col_btn = new DataGridViewButtonColumn();
//my_col_btn.Name = "DeleteBtn";
//my_col_btn.HeaderText = "删除";
//my_col_btn.DefaultCellStyle.NullValue = "删除";//添加按钮显示的名字
#endregion
//dgv_rule.Columns.Add(my_col_btn);
#region 设备类型
DataGridViewComboBoxColumn my_col1 = new DataGridViewComboBoxColumn();
//要插入列的类型
my_col1.Name = "deviceType";
my_col1.HeaderText = "设备类型";
DataTable my_deviceType_dts = new DataTable();
my_deviceType_dts.Columns.Add("name");
my_deviceType_dts.Columns.Add("value");
DataRow drr1 = my_deviceType_dts.NewRow();
drr1[0] = "请选择设备";
drr1[1] = "-1";
my_deviceType_dts.Rows.Add(drr1);
for (int i = 0; i < 4; i++)
{
DataRow drr = my_deviceType_dts.NewRow();
drr[0] = m_list_deviceType[i].dic_name == null ? "请选择设备" : m_list_deviceType[i].dic_name.ToString();
drr[1] = m_list_deviceType[i].dic_value == null ? "-1" : m_list_deviceType[i].dic_value.ToString();
my_deviceType_dts.Rows.Add(drr);
}
my_col1.DataSource = my_deviceType_dts;
my_col1.DataPropertyName = "name";
my_col1.DisplayMember = "name";
my_col1.ValueMember = "value";
#endregion
#region 功能
DataGridViewComboBoxColumn my_col2 = new DataGridViewComboBoxColumn();
//要插入列的类型
my_col2.Name = "function";
my_col2.HeaderText = "功能";
DataTable my_function_dts = new DataTable();
my_function_dts.Columns.Add("name");
my_function_dts.Columns.Add("value");
DataRow drr2 = my_function_dts.NewRow();
drr2[0] = "请选择功能";
drr2[1] = "-1";
my_function_dts.Rows.Add(drr2);
for (int i = 4; i < 9; i++)
{
DataRow drr = my_function_dts.NewRow();
drr[0] = m_list_deviceType[i].dic_name == null ? "请选择功能" : m_list_deviceType[i].dic_name.ToString();
drr[1] = m_list_deviceType[i].dic_value == null ? "-1" : m_list_deviceType[i].dic_value.ToString();
my_function_dts.Rows.Add(drr);
}
my_col2.DataSource = my_function_dts;
my_col2.DataPropertyName = "name";
my_col2.DisplayMember = "name";
my_col2.ValueMember = "value";
#endregion
#region 指令类型
DataGridViewComboBoxColumn my_col3 = new DataGridViewComboBoxColumn();
//要插入列的类型
my_col3.Name = "commandType";
my_col3.HeaderText = "指令类型";
DataTable my_dts3 = new DataTable();
my_dts3.Columns.Add("name");
my_dts3.Columns.Add("value");
DataRow drr3 = my_dts3.NewRow();
drr3[0] = "请选择指令类型";
drr3[1] = "-1";
my_dts3.Rows.Add(drr3);
for (int i = 9; i < 11; i++)
{
DataRow drr = my_dts3.NewRow();
drr[0] = m_list_deviceType[i].dic_name == null ? "请选择指令类型" : m_list_deviceType[i].dic_name.ToString();
drr[1] = m_list_deviceType[i].dic_value == null ? "-1" : m_list_deviceType[i].dic_value.ToString();
my_dts3.Rows.Add(drr);
}
my_col3.DataSource = my_dts3;
my_col3.DataPropertyName = "name";
my_col3.DisplayMember = "name";
my_col3.ValueMember = "value";
#endregion
#region 是否编辑
DataGridViewTextBoxColumn my_col4 = new DataGridViewTextBoxColumn();
//要插入列的类型
my_col4.Name = "isEdit";
my_col4.HeaderText = "是否编辑";
my_col4.Visible = false;
#endregion
dgv_rule.Columns.Add(my_col1);
dgv_rule.Columns.Add(my_col2);
dgv_rule.Columns.Add(my_col3);
dgv_rule.Columns.Add(my_col4);
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "DirectiveRuleForm->initFirstForuthFunc");
}
}
/// <summary>
/// 初始化DataGridView数据
/// </summary>
public void initDataSourceFunc()
{
dgv_rule.Rows.Clear();
for (int i = 0; i < m_list_send_command.Count; i++)
{
List<dynamic> my_dy_send_data = m_list_send_command[i];
int index = this.dgv_rule.Rows.Add();
int my_col = dgv_rule.Columns.Count;
//设备类型
this.dgv_rule.Rows[index].Cells[0].Value = my_dy_send_data[0].product_type == null ? "-1": my_dy_send_data[0].product_type.ToString();
//功能
this.dgv_rule.Rows[index].Cells[1].Value = my_dy_send_data[0].function_type == null ? "-1" : my_dy_send_data[0].function_type.ToString();
//指令类型
this.dgv_rule.Rows[index].Cells[2].Value = my_dy_send_data[0].command_type == null ? "-1" : my_dy_send_data[0].command_type.ToString();
//未编辑
this.dgv_rule.Rows[index].Cells[3].Value = "0";
}
for (int i = 0; i < m_list_receive_command.Count; i++)
{
List<dynamic> my_dy_receive_data = m_list_receive_command[i];
int index = this.dgv_rule.Rows.Add();
int my_col = dgv_rule.Columns.Count;
//设备类型
this.dgv_rule.Rows[index].Cells[0].Value = my_dy_receive_data[0].product_type == null ? "-1" : my_dy_receive_data[0].product_type.ToString();
//功能
this.dgv_rule.Rows[index].Cells[1].Value = my_dy_receive_data[0].function_type == null ? "-1" : my_dy_receive_data[0].function_type.ToString();
//指令类型
this.dgv_rule.Rows[index].Cells[2].Value = my_dy_receive_data[0].command_type == null ? "-1" : my_dy_receive_data[0].command_type.ToString();
//未编辑
this.dgv_rule.Rows[index].Cells[3].Value = "0";
}
}
/// <summary>
/// 初始化校验下拉框
/// 作者:徐文文
/// </summary>
public void initCheckTypeComboxFunc()
{
try
{
dynamic my_data = CommonGetInterFace.getCheckType("check");
List<dynamic> typelist = JsonConvert.DeserializeObject<List<dynamic>>(my_data.re.ToString());
DataTable my_deviceType_dts = new DataTable();
my_deviceType_dts.Columns.Add("name");
my_deviceType_dts.Columns.Add("value");
for (int i = 0; i < typelist.Count; i++)
{
DataRow drr = my_deviceType_dts.NewRow();
drr[0] = typelist[i].dic_name == null ? "请选择设备" : typelist[i].dic_name.ToString();
drr[1] = typelist[i].dic_value == null ? "-1" : typelist[i].dic_value.ToString();
my_deviceType_dts.Rows.Add(drr);
}
cmb_checkType.DataSource = my_deviceType_dts;
cmb_checkType.DisplayMember = "name";
cmb_checkType.ValueMember = "value";
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "DirectiveRuleForm->initCheckTypeComboxFunc");
}
}
/// <summary>
/// 将返回的数据放在表格中
/// </summary>
public void putDataSouceToDataGridViewFunc()
{
dgv_rule.Rows.Clear();
for (int i = 0; i < m_list_send_command.Count; i++)
{
List<dynamic> my_dy_send_data = m_list_send_command[i];
int index = this.dgv_rule.Rows.Add();
int my_col = dgv_rule.Columns.Count;
//设备类型
this.dgv_rule.Rows[index].Cells[0].Value = my_dy_send_data[0].product_type == null ? "-1" : my_dy_send_data[0].product_type.ToString();
//功能
this.dgv_rule.Rows[index].Cells[1].Value = my_dy_send_data[0].function_type == null ? "-1" : my_dy_send_data[0].function_type.ToString();
//指令类型
this.dgv_rule.Rows[index].Cells[2].Value = my_dy_send_data[0].command_type == null ? "-1" : my_dy_send_data[0].command_type.ToString();
//未编辑
this.dgv_rule.Rows[index].Cells[3].Value = "0";
for (int j = 0; j < my_dy_send_data.Count; j++)
{
int my_temp_j = 2 * j;
//string my_str_cmd = my_dy_send_data[j].command_fun == null ? "-1" : my_dy_send_data[j].command_fun.ToString();
this.dgv_rule.Rows[index].Cells[4 + my_temp_j].Value = my_dy_send_data[j].command_fun == null ? "-1" : my_dy_send_data[j].command_fun.ToString();
this.dgv_rule.Rows[index].Cells[5 + my_temp_j].Value = my_dy_send_data[j].command == null ? "" : my_dy_send_data[j].command.ToString();
}
}
for (int i = 0; i < m_list_receive_command.Count; i++)
{
List<dynamic> my_dy_receive_data = m_list_receive_command[i];
int index = this.dgv_rule.Rows.Add();
int my_col = dgv_rule.Columns.Count;
//设备类型
this.dgv_rule.Rows[index].Cells[0].Value = my_dy_receive_data[0].product_type == null ? "-1" : my_dy_receive_data[0].product_type.ToString();
//功能
this.dgv_rule.Rows[index].Cells[1].Value = my_dy_receive_data[0].function_type == null ? "-1" : my_dy_receive_data[0].function_type.ToString();
//指令类型
this.dgv_rule.Rows[index].Cells[2].Value = my_dy_receive_data[0].command_type == null ? "-1" : my_dy_receive_data[0].command_type.ToString();
//未编辑
this.dgv_rule.Rows[index].Cells[3].Value = "0";
for (int j = 0; j < my_dy_receive_data.Count; j++)
{
int my_temp_j = 2 * j;
//string my_str_cmd = my_dy_send_data[j].command_fun == null ? "-1" : my_dy_send_data[j].command_fun.ToString();
this.dgv_rule.Rows[index].Cells[4 + my_temp_j].Value = my_dy_receive_data[j].command_fun == null ? "-1" : my_dy_receive_data[j].command_fun.ToString();
this.dgv_rule.Rows[index].Cells[5 + my_temp_j].Value = my_dy_receive_data[j].command == null ? "" : my_dy_receive_data[j].command.ToString();
}
}
DgvRowColor(dgv_rule);
}
private void dgv_rule_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
}
private void DirectiveRuleForm_Load(object sender, EventArgs e)
{
#region 双缓存
Type type = dgv_rule.GetType();
PropertyInfo pi = type.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv_rule, true, null);
#endregion
}
public int getListMaxCountFunc(List<List<dynamic>> my_dy_list)
{
int my_result = 0;
List<int> my_count_list = new List<int>();
for (int i = 0; i < my_dy_list.Count; i++)
{
my_count_list.Add(my_dy_list[i].Count);
}
my_result = my_count_list.Max();
return my_result;
}
private void dgv_rule_Scroll(object sender, ScrollEventArgs e)
{
if (colsHeaderText_V.Count > 0)
{
for (int i = 0; i < colsHeaderText_V.Count; i++)
{
int my_int = i * 2;
Rectangle rect = Rectangle.Union(
dgv_rule.GetCellDisplayRectangle(4 + my_int, -1, true),
dgv_rule.GetCellDisplayRectangle(my_int + 5, -1, true));
dgv_rule.Invalidate(rect);
}
}
}
private void dgv_rule_Paint(object sender, PaintEventArgs e)
{
if (colsHeaderText_V.Count > 0)
{
for (int i = 0; i < colsHeaderText_V.Count; i++)
{
int my_int = i * 2;
Rectangle r1 = dgv_rule.GetCellDisplayRectangle(4 + my_int, -1, true);
Rectangle r2 = dgv_rule.GetCellDisplayRectangle(my_int + 5, -1, true);
r1.X += 1;
r1.Y += 2;
r1.Width += r2.Width - 2;
r1.Height -= 4;
using (SolidBrush br = new SolidBrush(dgv_rule.ColumnHeadersDefaultCellStyle.BackColor))
{
e.Graphics.FillRectangle(br, r1);
}
//draw text
using (SolidBrush br = new SolidBrush(this.dgv_rule.ColumnHeadersDefaultCellStyle.ForeColor))
{
StringFormat sf = new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
};
e.Graphics.DrawString(colsHeaderText_V[i], dgv_rule.ColumnHeadersDefaultCellStyle.Font, br, r1, sf);
}
}
}
}
/// <summary>
/// 添加一列
/// 作者:徐文文
/// </summary>
public void addColumnFunc()
{
try
{
List<string> my_list = colsHeaderText_V.FindAll(t => t.Contains("Command"));
int my_count = my_list.Count;
if (my_count == 0)
{
colsHeaderText_V.Add("Command");
}
else
{
colsHeaderText_V.Add("Command" + my_count.ToString());
}
#region 添加列,其实都是添加两列(第一列:下拉框;第二列:输入框)
int my_dgv_count = dgv_rule.Columns.Count;
#region 添加下拉框一列
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
//要插入列的类型
col.Name = "Command" + my_count.ToString();
col.HeaderText = "Command" + my_count.ToString() + "cmb";
DataTable dts = new DataTable();
dts.Columns.Add("name");
dts.Columns.Add("value");
DataRow drr = dts.NewRow();
drr[0] = "请选择命令";
drr[1] = "-1";
dts.Rows.Add(drr);
for (int i = 0; i < m_list_function.Count; i++)
{
DataRow dr = dts.NewRow();
dr[0] = m_list_function[i].dic_name == null ? "请选择指令" : m_list_function[i].dic_name.ToString();
dr[1] = m_list_function[i].dic_value == null ? "-1" : m_list_function[i].dic_value.ToString();
dts.Rows.Add(dr);
}
col.DataSource = dts;
col.DataPropertyName = "name";
col.DisplayMember = "name";
col.ValueMember = "value";
dgv_rule.Columns.Add(col);
#endregion
#region 添加输入框一列
DataGridViewTextBoxColumn colt = new DataGridViewTextBoxColumn();
//要插入列的类型
colt.Name = "Command" + my_count.ToString();
colt.HeaderText = "Command" + my_count.ToString() + "txt";
dgv_rule.Columns.Add(colt);
#endregion
#endregion
int my_dgv_row_count = this.dgv_rule.Rows.Count;
for (int i = 0; i < my_dgv_row_count; i++)
{
this.dgv_rule.Rows[i].Cells[my_dgv_count].Value = "-1";//主键
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "DirectiveRuleForm->addColumnFunc");
}
}
/// <summary>
/// 添加行
/// 作者:徐文文
/// </summary>
public void addRowFunc()
{
try
{
int index = this.dgv_rule.Rows.Add();
int my_col = dgv_rule.Columns.Count;
//设备类型
this.dgv_rule.Rows[index].Cells[0].Value = "-1";
//功能
this.dgv_rule.Rows[index].Cells[1].Value = "-1";
//指令类型
this.dgv_rule.Rows[index].Cells[2].Value = "-1";
//未编辑
this.dgv_rule.Rows[index].Cells[3].Value = "0";
for (int i = 4; i < my_col; i++)
{
this.dgv_rule.Rows[index].Cells[i].Value = "-1";
this.dgv_rule.Rows[index].Cells[i+1].Value = "";
i++;
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "DirectiveRuleForm->addRowFunc");
}
}
/// <summary>
/// 初始化列
/// </summary>
public void initColumnFunc()
{
initFirstForuthFunc();
int my_send_count = getListMaxCountFunc(m_list_send_command);
int my_receive_count = getListMaxCountFunc(m_list_receive_command);
int my_max_command_count = my_send_count;
if (my_receive_count > my_send_count)
{
my_max_command_count = my_receive_count;
}
for (int i = 0; i < my_max_command_count; i++)
{
addColumnFunc();
}
}
private void btn_add_column_Click(object sender, EventArgs e)
{
addColumnFunc();
}
private void btn_add_row_Click(object sender, EventArgs e)
{
addRowFunc();
DgvRowColor(dgv_rule);
}
/// <summary>
/// 提交
/// 作者:徐文文
/// </summary>
/// <param name="sender">触发对象</param>
/// <param name="e">传递参数</param>
private void btn_submit_Click(object sender, EventArgs e)
{
try
{
Dictionary<string, object> my_param = new Dictionary<string, object>();
List<Dictionary<string, object>> keyValuePairs = new List<Dictionary<string, object>>();
if (dgv_rule.Rows.Count > 0 && dgv_rule.Rows[0].Cells[0].Value != null && dgv_rule.Rows[0].Cells[0].Value.ToString() != "")
{
foreach (DataGridViewRow r in dgv_rule.Rows)
{
if (r.Cells[0].Value != null && r.Cells[0].Value.ToString() != "" && r.Cells[3].Value != null && r.Cells[3].Value.ToString() == "1")
{
if (r.Cells[0].Value == null || r.Cells[0].Value.ToString() == "-1"|| r.Cells[1].Value == null || r.Cells[1].Value.ToString() == "-1"|| r.Cells[2].Value == null || r.Cells[2].Value.ToString() == "-1")
{
MessageBox.Show("请检查"+r.Index.ToString()+"行,前三列数据是否错误。");
break;
}
for (int i = 0; i < (dgv_rule.Columns.Count - 4)/2; i++)
{
int my_col_temp = i * 2;
string my_str_cmd = r.Cells[4 + i].Value != null ? r.Cells[4 + i].Value.ToString() : "-1";
if (my_str_cmd == "-1")
{
break;
}
else
{
Dictionary<string, object> keyValuePairs1 = new Dictionary<string, object>();
keyValuePairs1.Add("command_fun", r.Cells[4 + my_col_temp].Value.ToString());
keyValuePairs1.Add("command", r.Cells[5 + my_col_temp].Value.ToString());
keyValuePairs1.Add("command_order", i+1);
keyValuePairs1.Add("rule_uuid", "");
keyValuePairs1.Add("product_type", r.Cells[0].Value.ToString());
keyValuePairs1.Add("function_type", r.Cells[1].Value.ToString());
keyValuePairs1.Add("command_type", r.Cells[2].Value.ToString());
keyValuePairs.Add(keyValuePairs1);
}
}
}
}
}
if (keyValuePairs.Count > 0)
{
my_param.Add("device_type_uuid", m_parent_form.m_deviceType_uuid);
my_param.Add("brand_uuid", m_parent_form.m_brandType_uuid);
my_param.Add("check_type", cmb_checkType.SelectedValue.ToString());
int my_checkstart = 0;
if (!string.IsNullOrEmpty(txt_start_check.Text))
{
if (int.TryParse(txt_start_check.Text, out my_checkstart))
{
//donothing
}
else
{
my_checkstart = 0;
}
}
else
{
my_checkstart = 0;
}
int my_checkend = 0;
if (!string.IsNullOrEmpty(txt_end_check.Text))
{
if (int.TryParse(txt_start_check.Text, out my_checkend))
{
//donothing
}
else
{
my_checkend = 0;
}
}
else
{
my_checkend = 0;
}
my_param.Add("checkStart", my_checkstart);
my_param.Add("checkEnd", my_checkend);
my_param.Add("commandList", keyValuePairs);
string gstr = CommonClass.http.NewPostNew(InfoClass.api + @"/ToolRest/saveCommandRule", my_param);
dynamic data = JsonConvert.DeserializeObject<dynamic>(gstr);
if (data.code > 0)
{
MessageBox.Show("保存成功");
this.Close();
}
else
{
MessageBox.Show("保存失败" + data.msg.ToString());
}
}
else
{
MessageBox.Show("就没有可以提交的数据");
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "DirectiveRuleForm->btn_submit_Click");
}
}
private void dgv_rule_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
dgv_rule[3, e.RowIndex].Value = "1";//认定被修改过
}
if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && dgv_rule[e.ColumnIndex, e.RowIndex] != null && !dgv_rule[e.ColumnIndex, e.RowIndex].ReadOnly)
{
DataGridViewComboBoxColumn comboBoxColumn = dgv_rule.Columns[e.ColumnIndex] as DataGridViewComboBoxColumn;
if (comboBoxColumn != null)
{
this.dgv_rule.CurrentCell = dgv_rule[e.ColumnIndex, e.RowIndex];
dgv_rule.BeginEdit(true);
DataGridViewComboBoxEditingControl comboBoxEditingControl = dgv_rule.EditingControl as DataGridViewComboBoxEditingControl;
if (comboBoxEditingControl != null)
{
comboBoxEditingControl.DroppedDown = true;
}
}
}
}
private void dgv_rule_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0 && e.ColumnIndex >=0)
{
m_current_select_row = e.RowIndex;
//弹出操作菜单
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
deleteOneDataFunc(m_current_select_row);
}
/// <summary>
/// 删除一条数据
/// 作者:徐文文
/// </summary>
/// <param name="my_index">序号</param>
public void deleteOneDataFunc(int my_index)
{
try
{
string my_product_type;
string my_function_type;
string my_command_type;
if (dgv_rule.Rows[my_index].Cells[0].Value != null && dgv_rule.Rows[my_index].Cells[0].Value.ToString() != "-1")
{
my_product_type = dgv_rule.Rows[my_index].Cells[0].Value.ToString();
}
else
{
MessageBox.Show("设备类型为空,不能删除指令");
return;
}
if (dgv_rule.Rows[my_index].Cells[1].Value != null && dgv_rule.Rows[my_index].Cells[1].Value.ToString() != "-1")
{
my_function_type = dgv_rule.Rows[my_index].Cells[1].Value.ToString();
}
else
{
MessageBox.Show("功能类型为空,不能删除指令");
return;
}
if (dgv_rule.Rows[my_index].Cells[2].Value != null && dgv_rule.Rows[my_index].Cells[2].Value.ToString() != "-1")
{
my_command_type = dgv_rule.Rows[my_index].Cells[2].Value.ToString();
}
else
{
MessageBox.Show("发送/接收类型为空,不能删除指令");
return;
}
Dictionary<string, string> my_param = new Dictionary<string, string>();
my_param.Add("product_type", my_product_type);
my_param.Add("function_type", my_function_type);
my_param.Add("command_type", my_command_type);
my_param.Add("device_type_uuid", m_parent_form.m_deviceType_uuid);
my_param.Add("brand_uuid", m_parent_form.m_brandType_uuid);
string gstr = CommonClass.http.NewPost(InfoClass.api + @"/ToolRest/deleteCommand", my_param);
dynamic data = JsonConvert.DeserializeObject<dynamic>(gstr);
if (data.code > 0)
{
MessageBox.Show("删除成功");
this.Close();
}
else
{
MessageBox.Show("删除失败" + data.msg.ToString());
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "DirectiveRuleForm->deleteOneDataFunc");
}
}
private void btn_delete_Click(object sender, EventArgs e)
{
deleteOneDataFunc(m_current_select_row);
}
/// <summary>
/// 隔行变色
/// 作者:徐文文
/// </summary>
/// <param name="my_dgv">传入DataGridView控件名称</param>
public void DgvRowColor(System.Windows.Forms.DataGridView my_dgv)
{
if (my_dgv.Rows.Count != 0)
{
for (int i = 0; i < my_dgv.Rows.Count; i++)
{
if ((i + 1) % 2 == 0)
{
my_dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(0, 191, 255);
}
else
{
my_dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(144, 238, 144);
}
}
}
}
}
}