c# - Datagridview单元格值不相等但它们应该是c#
问题描述
我有一个数据绑定 datagridview 表,用户可以在其中输入每行中的一组值。每次用户更改/单击新单元格时都会调用一个关联的 CellContentClick 事件。在这种情况下,它首先检查该行是否已满,然后有一个 if 语句通过检查以及两个感兴趣的单元格的值是否相等。
当用户点击一个新的单元格时,它只运行两个单元格不相等的情况,无论它们相等还是不相等。我试过用另一种方式切换它,说它是否相等 == 运行 if 语句,但它没有通过语句。我还尝试了 if/else if 语句来强制这两种情况,但它仍然没有通过它们相等的情况。为什么当它在消息框中显示值理论上应该相等时,它不通过'等于'/'else'语句?
这是代码:
'''private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) { //MessageBox.Show("点击的单元格内容"); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MessageBox.Show("单元格改变了"); int empty_row = 0;
if (dataGridView1.RowCount != 0)
{ // If there is more than 1 row, check respective columns
for (int i = 0; i < Convert.ToInt32(number_of_sections.Text); i++)
{
// Check that the row is not empty
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (int j = 0; j <= dataGridView1.Columns.Count - 1; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value == null || dataGridView1.Rows[i].Cells[j].Value == DBNull.Value || String.IsNullOrWhiteSpace(dataGridView1.Rows[i].Cells[j].Value.ToString()))
{
empty_row = 1; // 1 when there is an empty cell
}
else
{
// if the cell has a value in it, do nothing
}
//MessageBox.Show("this is the cell " + "'" + dataGridView1.Rows[0].Cells[i].Value + "'");
}
if (empty_row == 0)
{
// if the cell has a value in it, compare the diameters to determine profile
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (dataGridView1.Rows[i].Cells[3].Value != dataGridView1.Rows[i].Cells[4].Value)
{
dataGridView1.Rows[i].Cells[2].Value = "linear";
MessageBox.Show("Passed through linear if, the value is " + dataGridView1.Rows[i].Cells[3].Value + " and " + dataGridView1.Rows[i].Cells[4].Value);
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "flat";
}
// if the cell has a value in it, compare the speeds to determine profile
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (dataGridView1.Rows[i].Cells[6].Value != dataGridView1.Rows[i].Cells[7].Value)
{
dataGridView1.Rows[i].Cells[5].Value = "linear";
MessageBox.Show("Passed through linear if, the value is " + dataGridView1.Rows[i].Cells[6].Value + " and " + dataGridView1.Rows[i].Cells[7].Value);
}
else
{
dataGridView1.Rows[i].Cells[5].Value = "flat";
}
}
else
{
empty_row = 1; // 1 when there is an empty cell
}
empty_row = 0; // reset
}
}
} '''
解决方案
@JohnG 回答了上面评论中的问题,它解决了我的问题,
“我很确定‘if’语句... if (dataGridView1.Rows[i].Cells[3].Value != dataGridView1.Rows[i].Cells[4].Value) {...} 将始终返回 true。 dataGridView1.Rows[i].Cells[3].Value 将返回一个对象,dataGridView1.Rows[i].Cells[4].Value ... 显然不会是相同的对象。我相信你需要将这些对象转换为整数然后比较它们。在这种情况下,由于比较是“等于”,那么您可以比较字符串,但是如果您需要“<” 或“>”……那么您需要将值转换为整数。”
推荐阅读
- javascript - 如何根据所选值加载 JSON 数据
- java - 如何将 hashmap 键(国家名称)与从 CSV 文件中检索的国家名称进行比较?
- asp.net-core - 使用预选项目填充 Bootstrap 双列表框
- ssl - TestCafe、HTTPS 和多浏览器测试
- php - PHP如何将数组组织成组但保持顺序?
- swiftui - SwiftUI 中 DragGesture 和 ScrollView 的交互
- java - 如果发生某些事情,我怎样才能使我的整数从较早的更改数字中获取?
- javascript - 如何将用户输入的数据更新为 JSON
- java - 从 Spring Batch 访问 OAuth 安全 API
- php - 如何编写验证整数并返回整数或空值的简写表达式?