我正在开发Windows Forms应用程序以进行贷款。当我单击datagrid1的add(绿色)按钮时,设备已添加到datagrid2,但是我希望每个项目只发生一次。图像程序:https://i.stack.imgur.com/mdTLP.png我正在使用此代码传递行数据:
private void DataGridEquipamento_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
{
return;
}
if (e.ColumnIndex == 2)
{
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
}
您可以删除或禁用对datagrid1中已选择的项目的单击。
下面的代码在单击时将单元格的颜色更改为灰色,并且在以后的单击中不允许更改灰色单元格。
private void DataGridEquipamento_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
{
return;
}
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
if(CellStyle.BackColor != Color.Gray)
{
if (e.ColumnIndex == 2)
{
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
CellStyle.BackColor = Color.Gray;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;
}
}