欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WinForm中DataGridView添加,删除,修改操作具体方法

程序员文章站 2024-02-08 22:18:46
1.添加操作,代码如下:复制代码 代码如下:ilist selfrunconfigs = new list();...

1.添加操作,代码如下:

复制代码 代码如下:

ilist<selfrun> selfrunconfigs = new list<selfrun>();
private void btnnewconfig_click(object sender, eventargs e)
{
try
{
string _lampno = updownselflampno.value.tostring();
int _ctrlgpno = convert.toint16(updownctrlgpcnt.value);
string _opway = string.format("{0}", rbconfig.checked == true ? 1 : 0);
string _opctuch = getselectedctuch();
if (checknewconfigislega(_ctrlgpno, _opctuch))
{
string _optype = rbcgopen.checked == true ? "01" : rbcgclose.checked == true ? "00" : "02";
selfrunconfigs.add(new selfrun(_opctuch, _optype, convert.toint32(updowntime.value)));
}
bindgridviewforilist<selfrun>(gcconfigshow, selfrunconfigs);
}
catch (exception ex)
{
messagebox.show(string.format("新增配置失败,原因:{0}", ex.message.trim()));
}
}
private void bindgridviewforilist<t>(datagridview gv, ilist<t> datasource)
{
bindinglist<t> _bindinglist = new bindinglist<t>(datasource);
bindingsource _source = new bindingsource(_bindinglist, null);
gv.datasource = _source;
}

selfrun实体类代码如下:

复制代码 代码如下:

    public struct selfrun
{
public selfrun(string _opctuch, string _opway, int _optime)
: this()
{
opctuch = _opctuch;
opway = _opway;
optime = _optime;
}
public string opctuch
{
get;
set;
}
public string opway { get; set; }
public int optime { get; set; }
}

界面绑定,如图:

WinForm中DataGridView添加,删除,修改操作具体方法

效果如图:

 

WinForm中DataGridView添加,删除,修改操作具体方法

2.修改操作,代码如下:
其实思路很简单,就是点击行的时候,获取行内数据信息,然后填充到控件内,修改后,点击‘修改配置'后即可保存修改。

所以首先设置点击控件的时候,是选择一行,如图:

 

WinForm中DataGridView添加,删除,修改操作具体方法

在cellclick事件中完成,当点击行的时候,将行数据填充到控件内,代码如下:

复制代码 代码如下:

private void gcconfigshow_cellclick(object sender, datagridviewcelleventargs e)
{
if (e.rowindex < 0) return;
datagridview _dgv = (datagridview)sender;
string _opway = _dgv.rows[e.rowindex].cells["opway"].value.tostring();
switch (_opway)
{
case "00":
threadsafeopradiobutton(rbcgclose, true);
break;
case "01":
threadsafeopradiobutton(rbcgopen, true);
break;
case "02":
threadsafeopradiobutton(rbsaveone, true);
break;
}
string _opctuch = _dgv.rows[e.rowindex].cells["opctuch"].value.tostring();
for (int i = 0; i < _opctuch.length; i++)
{
if (i == 0)
threadsafecheckbox(ckch1, _opctuch[i].equals('1'));
if (i == 1)
threadsafecheckbox(ckch2, _opctuch[i].equals('1'));
if (i == 2)
threadsafecheckbox(ckch3, _opctuch[i].equals('1'));
if (i == 3)
threadsafecheckbox(ckch4, _opctuch[i].equals('1'));
if (i == 4)
threadsafecheckbox(ckch5, _opctuch[i].equals('1'));
if (i == 5)
threadsafecheckbox(ckch6, _opctuch[i].equals('1'));
if (i == 6)
threadsafecheckbox(ckch7, _opctuch[i].equals('1'));
if (i == 7)
threadsafecheckbox(ckch8, _opctuch[i].equals('1'));
}
string _optime = _dgv.rows[e.rowindex].cells["optime"].value.tostring();
decimal _time;
if (decimal.tryparse(_optime, out _time))
threadsfeopupdown(updowntime, _time);
}

点击修改按钮内代码如下:

复制代码 代码如下:

private void btnupdateconfig_click(object sender, eventargs e)
{
try
{
if (checkselectedrow())
{
int _rowindex = gcconfigshow.currentcell.rowindex;
int _ctrlgpno = convert.toint16(updownctrlgpcnt.value);
string _opway = string.format("{0}", rbconfig.checked == true ? 1 : 0);
string _opctuch = getselectedctuch();
string _optype = rbcgopen.checked == true ? "01" : rbcgclose.checked == true ? "00" : "02";
selfrun _selfrunbyrowindex = selfrunconfigs[_rowindex];
_selfrunbyrowindex.opctuch = getselectedctuch();
_selfrunbyrowindex.optime = convert.toint32(updowntime.value);
_selfrunbyrowindex.opway = _optype;
selfrunconfigs.removeat(_rowindex);
selfrunconfigs.add(_selfrunbyrowindex);
bindgridviewforilist<selfrun>(gcconfigshow, selfrunconfigs);
}
}
catch (exception ex)
{
messagebox.show(string.format("修改配置失败,原因:{0}", ex.message.trim()));
}
}

3.删除操作,代码如下:

复制代码 代码如下:

private void btndeleteconfig_click(object sender, eventargs e)
{
if (checkselectedrow())
{
if (messagebox.show("是否删除该行数据?", "消息", messageboxbuttons.yesno, messageboxicon.question) == dialogresult.yes)
{
selfrunconfigs.removeat(gcconfigshow.currentcell.rowindex);
bindgridviewforilist<selfrun>(gcconfigshow, selfrunconfigs);
}
}
}