c#怎么比较datagridview的两列数
时间: 2023-08-02 17:41:34 浏览: 85
在C#中,可以通过以下步骤来比较DataGridView的两列数:
1. 获取DataGridView中的两个列。
```
DataGridViewColumn col1 = dataGridView1.Columns[0];
DataGridViewColumn col2 = dataGridView1.Columns[1];
```
2. 遍历DataGridView中的每一行,比较两个单元格的值。
```
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(row.Cells[col1.Index].Value != null && row.Cells[col2.Index].Value != null)
{
int value1 = Convert.ToInt32(row.Cells[col1.Index].Value);
int value2 = Convert.ToInt32(row.Cells[col2.Index].Value);
if(value1 > value2)
{
// Do something
}
else if(value1 < value2)
{
// Do something else
}
else
{
// Do something if they are equal
}
}
}
```
这段代码将比较第一列和第二列中每一行的值,并根据它们的关系执行不同的操作。请注意,这里假设列中的值都是整数类型,如果不是,需要相应地进行转换。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)