[英]How can I get the Value of a DataGridViewCell from the Cell_Leave event?
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex > 1)
{
int cellValue = Convert.ToInt32(((DataGridViewCell)sender).Value);
if (cellValue < 20)
{
((DataGridViewCell)sender).Value = 21;
}
}
}
我正在尝试获取事件触发的单元格的值。
当我尝试将sender
转换为DataGridViewCell
时会触发异常:
无法将类型为“System.Windows.Forms.DataGridView”的对象强制转换为“System.Windows.Forms.DataGridViewCell”。
你推荐我做什么?
我需要检查值是否小于20,如果是,则将其提高到21。
尝试使用theDataGrid[e.RowIndex, e.ColumnIndex].Value
。 我希望发送者更可能是DataGridView对象而不是单元本身。
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[1].Value != null)
{
int cellmarks = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
if (cellmarks < 32)
{
dataGridView1.Rows[e.RowIndex].Cells[2].Value = "Fail";
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[2].Value = "Pass";
}
}
}
此代码将获得currentcell值。这可以帮助您。
您可以获取单元格的值
dataGridView1[e.ColumnIndex, e.RowIndex].FormattedValue;
发件人的类型是DataGridView,因此您可以使用以下行:
int cellValue = Convert.ToInt32(((DataGridView)sender).SelectedCells[0].Value);
我为_CellClick事件做了一个轻微的变体。
private void Standard_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
int intHeaderId = 0;
switch (((DataGridView)sender).Columns[e.ColumnIndex].Name)
{
case "grcHeaderId":
intHeaderId = (int)grvEnteredRecords[grcHeaderId.Index, e.RowIndex].Value;
break;
...
我建议使用EditedFormattedValue属性获取单元格的值,因为在我的测试中,我输入的FormattedValue始终为null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.