一、效果图
二、表格制作
①在窗体程序界面拖拽一个DataGridView控件,并且放置好位置,如下图所示:
②修改该DataGridView控件的名称和对应的窗体适配属性
③表格布局的基础设置
#region 表格设置
///
/// 添加表格标题
///
private void AddTableTitle()
{
dtAllInfoData.Columns.Add("Number");
dtAllInfoData.Columns.Add(ID);
dtAllInfoData.Columns.Add(ParameterDesc);
dtAllInfoData.Columns.Add(ParameterAddress);
}
///
/// 每一行都包含编辑删除按钮
///
private void SingleRowOfEditAndDelete()
{
dlink1.Text = "修改";
dlink1.Name = "Eidt";
dlink1.HeaderText = "操作";
dlink1.UseColumnTextForLinkValue = true;
dlink1.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dlink2.DisplayIndex = 4;
GridView_EqParameter.Columns.Add(dlink1);
dlink2.Text = "删除";
dlink2.Name = "Delete";
dlink2.HeaderText = "操作";
dlink2.UseColumnTextForLinkValue = true;
dlink2.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dlink2.DisplayIndex = 5;
GridView_EqParameter.Columns.Add(dlink2);
dbutton.Text = "查看";
dbutton.Name = "Look";
dbutton.HeaderText = "操作";
dbutton.UseColumnTextForButtonValue = true;
dbutton.AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells;
dbutton.FlatStyle = FlatStyle.Standard;
dbutton.CellTemplate.Style.BackColor = Color.Honeydew;
dbutton.DisplayIndex = 6;
GridView_EqParameter.Columns.Add(dbutton);
}
///
/// 表格标题名称
///
private void TableTitleName()
{
//修改表格标题头显示名称
GridView_EqParameter.Columns[0].HeaderText = "序号";
//GridView_EqParameter.Columns[1].HeaderText = "参数编号";
GridView_EqParameter.Columns[2].HeaderText = "PLC检测参数名称";
GridView_EqParameter.Columns[3].HeaderText = "PLC检测参数地址";
//表格样式
GridView_EqParameter.Columns[0].Width = 30;
GridView_EqParameter.Columns[1].Width = 30;
GridView_EqParameter.Columns[ID].Visible = false;//隐藏数据库的ID列
GridView_EqParameter.Columns[2].Width = 80;
GridView_EqParameter.Columns[3].Width = 80;
GridView_EqParameter.Columns[4].Width = 30;
GridView_EqParameter.Columns[5].Width = 30;
GridView_EqParameter.Columns[6].Width = 20;
//根据数据内容自动调整列、行宽
//GridView_EqParameter.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
//GridView_EqParameter.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
GridView_EqParameter.ColumnHeadersDefaultCellStyle.Font= new Font("黑体", 15, FontStyle.Bold);
GridView_EqParameter.RowsDefaultCellStyle.Font = new Font("黑体",13, FontStyle.Regular);
}
#endregion
三、数据加载
#region 数据操作
///
/// 加载当前设备参数类型的所有信息
///
/// 当前设备参数类型名称
private void LoadCurEqParameterTypeAllInfos(string curEqParameterTypeName)
{
//清空缓存
dtAllInfoData.Clear();
if (!string.IsNullOrEmpty(curEqParameterTypeName))
{
//获取当前设备参数类型
curEqParameterType = curEqParameterTypeName;
Global_Parameter.curEqParameterType = curEqParameterType;
//获取当前设备ID
curEquipmentID = Global_Parameter.curEquipmentID;
if (!string.IsNullOrEmpty(curEquipmentID))
{
int eq_ID = int.Parse(curEquipmentID);
//加载角度信息
List eqParameterDatas = new List();
int numbers = CommonEqParameterOperation.GetInstance().QueryCurEqParameterOfTypeTotalNumbers(eq_ID, curEqParameterType);
eqParameterDatas = CommonEqParameterOperation.GetInstance().QueryCurEqParameterOfTypeAllInfos(eq_ID, curEqParameterType, 0, numbers);
int count = eqParameterDatas.Count;
if (count > 0)
{
for (int i = 0; i < count; i++)
{
row = dtAllInfoData.NewRow();
row["Number"] = i + 1;
row[ID] = eqParameterDatas[i].ID;
row[ParameterDesc] = eqParameterDatas[i].ParameterDesc;
row[ParameterAddress] = eqParameterDatas[i].ParameterAddress;
dtAllInfoData.Rows.Add(row);
}
if (row != null)
{
GridView_EqParameter.DataSource = dtAllInfoData;
if (firstOne == 1)
{
//每一行都包含编辑删除按钮
SingleRowOfEditAndDelete();
}
firstOne = 0;
}
//表格标题名称
TableTitleName();
}
else
{
MessageBox.Show("当前没有参数类型的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("请选择设备!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("当前设备类型参数为NULL!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
四、获取当前选中数据行编辑信息
///
/// 获取到当前选中行的数据信息
///
/// 查询值的数据库字段名称
///
private string GetCurSelectedRowData(string queryValueName)
{
string value = string.Empty;
if (!string.IsNullOrEmpty(queryValueName))
{
value= GridView_EqParameter.Rows[GridView_EqParameter.CurrentRow.Index].Cells[queryValueName].Value.ToString();
}
return value;
}
五、 编辑、删除当前选中行的事件方法
①首先选中表格控件(DataGridView)的事件属性,双击如下所示的红框内容编写事件方法
②编写对应的事件方法
//编辑、删除事件方法
private void GridView_EqParameter_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int col = e.ColumnIndex;
//获取到当前设备参数的ID
curSelectedEqParameterID = GetCurSelectedRowData(ID);
//编辑修改
if (col == 4)
{
if (!string.IsNullOrEmpty(curSelectedEqParameterID))
{
curSelectedEqParameterName = GetCurSelectedRowData(ParameterDesc);
curSelectedEqParameterAddress = GetCurSelectedRowData(ParameterAddress);
if (!string.IsNullOrEmpty(curSelectedEqParameterName))
{
Global_Parameter.curEqParameterID = curSelectedEqParameterID;
Global_Parameter.curEqParameterDesc = curSelectedEqParameterName;
Global_Parameter.curEqParameterAddress = curSelectedEqParameterAddress;
//打开编辑区域面板
EqParameterUpdate eqParameterUpdate = new EqParameterUpdate();
eqParameterUpdate.Show();
}
else
{
MessageBox.Show("设备参数信息获取失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("当前设备参数ID为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
//删除
if (col==5)
{
if (!string.IsNullOrEmpty(curSelectedEqParameterID))
{
DialogResult dr = MessageBox.Show("你确定删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
//删除信息
bool isSuccess = CommonEqParameterOperation.GetInstance().DeleteEqParameterInfo(curSelectedEqParameterID);
if (isSuccess)
{
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
EverydayLog.Write("删除成功!删除设备参数ID="+ curSelectedEqParameterID);
//刷新数据
RefreshData(curEqParameterType,true);
}
else
{
MessageBox.Show("删除失败!数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
EverydayLog.Write("删除失败!数据库连接失败!");
}
}
}
else
{
MessageBox.Show("当前设备参数ID为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
参考内容:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.datagridviewbuttoncolumn.text?redirectedfrom=MSDN&view=netframework-4.7.2#System_Windows_Forms_DataGridViewButtonColumn_Text