将 DataGridView 值转换为 int



我从datagridview内的单元格获取值,如下所示,我将该值转换为int首先我使用了"Convert.ToInt32",但出现错误时说

输入字符串的格式不正确。

我搜索并发现我必须使用int.tryParse

ImgId = Int32.TryParse(DGV_PatientImages.Rows[RowIndex].Cells[4].Value, out ImgId);

但我收到一个错误说

'int. 的最佳重载方法匹配。TryParse(string, out int(' 有一些无效的参数

int ImgId = 0;

所以我尝试用户(int(指定的强制转换无效

ImgId = (int)DGV_PatientImages.Rows[RowIndex].Cells[4].Value;

我得到了

指定的强制转换无效

DataGridViewRow dgvrow = DGV_PatientImages.CurrentRow;
if (dgvrow.Cells["DGV_PatientImages_ID"].Value != DBNull.Value)
{
ImgId = Convert.ToInt32(DGV_PatientImages.Rows[RowIndex].Cells[4].Value);
}

请提供任何建议

好吧DGV_PatientImages.Rows[RowIndex].Cells[4].Value是一种不能转换为int的类型。它不是一个字符串。

通过以下代码行,您可以简单地获取其类型:

string type = DGV_PatientImages.Rows[RowIndex].Cells[4].Value.GetType().ToString();

最新更新