datagridview-数据处理方法-修改-删除-添加-下拉类表

上传人:dus****log 文档编号:64023050 上传时间:2022-03-21 格式:DOCX 页数:18 大小:31.50KB
返回 下载 相关 举报
datagridview-数据处理方法-修改-删除-添加-下拉类表_第1页
第1页 / 共18页
datagridview-数据处理方法-修改-删除-添加-下拉类表_第2页
第2页 / 共18页
datagridview-数据处理方法-修改-删除-添加-下拉类表_第3页
第3页 / 共18页
点击查看更多>>
资源描述
Datagridview的三种处理数据方法一、 第一种方法常规方法,在窗口界面上放入一个datagridview,在放各个textbox,然后通过选取对应的记录,修改textbox的值,所有的操作都在一个界面上进行,没什么多说的,大局部方法都这么做二、 弹出窗口方式此方式,通过双击记录,或者是利用按钮操作,倾向于用按钮方式,一次修改或添加、删除一条记录。利用窗口传值方式,实现数据输入、输出,datagridview的显示跟新。特点:1。父子窗口之间的双向传值,很有参考意义2.父子窗体监combox绑定数据表条件下,双向传值,很多资料接收的都不是很清晰,主要是利用了combox.findstring()这个方法,传递回index,利用index得到value,好绕啊,废了很大劲。3.datagridview修改、添加数据下,不用重新访问数据库,而是直接显示修改的结果,这样感觉反响速度快,很有意义。具体如下修改界面添加界面主窗口代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using WDZ.CommonClass;namespace WDZpublicpartialclassfrmMain2 : Form public frmMain2() InitializeComponent(); privatevoid frmMain2_Load(object sender, EventArgs e) this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; /表格自适应宽度/DataCon datacon = new DataCon();/加载数据DataOperate dataoperate = newDataOperate();string strSql = select * from sql3 ;DataSet ds = dataoperate.getDs(strSql, sql3); dataGridView1.DataSource = ds.Tables0;/设置datagridview属性 dataGridView1.Columns0.ReadOnly = true; /列不可编写 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; /选择全行/dataGridView1.ReadOnly = true; /只读 dataGridView1.DefaultCellStyle.SelectionBackColor = Color.YellowGreen; /选择的行为颜色/最后一行的星号行不显示,不允许用户添加数据 dataGridView1.AllowUserToAddRows = false;/*/加载下拉列表框 BindSex(); / 绑定性别下拉列表框 cmb_Temp.Visible = false; / 设置下拉列表框不可见/ 添加下拉列表框事件 cmb_Temp.SelectedIndexChanged += newEventHandler(cmb_Temp_SelectedIndexChanged);/ 将下拉列表框参加到DataGridView控件中this.dataGridView1.Controls.Add(cmb_Temp); /*/datagridview中下拉类表框的绑定privateComboBox cmb_Temp = newComboBox();/绑定性别下拉列表框/privatevoid BindSex() DataTable dtSex = newDataTable();DataOperate dataoperate = newDataOperate();string strSql = select * from user_class ;DataSet ds = dataoperate.getDs(strSql, user_class); dtSex = ds.Tables0; cmb_Temp.ValueMember =user_class_id; /设置隐含的形式显示数值,起对应一定的名称/cmb_Temp.ValueMember = user_class; cmb_Temp.DisplayMember = user_class; cmb_Temp.DataSource = dtSex; cmb_Temp.DropDownStyle =ComboBoxStyle.DropDownList; /只能选择,不能写入/cmb_Temp.DropDownStyle =ComboBoxStyle.DropDown; /可以写入/ cmb_Temp.DropDownStyle = ComboBoxStyle.Simple; / 当用户选择下拉列表框时改变DataGridView单元格的内容privatevoid cmb_Temp_SelectedIndexChanged(object sender, EventArgs e) if (dataGridView1.CurrentCell != null) DataRowView myrowview = (DataRowView)cmb_Temp.SelectedItem;/dataGridView1.CurrentCell.Value = cmb_Temp.SelectedValue.ToString(); dataGridView1.CurrentCell.Value = myrowview2.ToString();/选取的文本 dataGridView1.CurrentCell.Tag = cmb_Temp.SelectedValue; /选取的编码/ MessageBox.Show(text: + cmb_Temp.SelectedText + -item: + cmb_Temp.SelectedItem.ToString() + -value: + cmb_Temp.SelectedValue.ToString()+-index:+cmb_Temp.SelectedIndex.ToString();/ DataRowView myrowview = (DataRowView)cmb_Temp.SelectedItem;/ MessageBox.Show(myrowview0.ToString()+-+myrowview1.ToString()+-+myrowview2.ToString(); /获得对应的value和text privatevoid dataGridView1_CurrentCellChanged_1(object sender, EventArgs e) / 当用户移动到性别这一列时单元格显示下拉列表框try if (dataGridView1.CurrentCell.ColumnIndex = 3) Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);string sexValue = dataGridView1.CurrentCell.Value.ToString();/MessageBox.Show(sexValue); cmb_Temp.Text = sexValue; cmb_Temp.Left = rect.Left; cmb_Temp.Top = rect.Top; cmb_Temp.Width = rect.Width; cmb_Temp.Height = rect.Height; cmb_Temp.Visible = true; else cmb_Temp.Visible = false; catch privatevoid dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) if (e.ColumnIndex = 0) DataGridView dgv = sender asDataGridView;if (dgv.Rowse.RowIndex.Cells1.Value != null) string id = dgv.Rowse.RowIndex.Cells0.Value.ToString();string user_id = dgv.Rowse.RowIndex.Cells1.Value.ToString();string user_password= dgv.Rowse.RowIndex.Cells2.Value.ToString();string user_class = dgv.Rowse.RowIndex.Cells3.Value.ToString();string user_name = dgv.Rowse.RowIndex.Cells4.Value.ToString();string user_sex = dgv.Rowse.RowIndex.Cells5.Value.ToString();string outUserid, outUserpassword,outUserclass,outUsername,outUsersex;DialogResult myresult;this.ShowDialogForm(id, user_id, user_password, user_class, user_name, user_sex,out outUserid, out outUserpassword, out outUserclass, out outUsername, out outUsersex, out myresult);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; privatevoid ShowDialogForm(string id, string user_id, string user_password, string user_class, string user_name, string user_sex,outstring outUserid, outstring outUserpassword, outstring outUserclass, outstring outUsername, outstring outUsersex, outDialogResult myresult) dialog2 frm = newdialog2();/读取输入参数,显示在子窗口中 frm.ControlsID_TB.Text = id; frm.ControlsUSER_ID_TB.Text = user_id; frm.ControlsUSER_PWD_TB.Text = user_password; frm.ControlsUSER_CLASS_TB.Text = user_class;/ frm.USER_CLASS_CB.SelectedValue = 2; frm.ControlsUSER_NAME_TB.Text = user_name; frm.ControlsUSER_SEX_TB.Text = user_sex; frm.ShowDialog(); /显示子窗口/给输出参数赋值默认值 outUserid = user_id; outUserpassword = user_password; outUserclass = user_class; outUsername = user_name; 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_TB.Text ; outUserclass= frm.ControlsUSER_CLASS_CB.Text ; outUsername= frm.ControlsUSER_NAME_TB.Text ; outUsersex= frm.ControlsUSER_SEX_TB.Text;MessageBox.Show(修改了数据); else MessageBox.Show(没有修改数据!); frm.Dispose(); int selectrowindex = -1;privatevoid button2_Click(object sender, EventArgs e) if(selectrowindex0) MessageBox.Show(请先选择一条记录数据!);return; /采用弹出窗口方式,进行数据修改DataGridView dgv = dataGridView1;if (dgv.Rowsselectrowindex.Cells1.Value != null) /输入参数string id = dgv.Rowsselectrowindex.Cells0.Value.ToString();string user_id = dgv.Rowsselectrowindex.Cells1.Value.ToString();string user_password = dgv.Rowsselectrowindex.Cells2.Value.ToString();string user_class = dgv.Rowsselectrowindex.Cells3.Value.ToString();string user_name = dgv.Rowsselectrowindex.Cells4.Value.ToString();string user_sex = dgv.Rowsselectrowindex.Cells5.Value.ToString();string outUserid, outUserpassword, outUserclass, outUsername, outUsersex; /输出参数DialogResult myresult; /返回弹出窗口关闭状态this.ShowDialogForm(id, user_id, user_password, user_class, user_name, user_sex,out outUserid, out outUserpassword, out outUserclass, out outUsername, out outUsersex, out myresult);/如果修改了数据,把当前datagridview内容的对应值进行修改if (myresult = 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; privatevoid dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) selectrowindex = e.RowIndex; /获取当前选择的行坐标 /添加记录privatevoid button1_Click(object sender, EventArgs e) /采用弹出窗口方式,进行数据修改DataGridView dgv = dataGridView1;/输入参数string id = -1;string user_id =null;string user_password = null;string user_class = null;string user_name = null;string user_sex = null;string outUserid, outUserpassword, outUserclass, outUsername, outUsersex; /输出参数DialogResult myresult; /返回弹出窗口关闭状态this.ShowDialogForm(id, user_id, user_password, user_class, user_name, user_sex,out outUserid, out outUserpassword, out outUserclass, out outUsername, out outUsersex, out myresult);/如果添加了数据,把当前datagridview内容的对应值进行修改if (myresult = DialogResult.OK) /添加行数据 (DataTable)dataGridView1.DataSource).Rows.Add(1, outUserid, outUserpassword,outUserclass,outUsername,outUsersex);/dataGridView1是不能直接添加行的,只能在他的数据源里添,他会自动绑定 /删除一条记录privatevoid button3_Click(object sender, EventArgs e) int myid = int.Parse(dataGridView1.Rowsselectrowindex.Cells0.Value.ToString();string mysql = delete from user_tb1 where ID= + myid.ToString();DataOperate dataoperate = newDataOperate(); dataoperate.getCom(mysql);MessageBox.Show(删除数据成功);/加载数据 mysql = select * from sql3 ;DataSet ds = dataoperate.getDs(mysql, sql3); dataGridView1.DataSource = ds.Tables0;/* /获取总页码 SELECT Count(id) AS number1 FROM user_tb1; mysql = SELECT Count(id) AS number1 FROM user_tb1 ; DataSet ds = dataoperate.getDs(mysql, user_tb1); int indexsum = int.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); /显示当前页面 try button5_Click(sender, e); /刷新当前页面 catch (Exception) throw; */ / 弹出窗口代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using WDZ.CommonClass;namespace WDZpublicpartialclassdialog2 : Form public dialog2() InitializeComponent(); privatevoid dialog2_Load(object sender, EventArgs e) string mysql = select * from user_class order by id; bindcombox(USER_CLASS_CB, mysql, user_class); USER_CLASS_CB.SelectedIndex=USER_CLASS_CB.FindString(USER_CLASS_TB.Text,-1); /*非常重要,利用string查找到对应的index,然后就可以显示了,或者取value / 填充Combox控件privatevoid bindcombox(ComboBox mycombox,string mysql,string tbname) DataTable mydt = newDataTable();DataOperate dataoperate = newDataOperate();DataSet ds = dataoperate.getDs(mysql, tbname); mydt = ds.Tables0; mycombox.ValueMember =mydt.Columns1.ColumnName; /设置隐含的形式显示数值,起对应一定的名称/cmb_Temp.ValueMember = user_class; mycombox.DisplayMember = mydt.Columns2.ColumnName; mycombox.DataSource = mydt; mycombox.DropDownStyle = ComboBoxStyle.DropDownList; /只能选择,不能写入/cmb_Temp.DropDownStyle =ComboBoxStyle.DropDown; /可以写入/ cmb_Temp.DropDownStyle = ComboBoxStyle.Simple; privatevoid button1_Click(object sender, EventArgs e) /提交数据库操作,。修改数据DataOperate mydopt = newDataOperate();string mysql = ;/修改数据if(ID_TB.Text!=-1) mysql = update user_tb1 set user_id= + USER_ID_TB.Text + ,user_password= + USER_PWD_TB.Text + ,user_class_id= + USER_CLASS_CB.SelectedValue + ,user_name= + USER_NAME_TB.Text + ,user_sex= + USER_SEX_TB.Text + where id= +ID_TB.Text;elseif (ID_TB.Text = -1) mysql = insert into user_tb1 (user_id,user_password,user_class_id,user_name,user_sex) values( + USER_ID_TB.Text + , + USER_PWD_TB.Text + , + USER_CLASS_CB.SelectedValue + , + USER_NAME_TB.Text + , + USER_SEX_TB.Text + ); / MessageBox.Show(mysql); mydopt.getCom(mysql);this.DialogResult = DialogResult.OK; privatevoid button2_Click(object sender, EventArgs e) this.DialogResult = DialogResult.Cancel; 三、 直接在datagridview上修改、添加、删除。不需要添加textbox。而且在view中有combox,下拉类表,可以一次修改多行数据,很有意义。特点:1. view中含有combox,利用了datarowview获得选取的value,index,item都可以了2. 一次可以修改多行数据,利用dataapter的update的特性,比拟麻烦3. 可以直接在view中添加数据,同样利用update特性。问题这种方法,只能直接操作绑定的表,如果绑定的是视图,现在还没想好怎么解决。应该可以解决,需要记录下所有修改的记录的rowindex,然后利用sql语句分别进行修改表,感觉比拟麻烦,放弃了。代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using WDZ.CommonClass;using System.Data.OleDb;namespace WDZpublicpartialclassfrmMain : Form public frmMain() InitializeComponent(); privatevoid frmMain_Load(object sender, EventArgs e) this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; /表格自适应宽度/DataCon datacon = new DataCon();DataOperate dataoperate = newDataOperate();string strSql = select * from user_tb1 ;DataSet ds = dataoperate.getDs(strSql, user_tb1); dataGridView1.DataSource = ds.Tables0; dataGridView1.Columns0.ReadOnly = true; /列不可编写 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; /选择全行/dataGridView1.ReadOnly = true; /只读 dataGridView1.DefaultCellStyle.SelectionBackColor = Color.YellowGreen; /选择的行为颜色/最后一行的星号行不显示,不允许用户添加数据 dataGridView1.AllowUserToAddRows = false; button6.Enabled = false; button7.Enabled = false;/初始化索引数组for (int i = 0; i 20; i+) intindexi = 255;/*/加载下拉列表框 BindSex(); / 绑定性别下拉列表框 cmb_Temp.Visible = false; / 设置下拉列表框不可见/ 添加下拉列表框事件 cmb_Temp.SelectedIndexChanged += newEventHandler(cmb_Temp_SelectedIndexChanged);/ 将下拉列表框参加到DataGridView控件中this.dataGridView1.Controls.Add(cmb_Temp); privatevoid button1_Click(object sender, EventArgs e) string msg = String.Format(第0行,第1列,值:2,dataGridView1.CurrentCell.RowIndex,dataGridView1.CurrentCell.ColumnIndex,dataGridView1.CurrentCell.Value); label1.Text = 选择的单元格为: + msg; privatevoid button2_Click(object sender, EventArgs e) dataGridView1.DataSource = null; privatevoid button3_Click(object sender, EventArgs e) this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; /表格自适应宽度/DataCon datacon = new DataCon();DataOperate dataoperate = newDataOperate(); pagesize = int.Parse(textBox1.Text); /每页显示的记录数string strSql = select top + pagesize.ToString()+ * from user_tb1 ;DataSet ds = dataoperate.getDs(strSql, user_tb1); dataGridView1.DataSource = ds.Tables0; dataGridView1.Columns0.ReadOnly = true; /列不可编写/获取第1页页码 label2.Text = 1; pagenumber = 1;/获取总页码 SELECT Count(id) AS number1 FROM user_tb1; strSql = SELECT Count(id) AS number1 FROM user_tb1 ; ds = dataoperate.getDs(strSql, user_tb1);int indexsum =int.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);/设置上一页下一页按钮的可用性if (pagesum = 1) button7.Enabled = false;else button7.Enabled = true; button6.Enabled = true; int intindex =newint20; /假设一张表上默认数据为20个int selectindex = 0;OleDbConnection conn;OleDbDataAdapter adapter;privateDataTable dbconn(string strSql) DataCon myconn = newDataCon(); conn = myconn.getCon();this.adapter = newOleDbDataAdapter(strSql,conn);DataTable dtSelect = newDataTable();int rnt = this.adapter.Fill(dtSelect);return dtSelect; privateBoolean dbUpdate() string strSql = select * from user_tb1;DataTable dtUpdate = newDataTable(); dtUpdate = dbconn(strSql); dtUpdate.Rows.Clear();DataTable dtShow = newDataTable(); dtShow = (DataTable)this.dataGridView1.DataSource;/dtUpdate.ImportRow(dtShow.Rowsintindex); /如果不进行循环多行插入的话,只能够插入最后修改的一行数据。for (int i = 0; i 20;i+ ) if (intindexi!=255) dtUpdate.ImportRow(dtShow.Rowsintindexi); try /SqlCommandBuilder CommandBuilder;OleDbCommandBuilder CommandBuilder; CommandBuilder = newOleDbCommandBuilder(this.adapter);this.adapter.Update(dtUpdate); catch (Exception ex) MessageBox.Show(ex.Message.ToString();returnfalse; dtUpdate.AcceptChanges();returntrue; privatevoid button4_Click(object sender, EventArgs e) if (dbUpdate() MessageBox.Show(修改成功!); for (int i = 0; i 20; i+) intindexi = 255; button5_Click(sender, e); privatevoid dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) /intindex = e.RowIndex; selectindex = e.RowIndex; privatevoid dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) int mycellindex = e.RowIndex;/MessageBox.Show(dataGridView1.Rowse.RowIndex.Cells1.Value.ToString();if (dataGridView1.Rowse.RowIndex.Cells1.Value.ToString() = | dataGridView1.Rowse.RowIndex.Cells1.Value.ToString() = null)MessageBox.Show(用户ID不能为空!);else intindexmycellindex % 20 = mycellindex;/ MessageBox.Show(mycellindex.ToString(); int pagenumber = 0;int pagesum = 0;int pagesize=2; /每页的条目数privatevoid
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 办公文档 > 模板表格


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!