C# 如何从Cell_Leave事件中获取DataGridViewCell的值? private void dataGridView1\u CellLeave(对象发送方,DataGridViewCellEventArgs e) { 如果(如ColumnIndex>1) { int cellValue=Convert.ToInt32(((DataGridViewCell)sender.Value); 如果(单元值
我正在尝试获取触发事件的单元格的值 当我尝试将C# 如何从Cell_Leave事件中获取DataGridViewCell的值? private void dataGridView1\u CellLeave(对象发送方,DataGridViewCellEventArgs e) { 如果(如ColumnIndex>1) { int cellValue=Convert.ToInt32(((DataGridViewCell)sender.Value); 如果(单元值,c#,winforms,datagridview,int,C#,Winforms,Datagridview,Int,我正在尝试获取触发事件的单元格的值 当我尝试将sender强制转换为DataGridViewCell时,会触发异常: 无法强制转换类型为的对象 “System.Windows.Forms.DataGridView”到 类型 “System.Windows.Forms.DataGridViewCell” 你建议我做什么 我需要检查该值是否小于20,如果小于,则将其增加到21。尝试使用数据网格[e.RowIndex,e.ColumnIndex]。值。我希望发送方更可能是DataGridView对象,
sender
强制转换为DataGridViewCell
时,会触发异常:
无法强制转换类型为的对象
“System.Windows.Forms.DataGridView”到
类型
“System.Windows.Forms.DataGridViewCell”
你建议我做什么
我需要检查该值是否小于20,如果小于,则将其增加到21。尝试使用
数据网格[e.RowIndex,e.ColumnIndex]。值
。我希望发送方更可能是DataGridView对象,而不是单元格本身。您可以将单元格的值作为
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;
}
}
}
发件人的类型为DataGridView,因此您可以使用以下行:
dataGridView1[e.ColumnIndex, e.RowIndex].FormattedValue;
private void dataGridView1\u CellEndEdit(对象发送方,DataGridViewCellEventArgs e)
{
如果(dataGridView1.Rows[e.RowIndex].Cells[1].Value!=null)
{
int cellmarks=Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
如果(单元格标记<32)
{
dataGridView1.Rows[e.RowIndex]。单元格[2]。Value=“Fail”;
}
其他的
{
dataGridView1.Rows[e.RowIndex]。单元格[2]。Value=“Pass”;
}
}
}
此代码将获取currentcell值。这可能会对您有所帮助。我为一个_CellClick事件做了一个轻微的修改
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";
}
}
}
我建议使用EditedFormattedValue属性获取单元格的值,因为在我的测试中,我输入的FormattedValue始终为null。这是一个非常好的答案,因为有时会创建一个通用事件处理程序。就是,;事件处理程序旨在为多个DataGridView提供服务。Dulini Atapattu通过将sender参数作为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";
}
}
}
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;
...