datagridview数据处理方法修改删除添加下拉类表_第1页
datagridview数据处理方法修改删除添加下拉类表_第2页
datagridview数据处理方法修改删除添加下拉类表_第3页
datagridview数据处理方法修改删除添加下拉类表_第4页
datagridview数据处理方法修改删除添加下拉类表_第5页
免费预览已结束,剩余19页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Datagridview的三种处理数据方法一、第一种方法常规方法,在窗口界面上放入一个datagridview,在放各个textbox,然后通过选取对应的记录,修改textbox的值,所有的操作都在一个界面上进行,没什么多说的,大部分方法都这么做、弹出窗口方式此方式,通过双击记录,或者是利用按钮操作,倾向于用按钮方式,一次修改或添加、删除一条记录。利用窗口传值方式,实现数据输入、输出,datagridview的显示跟新。特点:1。父子窗口之间的双向传值,很有参考意义2.父子窗体监combox绑定数据表条件下,双向传值,很多资料接收的都不是很清晰,主要是利用了combox.findstring(

2、)这个方法,传递回index,利用index得到value,好绕啊,废了很大劲。3 .datagridview修改、添加数据下,不用重新访问数据库,而是直接显示修改的结果,这样感觉反应速度快,很有意义。具体如下修改界面System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;/表格自适应宽度/DataCondatacon=newDataCon();/加载数据usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usi

3、ngSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingWDZ.CommonClass;namespaceWDZ三重若t”|hOK.舛户得过造乱时*Irn-WainJDataOperatedataoperate=newDataOperate();stringstrSql=select*fromsql3DataSetds=dataoperate.getDs(strSql,sql3);dataGridViewl.DataSource=d

4、s.Tables0;/设置datagridview属性dataGridView1.Columns0.ReadOnly=true;/列不可编写dataGridView1.SelectionMode=DataGridViewSelectionMode.FullRowSelect;/选择全行/dataGridView1.ReadOnly=true;/只读dataGridView1.DefaultCellStyle.SelectionBackC010r=Color.YellowGreen;/选择的行为颜色/最后一行的星号行不显示,不允许用户添加数据dataGridView1.AllowUserToAd

5、dRows=false;/*/加载下拉列表框BindSex();/绑定性别下拉列表框cmbTemp.Visible=false;/设置下拉列表框不可见/添加下拉列表框事件cmbTemp.SelectedIndexChanged+=newEventHandler(cmb_Temp_SelectedIndexChanged);/将下拉列表框加入到DataGridView控件中this.dataGridView1.Controls.Add(cmb_Temp);/*/datagridview中下拉类表框的绑定privateComboBox:mb_Temp=newComboBoX;/绑定性别下拉列表框/

6、privatevoidBindSex()DataTabledtSex=newDataTable();DataOperatedataoperate=newDataOperate();stringstrSql=select*fromuserclass;DataSetds=dataoperate.getDs(strSql,user_class);dtSex=ds.Tables0;cmbTemp.ValueMember=userclassid;/设置隐含的形式显示数值,起对应一定的名称/cmbTemp.ValueMember=userclass;cmb_Temp.DisplayMember=user_

