c#中DataGrid的数据新增编辑删除操作代码
2024-07-21 02:23:19
供稿:网友
private void deletedevdata()
??{
???int deletenum=0;
???//删除选中的某一行或多行记录,调用datagrid的isselected(i)函数,如果函数返回结果为true,则表示该行被选中
???arraylist arrlist = new arraylist();
???for(int i=0;i???{???
????if(dtg_basicdatainfo.isselected(i)==true)
????{?
?????f_wr_public.vo_devspec vo_devspec_delete = new f_wr_public.vo_devspec();
?????//表中第11列保存的是设备类型的主键,但没有显示在表中。
?????vo_devspec_delete.devspecid = int64.parse(devspec.rows[i][10].tostring());
?????arrlist.add(vo_devspec_delete);
?????deletenum++;
????}
???}
???if (deletenum !=0)
???{
????if (messagebox.show ("你确定要删除这些数据吗?", "删除数据",
?????messageboxbuttons.yesno, messageboxicon.question)== dialogresult.yes)
????{
?????f_wr_public.f_wr_public f_wr_public = new f_wr_public.f_wr_public();?????
?????f_wr_public.vo_devspec[] arrvo_devspec_delete = new f_wr_public.vo_devspec[deletenum];
?????ienumerator ienum = arrlist.getenumerator();
?????int deleteindex=0;
?????while(ienum.movenext())
?????{
??????arrvo_devspec_delete[deleteindex] = (f_wr_public.vo_devspec)ienum.current;
??????arrvo_devspec_delete[deleteindex].col=int64.maxvalue;
??????arrvo_devspec_delete[deleteindex].devtypeid=int64.maxvalue;
??????arrvo_devspec_delete[deleteindex].num=int64.maxvalue;
??????arrvo_devspec_delete[deleteindex].row=int64.maxvalue;
??????deleteindex++;
?????}
?????f_wr_public.removedevspec(arrvo_devspec_delete);
?????messagebox.show ("删除成功!", "删除数据",messageboxbuttons.ok, messageboxicon.information);
????}
????else
????{
?????return;
????}
???}
???else
???{
????messagebox.show ("未选中所要删除的数据!", "删除数据", messageboxbuttons.ok, messageboxicon.warning);
???}
??}
?
private void querydevspec()
??{
???try
???{
????devspec.clear();
????f_wr_public.f_wr_public f_wr_public = new f_wr_public.f_wr_public();
????f_wr_public.vo_devspec vo_devspec = new f_wr_public.vo_devspec();
????vo_devspec.col=int64.maxvalue;
????vo_devspec.devspecid=int64.maxvalue;
????vo_devspec.devtypeid=int64.parse(cbo_devtype.selectedvalue.tostring());
????vo_devspec.num=int64.maxvalue;
????vo_devspec.row=int64.maxvalue;
????f_wr_public.vo_devspec[] arrvo_devspec=f_wr_public.querydevspec(vo_devspec);
????if(arrvo_devspec!=null)
????{
?????for(int i=0;i?????{
??????f_wr_public.vo_devspec vo_devspec_result = arrvo_devspec[i];
??????datarow myrow;
??????myrow = devspec.newrow();
??????myrow["设备大类"] =dic_devspec[vo_devspec_result.devtypeid].displaymember.tostring();
??????myrow["规格类型"] = vo_devspec_result.spectype;
??????myrow["生产厂家"] = vo_devspec_result.manufacturer;
??????myrow["长"] = [email protected];
??????myrow["宽"] = vo_devspec_result.width;
??????myrow["高"] = vo_devspec_result.high;
??????if ( vo_devspec_result.num != int64.maxvalue)
??????{
???????myrow["数量"] = vo_devspec_result.num;
??????}
??????else
???????myrow["数量"] ="";
??????if ( vo_devspec_result.row != int64.maxvalue)
??????{
???????myrow["行"] = vo_devspec_result.row;
??????}
??????else
???????myrow["行"] = "";
??????if ( vo_devspec_result.col != int64.maxvalue)
??????{
???????myrow["列"] = vo_devspec_result.col;
??????}
??????else
???????myrow["列"] = "";;
??????
??????myrow["排列方式"] = vo_devspec_result.disposetype;
??????myrow["设备型号"] = vo_devspec_result.devspecid;
??????devspec.rows.add(myrow);
?????}
?????devspec.acceptchanges();
?????//设置dtg_basicdatainfo的datamember为devspec
?????dtg_basicdatainfo.datamember = devspec.tostring();
?????dtg_basicdatainfo.datasource=dataset1;
?????devtablestyle.mappingname="devspec";
?????rowcounts=arrvo_devspec.length;
????}
????else
????{
?????rowcounts=0;
????}
????dtg_basicdatainfo.readonly = true;
???}
???catch(exception ee)
???{
????
????messagebox.show (ee.tostring(), "error",
?????messageboxbuttons.ok, messageboxicon.information);
???}
??}
?
?private void savedevdata()
??? {
???try
???{
????f_wr_public.f_wr_public f_wr_public = new f_wr_public.f_wr_public();
????//如果某行的状态为added,则表示该行的数据是新增加的
????datarow[] datarowadd = devspec.select(null,null,dataviewrowstate.added);
????int mcount= datarowadd.length;
????if (mcount != 0)
????{
?????f_wr_public.vo_devspec[] arrvo_devspec_add = new f_wr_public.vo_devspec[mcount];
?????for(int i=0;i?????{
??????f_wr_public.vo_devspec vo_devspec = new f_wr_public.vo_devspec();
??????vo_devspec.col=int64.maxvalue;
??????vo_devspec.num=int64.maxvalue;
??????vo_devspec.row=int64.maxvalue;
??????vo_devspec.devtypeid =int64.parse(cbo_devtype.selectedvalue.tostring());
??????vo_devspec.spectype=datarowadd[i][1].tostring();
??????vo_devspec.manufacturer=datarowadd[i][2].tostring();
[email protected]=datarowadd[i][3].tostring();
??????vo_devspec.width=datarowadd[i][4].tostring();
??????vo_devspec.high=datarowadd[i][5].tostring();
??????if(!datarowadd[i][6].tostring().equals(""))
??????{
???????vo_devspec.num= int64.parse(datarowadd[i][6].tostring());
??????}
??????if(!datarowadd[i][7].tostring().equals(""))
??????{
???????vo_devspec.row= int64.parse(datarowadd[i][7].tostring());
??????}
??????if(!datarowadd[i][8].tostring().equals(""))
??????{
???????vo_devspec.col= int64.parse(datarowadd[i][8].tostring());
??????}
??????vo_devspec.disposetype=datarowadd[i][9].tostring();
??????vo_devspec.devspecid=int64.maxvalue;
????????
????????
??????arrvo_devspec_add[i] = vo_devspec;
?????}
?????f_wr_public.adddevspec(arrvo_devspec_add);
?????messagebox.show ("增加成功!", "增加数据",messageboxbuttons.ok, messageboxicon.information);
????}
????//如果某行的状态为modifiedcurrent,则表示该行的数据被修改过
????datarow[] datarowmodify = devspec.select(null,null,dataviewrowstate.modifiedcurrent);
????int intdatarowmodify = datarowmodify.length;
????if (intdatarowmodify != 0)
????{
?????f_wr_public.vo_devspec[] arrvo_devspec_modify = new f_wr_public.vo_devspec[intdatarowmodify];
?????for(int i=0;i?????{
??????f_wr_public.vo_devspec vo_devspec = new f_wr_public.vo_devspec();
??????//表中第11列保存的是设备类型的主键,但没有显示在表中。
??????vo_devspec.devspecid= int64.parse(datarowmodify[i][10].tostring());
??????//
??????vo_devspec.col=int64.maxvalue;
??????vo_devspec.num=int64.maxvalue;
??????vo_devspec.row=int64.maxvalue;
??????vo_devspec.spectype=datarowmodify[i][1].tostring();
??????vo_devspec.manufacturer=datarowmodify[i][2].tostring();
[email protected]=datarowmodify[i][3].tostring();
??????vo_devspec.width=datarowmodify[i][4].tostring();
??????vo_devspec.high=datarowmodify[i][5].tostring();
??????if(!datarowmodify[i][6].tostring().equals(""))
??????{
???????vo_devspec.num= int64.parse(datarowmodify[i][6].tostring());
??????}
??????if(!datarowmodify[i][7].tostring().equals(""))
??????{
???????vo_devspec.row= int64.parse(datarowmodify[i][7].tostring());
??????}
??????if(!datarowmodify[i][8].tostring().equals(""))
??????{
???????vo_devspec.col= int64.parse(datarowmodify[i][8].tostring());
??????}
??????vo_devspec.disposetype=datarowmodify[i][9].tostring();
??????arrvo_devspec_modify[i] = vo_devspec;
?????}
???????
?????f_wr_public.modifydevspec(arrvo_devspec_modify);
?????messagebox.show ("保存成功!", "保存数据",messageboxbuttons.ok, messageboxicon.information);
????}
????devspec.acceptchanges();
???}
???catch(exception ee)
???{
????messagebox.show (ee.tostring(), "error",messageboxbuttons.ok, messageboxicon.information);
???}
??? }
?
?
private void queryifsavedevdata()
??{
???/*该函数查询dev表中是否有未保存的数据,如果有,则询问是否保存,
??? * 如果用户回答需要保存,则调用savedevdata过程保存数据,如果回答否,
??? * 则调用查询过程querydevspec刷新页面*/
???datarow[] datarowadd1 = devspec.select(null,null,dataviewrowstate.added);
???int maddcount1= datarowadd1.length;
???datarow[] datarowmodify1 = devspec.select(null,null,dataviewrowstate.modifiedcurrent);
???int mmodifycount1=datarowmodify1.length;
???if (maddcount1!=0 || mmodifycount1 !=0)
???{
????if (messagebox.show ("有编辑过的数据未保存,需要保存吗?", "提示",
?????messageboxbuttons.yesno, messageboxicon.question)== dialogresult.yes)
?????savedevdata();
????else
?????querydevspec();
???}
??}
?