C# DataGridView控件更新数据的方式
WinFrom程序绑定了一个DataGridView控件,我需要添加一个button按钮来更改状态,还需要把更新之后的状态更新到数据库,如下图所示的这样:
1、按钮根据不同条件有不同的展示:
代码:
//dataGridView第2列隐藏 dataGridView1.Columns[1].Visible = false;
2、按钮区域是通过视图界面编辑的,也可在程序加载时动态添加:
3、数据窗口绑定数据
采用独立的方法searh()去根据条件获取数据并绑定,其中 dataGridView1.DataSource = dt_doctor; 是将datatable对象绑定到dataGridView控件;
private void Searh() { string sql; if (checkBox1.Checked) { sql = "select MR_NAME,SERIAL_NO,MR_CODE,DATA_MODEL_CODE from cpr.mr_item_index where DATA_MODEL_CODE = '" + comboBox1.Text + "F' and DEPT_CODE = '*' order by SERIAL_NO asc"; dataGridView1.Columns[1].Visible = false; dataGridView1.Columns[2].Visible = true; } else { sql = "select MR_NAME,SERIAL_NO,MR_CODE,DATA_MODEL_CODE from cpr.mr_item_index where DATA_MODEL_CODE = '" + comboBox1.Text + "' and DEPT_CODE = '*' order by SERIAL_NO asc"; dataGridView1.Columns[2].Visible = false; dataGridView1.Columns[1].Visible = true; } DataTable dt_doctor = conn.GetDataSet(sql).Tables[0]; dataGridView1.DataSource = dt_doctor; dataGridView1.Columns[3].HeaderText = "模板名称"; dataGridView1.Columns[4].HeaderText = "排序编号"; dataGridView1.Columns[5].HeaderText = "模板编码"; dataGridView1.Columns[6].HeaderText = "病历子集编码"; //根据单元格内容自动跳帧宽度 dataGridView1.AutoResizeColumns(); dataGridView1.Columns[6].ReadOnly = true; dataGridView1.Columns[5].ReadOnly = true; dataGridView1.Columns[0].Width = 50; dataGridView1.Columns[1].Width = 50; dataGridView1.Columns[2].Width = 50; }
4、更新一条数据做法
双击datagridview控件,修改其点击事件代码:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //点击的行索引大于等于0,则执行接下来的语句,这里排除点击了边框区域的BUG问题 //e.RowIndex 行索引号 if (e.RowIndex >= 0) { //根据列索引获取点击的单元元素 e.ColumnIndex DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex]; switch (e.ColumnIndex) { case 0: //判断列是否为DataGridViewButtonColumn if (column is DataGridViewButtonColumn) { //根据行索引获取行数据集合 DataGridViewRow Rows = dataGridView1.Rows[e.RowIndex]; //组合更新语句,Rows.Cells[2].Value 当前行第三列数据 //在将数据转换为INT类型前,需要先把Object对象转换为string //Rows.Cells[3].Value.ToString() string sql = "update cpr.mr_item_index set MR_NAME = '" + Rows.Cells[3].Value + "',SERIAL_NO = " + Int32.Parse(Rows.Cells[4].Value.ToString()) + " where MR_CODE = '" + Rows.Cells[5].Value + "'"; if (conn.ExecSQLresult(sql) > 0) { Searh(); } } break; case 1: //判断列是否为DataGridViewButtonColumn if (column is DataGridViewButtonColumn) { //根据行索引获取行数据集合 DataGridViewRow Rows = dataGridView1.Rows[e.RowIndex]; string sql = "update cpr.mr_item_index set DATA_MODEL_CODE = '" + Rows.Cells[6].Value + "F' where MR_CODE = '" + Rows.Cells[5].Value + "'"; if (conn.ExecSQLresult(sql) > 0) { Searh(); } } break; case 2: //判断列是否为DataGridViewButtonColumn if (column is DataGridViewButtonColumn) { //根据行索引获取行数据集合 DataGridViewRow Rows = dataGridView1.Rows[e.RowIndex]; string str = Rows.Cells[6].Value.ToString().Substring(0, Rows.Cells[6].Value.ToString().Length - 1); string sql = "update cpr.mr_item_index set DATA_MODEL_CODE = '" + str + "' where MR_CODE = '" + Rows.Cells[5].Value + "'"; if (conn.ExecSQLresult(sql) > 0) { Searh(); } } break; default: break; } } }
以上代码根据点击的哪一行哪一列的按钮进行判断,执行不同的按钮功能;
5、批量更新修改数据
采用Update_All()方法批量更新修改的数据(该方法会从0到全部的更新数据);
private void Update_All() { if (dataGridView1.Rows.Count > 0) { for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { DataGridViewRow Rows = dataGridView1.Rows[i]; string sql = "update cpr.mr_item_index set MR_NAME = '" + Rows.Cells[3].Value + "',SERIAL_NO = " + Int32.Parse(Rows.Cells[4].Value.ToString()) + " where MR_CODE = '" + Rows.Cells[5].Value + "'"; conn.ExecSQLresult(sql); } MessageBox.Show("数据批量更新完毕!!!", "提示"); Searh(); } else { MessageBox.Show("没有数据需要更新!!!", "警告"); } }
6、模糊搜索定位数据
首先我们得创建点击按钮,判断是否有选中的行数据,没有选中行从第一个数据检索,有则从选中数据第一个行索引开始检索;
int ii = 0; if (this.dataGridView1.SelectedRows.Count>0) { for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)//遍历所有选中的行 { ii = this.dataGridView1.SelectedRows[i].Index; } Searh_name(ii); } else { Searh_name(1); }
检索方法如下:
这行语句
Rows.Cells[3].Value.ToString().Contains(textBox1.Text.Trim())
表示指定单元格数据是否包含某个子字符串
private void Searh_name(int start_i) { if (dataGridView1.Rows.Count > 0) { dataGridView1.Rows[start_i].Selected = false; for (int i = start_i+1; i < dataGridView1.Rows.Count; i++) { DataGridViewRow Rows = dataGridView1.Rows[i]; if (Rows.Cells[3].Value.ToString().Contains(textBox1.Text.Trim())) { dataGridView1.Rows[i].Selected = true; this.dataGridView1.CurrentCell = this.dataGridView1.Rows[i].Cells[0]; return; } } } else { MessageBox.Show("没有数据检索!!!", "警告"); } }