本文主要是介绍WinForm小技巧之向下复制行和编辑行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
WinForm小技巧之向下复制行
文章目录
- WinForm小技巧之向下复制行
- 场景
- 代码
- 编辑
- 场景
- 代码
场景
当我点击向下增加行按钮,会在表格中添加一行一摸一样的
代码
按钮点击事件
/// <summary>/// 向下新增/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void addSpaceBtn_Click(object sender, EventArgs e){// 假设你的DataGridView的数据源是一个DataTableDataTable dt = (DataTable)dataGridView_machine.DataSource;// 获取最后一行的数据DataRow lastRow = dt.Rows[dt.Rows.Count - 1];// 在DataTable中添加一个新行DataRow newRow = dt.NewRow();string condition = " set ";//用于添加字段var para = new List<MySql.Data.MySqlClient.MySqlParameter>();// 将最后一行的数据复制到新行for (int i = 0; i < lastRow.ItemArray.Length; i++){string columnName = dt.Columns[i].ColumnName;if (!columnName.Equals("ID")){newRow[columnName] = lastRow[columnName]; // 使用列名来设置新行的值para.Add(new MySql.Data.MySqlClient.MySqlParameter(columnName, newRow[columnName]));}}//id 自增一string sqlmaxid = String.Format("select max(ID) as maxId from {0}", Table_lab_test_schedule.table_name);var maxid = DataBase.Get_LabratoryManage(sqlmaxid);int ID = int.Parse(maxid.Rows[0]["maxId"].ToString()) + 1;para.Add(new MySql.Data.MySqlClient.MySqlParameter(Table_lab_test_schedule.ID, ID.ToString()));// 将新行添加到DataTabledt.Rows.Add(newRow);//添加数据库string sql = String.Format("insert into {0} ({1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31})values(@{1},@{2},@{3},@{4},@{5},@{6},@{7},@{8},@{9},@{10},@{11},@{12},@{13},@{14},@{15},@{16},@{17},@{18},@{19},@{20},@{21},@{22},@{23},@{24},@{25},@{26},@{27},@{28},@{29},@{30},@{31})", Table_lab_test_schedule.table_name, Table_lab_test_schedule.keshi, Table_lab_test_schedule.Type, Table_lab_test_schedule.jinji, Table_lab_test_schedule.mingcheng, Table_lab_test_schedule.shuxing, Table_lab_test_schedule.User, Table_lab_test_schedule.Xinghao, Table_lab_test_schedule.Test_from, Table_lab_test_schedule.Test_to, Table_lab_test_schedule.No, Table_lab_test_schedule.Quatity, Table_lab_test_schedule.tiaojian, Table_lab_test_schedule.jieshoudate, Table_lab_test_schedule.Tester, Table_lab_test_schedule.Jihua_from, Table_lab_test_schedule.Jihua_to, Table_lab_test_schedule.Yuchuli_Mach, Table_lab_test_schedule.Yuchuli_from, Table_lab_test_schedule.Yuchuli_to, Table_lab_test_schedule.Test_Mach, Table_lab_test_schedule.Check_Mach, Table_lab_test_schedule.Check_from, Table_lab_test_schedule.Check_to, Table_lab_test_schedule.result, Table_lab_test_schedule.yichang, Table_lab_test_schedule.storeplace, Table_lab_test_schedule.storestate, Table_lab_test_schedule.tuiyangamount, Table_lab_test_schedule.tuiyangremark, Table_lab_test_schedule.Pause, Table_lab_test_schedule.ID);if (!DataBase.Deal_LabratoryManage(sql, para.ToArray())){new WarningDialogForm("操作失败!").ShowDialog();return;}// 更新DataGridView以显示新添加的行dataGridView_machine.DataSource = null; // 清除数据源绑定dataGridView_machine.DataSource = dt; // 重新绑定数据源LoadTable();//重新加载数据}
编辑
场景
代码
给单元格添加事件
/// <summary>/// 用户编辑单元格结束/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void dataGridView_machine_CellEndEdit(object sender, DataGridViewCellEventArgs e){// 获取当前编辑的单元格DataGridViewCell cell = dataGridView_machine[e.ColumnIndex, e.RowIndex];// 获取编辑的列名string columnName = dataGridView_machine.Columns[e.ColumnIndex].Name;if (columnName.Equals("check")){return;}// 获取单元格所在的行DataGridViewRow row = dataGridView_machine.Rows[e.RowIndex];//获取流水号string ID = row.Cells[Table_lab_test_schedule.ID].Value.ToString();string sql = $"update {Table_lab_test_schedule.table_name} set {columnName}='{cell.Value}' where {Table_lab_test_schedule.ID}='{ID}'";// 获取行数据//string columnName1 = row.Cells["ColumnName1"].Value.ToString();// string columnName2 = row.Cells["ColumnName2"].Value.ToString();// ... 获取其他列的数据if (!DataBase.Deal_LabratoryManage(sql)){new WarningDialogForm("操作失败").ShowDialog();return;}//LoadTable();}
这篇关于WinForm小技巧之向下复制行和编辑行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!