如何禁用格式化等于其他数据网格上的列?
How to disable formatting equals column on other datagrid?
我正在开发 Windows 贷款申请表。当我点击 datagrid1 添加(绿色)按钮时,设备被添加到 datagrid2 上,但我希望每个项目只发生一次。
我正在使用此代码传递线路数据:
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);
}
}
}
有几种方法可以实现我认为您所要求的。
第一种方法是在将项目添加到第二个列表后实际将其从第一个列表中删除。这甚至可以防止用户尝试添加多个,并确保界面呈现的内容具有上下文意义。
话虽如此,要准确回答问题,因为您正在有效地克隆该行,您可以执行比较以确定它是否已被添加:
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;
}
if (!this.dataGridEmprestimo.Rows.Contains(rowData))
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
在优化方面,您可能只需要检查 Row 中的单个值(例如 Id)以确定它是否已经存在。您可以编写一个简单的方法来 return 具有该 ID 的行数。如果有 none,或者数量小于允许的最大值,那么您可以随意添加该行。
public int CountObjectsInRows(string idValue)
{
int count = 0;
if (string.IsNullOrEmpty(idValue))
{
return count;
}
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
if (row.Cells[0].Value.ToString() == idValue)
{
count++;
}
}
return count;
}
您可以使用此方法代替上述 "this.dataGridEmprestimo.Rows.Contains(rowData)" 条件。
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;
}
if (CountObjectsInRows(rowData.Cells[0].ToString()) == 0)
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
我正在开发 Windows 贷款申请表。当我点击 datagrid1 添加(绿色)按钮时,设备被添加到 datagrid2 上,但我希望每个项目只发生一次。
我正在使用此代码传递线路数据:
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);
}
}
}
有几种方法可以实现我认为您所要求的。
第一种方法是在将项目添加到第二个列表后实际将其从第一个列表中删除。这甚至可以防止用户尝试添加多个,并确保界面呈现的内容具有上下文意义。
话虽如此,要准确回答问题,因为您正在有效地克隆该行,您可以执行比较以确定它是否已被添加:
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;
}
if (!this.dataGridEmprestimo.Rows.Contains(rowData))
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
在优化方面,您可能只需要检查 Row 中的单个值(例如 Id)以确定它是否已经存在。您可以编写一个简单的方法来 return 具有该 ID 的行数。如果有 none,或者数量小于允许的最大值,那么您可以随意添加该行。
public int CountObjectsInRows(string idValue)
{
int count = 0;
if (string.IsNullOrEmpty(idValue))
{
return count;
}
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
if (row.Cells[0].Value.ToString() == idValue)
{
count++;
}
}
return count;
}
您可以使用此方法代替上述 "this.dataGridEmprestimo.Rows.Contains(rowData)" 条件。
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;
}
if (CountObjectsInRows(rowData.Cells[0].ToString()) == 0)
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}