![](/img/trans.png)
[英]How can I iterate over a column to find cells with a specific value and then iterate over those rows?
[英]How can I change the value of cells in a given column when selecting multiple rows?
我希望能够在数据表中选择多行,然后如果选择了它们,则更改单元格中的值。 我的代码是:
private void btnSetToReceived_Click(object sender, EventArgs e)
{
SetToReceived();
}
private void SetToReceived()
{
this.dgvPod.CurrentCell.Value = "Yes";
}
private void dgvPod_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
dgvPod.BeginEdit(true);
SetToReceived();
}
我从你的问题中得到的是,你想选择多个单元格,然后开始输入,你希望所有选定的单元格值都发生变化:
为此,您可以处理EditingControlShowing
事件并获取TextBox
编辑控件并处理其TextChanged
事件并更新所选单元格的文本。 例如:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 4;
dataGridView1.RowCount = 4;
TextBox txt = null;
dataGridView1.EditingControlShowing += (s1, e1) =>
{
if (dataGridView1.EditingControl is TextBox)
{
if (txt == null)
{
txt = (TextBox)dataGridView1.EditingControl;
txt.TextChanged += (s2, e2) =>
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
cell.Value = txt.Text;
};
}
}
};
}
该代码不会检查所选单元格是否都在同一列中,但是它在示例中显示了如何在键入的同时获取文本以及如何设置其他所选单元格的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.