7、class;cmb_Temp.DataSource=dtSex;cmb_Temp.DropDownStyle=ComboBoxStyle.DropDownList;/只能选择,不能写入/cmbTemp.DropDownStyle=ComboBoxStyle.DropDown;/可以写入/cmb_Temp.DropDownStyle=ComboBoxStyle.Simple;/当用户选择下拉列表框时改变DataGridView单元格的内容privatevoidcmbTempSelectedIndexChanged(objectsender,EventArgse)if(dataGridView1.

8、CurrentCell!=null)DataRowViewmyrowview=(DataRowVieWcmbTemp.SelectedItem;/dataGridView1.CurrentCell.Value=cmb_Temp.SelectedValue.ToString();dataGridView1.CurrentCell.Value=myrowview2.ToString();/选取的文本dataGridView1.CurrentCell.Tag=cmb_Temp.SelectedValue;/选取的编码/MessageBox.Show(text:+cmbTemp.SelectedTex

9、t+-item:+cmbTemp.SelectedItem.ToString()+-value:+cmb_Temp.SelectedValue.ToString()+-index:+cmb_Temp.SelectedIndex.ToString();/DataRowViewmyrowview=(DataRowView)cmbTemp.SelectedItem;/MessageBox.Show(myrowview0.ToString()+-+myrowview1.ToString()+-+myrowview2.ToString();/获得对应的value和texttry(if(dataGridV

10、iewl.CurrentCell.ColumnIndex=3)(Rectanglerect=dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex,dataGridView1.CurrentCell.RowIndex,false);stringsexValue=dataGridView1.CurrentCell.Value.ToString();MessageBox.Show(sexValue);cmb_Temp.Text=sexValue;cmb_Temp.Left=rect.Left;cmb_T

11、emp.Top=rect.Top;cmbTemp.Width=rect.Width;cmb_Temp.Height=rect.Height;cmbTemp.Visible=true;)else(cmb_Temp.Visible=false;)catch()privatevoiddataGridView1CellDoubleClick(objectsender,DataGridViewCellEventArgse)=1if(e.ColumnIndex=0)DataGridViewdgv=senderasDataGridView;if(dgv.Rowse.RowIndex.Cells1.Value

12、!=null)stringid=dgv.Rowse.RowIndex.Cells0.Value.ToString();stringuser_id=dgv.Rowse.RowIndex.Cells1.Value.ToString();stringuserpassword=dgv.Rowse.RowIndex.Cells2.Value.ToString();stringuser_class=dgv.Rowse.RowIndex.Cells3.Value.ToString();stringuser_name=dgv.Rowse.RowIndex.Cells4.Value.ToString();str

13、inguser_sex=dgv.Rowse.RowIndex.Cells5.Value.ToString();stringoutUserid,outUserpassword,outUserclass,outUsername,outUsersex;DialogResultmyresult;this.ShowDialogForm(id,userid,userpassword,userclass,username,user_sex,outoutUserid,outoutUserpassword,outoutUserclass,outoutUsername,outoutUsersex,outmyres

14、ult);if(myresult=DialogResult.OK)dgv.Rowse.RowIndex.Cells1.Value=outUserid;dgv.Rowse.RowIndex.Cells2.Value=outUserpassword;dgv.Rowse.RowIndex.Cells3.Value=outUserclass;dgv.Rowse.RowIndex.Cells4.Value=outUsername;dgv.Rowse.RowIndex.Cells5.Value=outUsersex;privatevoidShowDialogForm(stringid,stringuser

15、id,stringuserpassword,stringuser_class,stringuser_name,stringuser_sex,outstringoutUserid,outstringoutUserpassword,outstringoutUserclass,outstringoutUsername,outstringoutUsersex,outDialogResultmyresult)dialog2frm=newdialog2();/读取输入参数,显示在子窗口中frm.ControlsIDTB.Text=id;frm.ControlsUSERIDTB.Text=userid;fr

16、m.ControlsUSER_PWD_TBText=user_password;frm.ControlsUSER_CLASS_TBText=user_class;/frm.USER_CLASS_CB.SelectedValue=2;frm.ControlsUSERNAMETBText=username;frm.ControlsUSER_SEX_T:Text=user_sex;frm.ShowDialog();/显示子窗口/给输出参数赋值默认值outUserid=user_id;outUserpassword=userpassword;outUserclass=user_class;outUse

17、rname=username;outUsersex=user_sex;/判断是否进行修改数据myresult=frm.DialogResult;if(myresult=DialogResult.OK)/outUserid=frm.ControlsID_TB.Text;outUserid=frm.ControlsUSER_ID_TB.Text;outUserpassword=frm.ControlsUSER_PWD_TBText;outUserclass=frm.ControlsUSERCLASSCBText;outUsername=frm.ControlsUSERNAMETBText;outU

18、sersex=frm.ControlsUSER_SEX_TI|Text;MessageBoxShow(修改了数据);frm.Dispose();MessageBoxShow(请先选择一条记录数据!);return)采用弹出窗口方式,进行数据修改DataGridViewdgv=dataGridViewl;if(dgv.Rowsselectrowindex.Cells1.Value!=null)(/输入参数stringid=dgv.Rowsselectrowindex.Cells0.Value.ToString();stringuser_id=dgv.Rowsselectrowindex.Cell

19、s1.Value.ToString();stringuserpassword=dgv.Rowsselectrowindex.Cells2.Value.ToString();stringuser_class=dgv.Rowsselectrowindex.Cells3.Value.ToString();stringuser_name=dgv.Rowsselectrowindex.Cells4.Value.ToString();stringusersex=dgv.Rowsselectrowindex.Cells5.Value.ToString();stringoutUserid,outUserpas

20、sword,outUserclass,outUsername,outUsersex;/输出参数DialogResultmyresult;/返回弹出窗口关闭状态this.ShowDialogForm(id,userid,userpassword,userclass,username,user_sex,outoutUserid,outoutUserpassword,outoutUserclass,outoutUsername,outoutUsersex,outmyresult);/如 果 修 改 了 数 据 , 把 当 前datagridview内 容 的 对 应 值 进 行 修 改if(myre

21、sult=DialogResult.OK)(dgv.Rowsselectrowindex.Cells1.Value=outUserid;dgv.Rowsselectrowindex.Cells2.Value=outUserpassword;dgv.Rowsselectrowindex.Cells3.Value=outUserclass;dgv.Rowsselectrowindex.Cells4.Value=outUsername;dgv.Rowsselectrowindex.Cells5.Value=outUsersex;)privatevoiddataGridView1CellClick(o

22、bjectsender,DataGridViewCellEventArgse)selectrowindex=e.RowIndex;)/添加记录privatevoidbutton1_Click(objectsender,EventArgse)(/采用弹出窗口方式,进行数据修改DataGridViewdgv=dataGridView1;/输入参数stringid=-1stringuser_id=nullstringuser_password=null;stringuser_class=null;stringuser_name=null;stringuser_sex=nullstringoutUse

23、rid,outUserpassword,outUserclass,outUsername,outUsersex;/输出参数DialogResultmyresult;/返回弹出窗口关闭状态this.ShowDialogForm(id,user_id,user_password,user_class,user_name,user_sex,outoutUserid,outoutUserpassword,outoutUserclass,outoutUsername,outoutUsersex,outmyresult);/如果添加了数据,把当前datagridview内容的对应值进行修改if(myres

24、ult=DialogResult.OK)(/添加行数据(DataTable)dataGridView1.DataSource).Rows.Add(1,outUserid,outUserpassword,outUserclass,outUsername,outUsersex);/dataGridView1是不能直接添加行的,只能在他的数据源里添,他会自动绑定/获取当前选择的行坐标/)弹出窗口代码usingSystem;stringmysql=deletefromuser_tb1whereID=+myid.ToString();DataOperatedataoperate=newDataOpera

25、te();dataoperate.getCom(mysql);MessageBoxShow(删除数据成功);/加载数据mysql=select*fromsql3;DataSetds=dataoperate.getDs(mysql,sql3);dataGridViewl.DataSource=ds.Tables0;/*/获取总页码SELECTCount(id)ASnumberlFROMuser_tb1;mysql=SELECTCount(id)ASnumberlFROMusertb1;DataSetds=dataoperate.getDs(mysql,usertb1);intindexsum=i

26、nt.Parse(ds.Tables0.Rows00.ToString();if(indexsum%pagesize=0)label5.Text=(indexsum/pagesize).ToString();else|label5.Text=(indexsum/pagesize+1).ToString();pagesum=int.Parse(label5.Text);/显示当前页面)usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem

27、.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingWDZ.CommonClass;namespaceWDZpublicpartialclassdialog2:Formpublicdialog2()InitializeComponent();privatevoiddialog2Load(objectsender,EventArgse)stringmysql=select*fromuserclassorderbyid;bindcombox(USER_CLASS_CB,mysql,use

28、r_class);USERCLASSCB.SelectedIndex=USERCLASSCB.FindString(USERCLASS_TB.Text,-1);/*非常重要,利用string查找到对应的index,然后就可以显示了,或者取valueDataOperatedataoperate=newDataOperate();DataSetds=dataoperate.getDs(mysql,tbname);mydt=ds.Tables0;mycombox.ValueMember=mydt.Columns1.ColumnName;起对应一定的名称/cmbTemp.ValueMember=use

29、rclass;mycombox.DisplayMember=mydt.Columns2.ColumnName;mycombox.DataSource=mydt;ComboBoxStyle.DropDownList;/只能选择,不能写入/设置隐含的形式显示数值,mycombox.DropDownStyle=/cmbTemp.DropDownStyle=ComboBoxStyle.DropDown;/可以写入/cmb_Temp.DropDownStyle=ComboBoxStyle.Simple;privatevoidbutton1_Click(objectsender,EventArgse)/提

30、交数据库操作,。修改数据DataOperatemydopt=newDataOperate();stringmysql=;修改数据if(ID_TB.Text!=-1)mysql=updateusertb1setuserid=+USERIDTB.Text+,userpassword=+USERPWDTB.Text+,user_class_id=+USER_CLASS_CB.SelectedValuemysql=insertintousertb1(user_id,user_password,user_class_id,user_name,user_sex)values(+USERPWDTB.Text

31、+USER_CLASS_CB.SelectedValue+USER_SEX_TB.Text+工/MessageBox.Show(mysql);mydopt.getCom(mysql);this.DialogResult=DialogResult.OK;privatevoidbutton2_Click(objectsender,EventArgse)1this.DialogResult=DialogResult.Cancel;直接在datagridview上修改、添加、删除。不需要添加textbox。而且在view+,user_name=+USER_NAME_TB.Te)+,user_sex=+

32、USER_SEX_TB.Text+whereid=+ID_TB.Text;+USERIDTB.Text+IIIIII+USERNAMETB.Text+IIIIIIelseif(ID_TB.Text=-1)中有combox,下拉类表,可以一次修改多行数据,很有意义。特点:1.view中含有combox,利用了datarowview获得选取的value,index,item都可以了2.一次可以修改多彳T数据,利用dataapter的update的特性,比较麻烦3.可以直接在view中添加数据,同样利用update特性。问题这种方法,只能直接操作绑定的表,如果绑定的是视图,现在还没想好怎么解决。应该

33、可以解决,需要记录下所有修改的记录的rowindex,然后利用sql语句分别进行修改表,感觉比较麻烦,放弃了。代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingWDZ.CommonClass;usingSystem.Data.OleDb;

34、namespaceWDZpublicpartialclassfrmMain:FormpublicfrmMain()IDuferJZIHrsevjBTTrariUEE.5fli12aaxn:SFL3同 c?OOKCCtt5SS7算cnrwrrtcFEB5B1的|JWX二LTwhLQannca:eaLTsIQillT221ZflOKOCL 二T-S-2253ainoQ:H立1fanxcr飞:唁枢由汨:牛哥当朝止于J11*-甲 1 区式产iiL?:rInitializeComponent();)privatevoidfrmMain_Load(objectsender,EventArgse)(this

35、.dataGridView1.AutoSizeColumnsMode=System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;/表格自适应宽度/DataCondatacon=newDataCon();DataOperatedataoperate=newDataOperate();stringstrSql=select*fromusertb1;DataSetds=dataoperate.getDs(strSql,user_tb1);dataGridViewl.DataSource=ds.Tables0;dataGridView1.Col

36、umns0.ReadOnly=true;/列不可编写dataGridViewl.SelectionMode=DataGridViewSelectionMode.FullRowSelect;/选择全行/dataGridViewl.ReadOnly=true;/只读dataGridViewl.DefaultCellStyle.SelectionBackC010r=Color.YellowGreen;/选择的行为颜色/最后一行的星号行不显示,不允许用户添加数据dataGridViewl.AllowUserToAddRows=false;button6.Enabled=false;button7.En

37、abled=false;/初始化索引数组for(inti=0;i20;i+)intindexi=255;/*/加载下拉列表框BindSex();/绑定性别下拉列表框cmbTemp.Visible=false;/设置下拉列表框不可见/添加下拉列表框事件cmbTemp.SelectedIndexChanged+=newEventHandler(cmb_Temp_SelectedIndexChanged);/将下拉列表框加入到DataGridView控件中this.dataGridView1.Controls.Add(cmb_Temp);privatevoidbutton1_Click(object

38、sender,EventArgse)(stringmsg=String.Format(第0行,第1歹1J,值:2,dataGridView1.CurrentCell.RowIndex,dataGridView1.CurrentCell.ColumnIndex,dataGridView1.CurrentCell.Value);label1.Text=选择的单元格为:+msg;System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;/表格自适应宽度/DataCondatacon=newDataCon();DataOperatedataop

39、erate=newDataOperate();pagesize=int.Parse(textBox1.Text);/每页显示的记录数stringstrSql=selecttop+pagesize.ToString()+*fromusertb1DataSetds=dataoperate.getDs(strSql,user_tb1);dataGridView1.DataSource=ds.Tables0;dataGridView1.Columns0.ReadOnly=/获取第1页页码label2.Text=1;pagenumber=1;/获取总页码SELECTCount(id)ASnumber1F

40、ROMusertb1;strSql=SELECTCount(id)ASnumber1FROMuser_tb1ds=dataoperate.getDs(strSql,usertb1);intindexsum=int.Parse(ds.Tables0.Rows00.ToString();if(indexsum%pagesize=0)label5.Text=(indexsum/pagesize).ToString();elselabel5.Text=(indexsum/pagesize+1).ToString();true;/列不可编写this.dataGridView1.AutoSizeColum

41、nsMode=pagesum=int.Parse(label5.Text);/设置上一页下一页按钮的可用性if(pagesum=1)button7.Enabled=falseelsebutton7.Enabled=true;button6.Enabled=true;intintindex=newint20;/假设一张表上默认数据为intselectindex=0;OleDbConnectionconn;OleDbDataAdapteradapter;privateDataTabledbconn(stringstrSql)DataConmyconn=newDataCon();conn=mycon

42、n.getCon();this.adapter=newOleDbDataAdapter(strSql,conn);DataTabledtSelect=newDataTable();intrnt=this.adapter.Fill(dtSelect);returndtSelect;privateBooleandbUpdate()stringstrSql=select*fromusertb1;DataTabledtUpdate=newDataTable();dtUpdate=dbconn(strSql);dtUpdate.Rows.Clear();DataTabledtShow=newDataTa

43、ble();dtShow=(DataTable)this.dataGridViewl.DataSource;/dtUpdate.ImportRow(dtShow.Rowsintindex);/只能够插入最后修改的一行数据。for(inti=0;i20;i+)if(intindexi!=255)dtUpdate.ImportRow(dtShow.Rowsintindexi);)二20个如果不进行循环多行插入的话,try/SqlCommandBuilderCommandBuilder;OleDbCommandBuilderCommandBuilder;CommandBuilder=newOleDb

44、CommandBuilder(this.adapter);this.adapter.Update(dtUpdate);)catch(Exceptionex)MessageBoxShow(ex.Message.ToString();returnfalse;)dtUpdate.AcceptChanges();returntrue;)privatevoidbutton4Click(objectsender,EventArgse)if(dbUpdate()1MessageBoxShow(修改成功!);)for(inti=0;i(selectmax(id)from(selecttop+pageindex

45、.ToString()+idfromusertb1orderbyid);pageindex=(pagenumber-1)*pagesize;strSql=SELECTTOP+pagesize.ToString()+*FROMuser_tb1WHEREid(selectmax(id)from(selecttop+pageindex.ToString()+idfromusertb1orderbyid)elsestrSql=selecttop+pagesize.ToString()+*fromuser_tb1/MessageBox.Show(strSql);/调试使用DataSetds=dataop

46、erate.getDs(strSql,user_tb1);dataGridView1.DataSource=ds.Tables0;dataGridView1.Columns0.ReadOnly=true;/列不可编写/显示当前的页数if(pagenumber0)npageindex=(pagenumber-2)*pagesize;/已经显示的条目数strSql=SELECTTOP+pagesize.ToString()+*FROMuser_tb1WHEREid(selectmax(id)from(selecttop+pageindex.ToString()+idfromusertb1order

47、byid);)elsestrSql=selecttop+pagesize.ToString()+*fromuser_tb1DataSetds=dataoperate.getDs(strSql,dataGridViewl.DataSource=ds.Tables0;dataGridView1.Columns0.ReadOnly=/显示当前的页数if(pagenumber!=1)pagenumber=pagenumber-1;elsepagenumber=1;label2.Text=pagenumber.ToString();|privatevoidtextBox1_TextChanged(obj

48、ectsender,EventArgse)1pagesize=int.Parse(textBox1.Text);privatevoidbutton5_Click(objectsender,EventArgse)/取消按钮,其实就是从新读本页数据库,在显示一次DataOperatedataoperate=newDataOperate();intpageindex=0;/已经显示的条目数stringstrSql;if(pagenumber-1)0)Ipageindex=(pagenumber-1)*pagesize;/已经显示的条目数strSql=SELECTTOP+pagesize.ToStri

49、ng()+*FROMuser_tb1WHEREid(selectmax(id)from(selecttop+pageindex.ToString()+idfromusertb1orderbyid);elsestrSql=selecttop+pagesize.ToString()+user_tb1);true;/列不可编写*fromusertb1DataSetds=dataoperate.getDs(strSql,user_tb1);dataGridViewl.DataSource=ds.Tables0;dataGridView1.Columns0.ReadOnly=true;/列不可编写pri

50、vatevoidbutton8Click(objectsender,EventArgse)/显不到最后一页DataOperatedataoperate=newDataOperate();pagenumber=pagesum;button7_Click(sender,e);/添加行数据(DataTable)dataGridView1.DataSource).Rows.Add(1,000000);/就这一句就可以了,兄弟!也就是说dataGridView1是不能直接添加行的,只能在他的数据源里添,他会自动绑定privatevoidbutton9Click(objectsender,EventArg

51、se)intmyid=int.Parse(dataGridView1.Rowsselectindex.Cells0.Value.ToString();stringmysql=deletefromuser_tb1whereID=+myid.ToString();DataOperatedataoperate=newDataOperate();dataoperate.getCom(mysql);MessageBoxShow(删除数据成功);/获取总页码SELECTCount(id)ASnumber1FROMusertb1;mysql=SELECTCount(id)ASnumber1FROMuser_tb1;DataSetds=dataoperate.getDs(mysql,usertb1);intindexsum=int.Parse(ds.Tables0.Rows00.ToString();if(indexsum%pagesize=0)label5.Text=(indexsum/page

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论