datagridview后台获取选中行的值的几种方式
1.双击datagridview中的一行,后台获取改行的一个或多个列
private void dgvImport_DoubleClick(object sender, EventArgs e) { string documentNo = dgvImport.CurrentRow.Cells["DOCUMENT_NO"].Value.ToString(); GetImportDetail(documentNo); }
2.dagagridview前添加checkbox按钮,可以选中多行,后台获取选中的多行的列
string sel = this.dgvImportMaster.Rows[e.RowIndex].Cells["SELECTION"].Value.ToString(); string documentNO = this.dgvImportMaster.Rows[e.RowIndex].Cells["DOCUMENT_NO"].Value.ToString();
3.单元格值更改时
/// <summary> /// 单元格值更改时发生 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvExpExportDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dgvExpExportDetail.CurrentRow == null) return; if (dgvExpExportDetail.CurrentCell.OwningColumn.Name == "QUANTITY") { string filter = "COMMUNITY_CODE=\'" + dgvExpExportDetail.CurrentRow.Cells["COMMUNITY_CODE"].Value + "\' AND DOCUMENT_NO=\'" + dgvExpExportDetail.CurrentRow.Cells["DOCUMENT_NO"].Value + "\' AND ITEM_NO=\'" + dgvExpExportDetail.CurrentRow.Cells["ITEM_NO"].Value + "\'"; DataRow[] rowList = dsExpDetail.Tables[0].Select(filter); if (rowList.Length < 0) { return; } DataRow newRow = rowList[0]; if (!string.IsNullOrEmpty(newRow["EXP_CODE"].ToString())) { decimal quantity = 0; if (!decimal.TryParse(dgvExpExportDetail.CurrentCell.Value.ToString(), out quantity)) { MessageBox.Show("数量必须为数字!", "注意"); newRow["QUANTITY"] = newRow["INVENTORY"]; return; } if (quantity > decimal.Parse(newRow["INVENTORY"].ToString())) { MessageBox.Show("数量不能大于库存数量:" + newRow["INVENTORY"], "注意"); newRow["QUANTITY"] = newRow["INVENTORY"]; return; } newRow["QUANTITY"] = dgvExpExportDetail.CurrentCell.Value; } } }