如何检查datagridview的所有列单元格?如果一个或多个单元格为空,它将返回一个值? [英] How to check all column cell of datagridview? If one or more cell is empty, it will return a value?

查看:136
本文介绍了如何检查datagridview的所有列单元格?如果一个或多个单元格为空,它将返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查看datagridview的单元格。如果至少其中一个单元格为空,它将返回false



我尝试过:



等等,我有这个方法,我知道这是不正确的。因为如果数组中的最后一项有值,它将检查为真。我只是无法为我打算制作的内容获得正确的代码或逻辑。

I want to check the cells of my datagridview. also if atleast one of the cell is empty, it will return "false"

What I have tried:

and so, I have this method WHICH is i knew is not correct. since if ever the last item in the array has value, it will make "checking" a true. I just cant get the correct code or logic for what i plan to make.

public void checker()
        {
            string[] check = new string[50];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                
                check[i] = dataGridView1.Rows[i].Cells[0].Value.ToString();
                
            }
            for (int e = 0; e < check.Length; e++)
            {
                if (check[e] == null)
                {
                    checking = "false";
                }
                else
                {
                    checking = "true";
                }
            }
        }

推荐答案

试错法是学习算法的好方法。 ..只要你了解错误的原因。调试器是一个很好的工具,因为它允许你逐步看到你的代码正在做什么。

在你的代码中,你设置检查每个单元格一次,这意味着最终的resilt仅取决于最后一个单元格的值,这不是你想要的。

Trial and error is a good way to learn algorithms ... as long as you understand the reason of error. The debugger is a great tool for this purpose because it allow you to see what your code is doing step by step.
In your code, you set checking once per cell, which means that the final resilt depend only on the value of last cell, that is not what you want.
Quote:

我想查看datagridview的单元格。如果至少有一个单元格是空的,它将返回false

I want to check the cells of my datagridview. also if at least one of the cell is empty, it will return "false"



这转换为:

- 答案是真,只要因为你没有遇到一个空单元格。

- 只有当你遇到一个空单元时,答案才是假。

这导致了解决方案1的代码。您可以在开始时设置默认答案,并仅在遇到满足条件的单元格时更改它。



有一个工具可以让您查看代码是什么这样做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - 您管 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它找不到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


This translates to:
- The answer is "true" as long as you don't encounter an empty cell.
- The answer is "false" only when you encounter an empty cell.
Which lead to the code of Solution 1. You set the default answer at beginning and change it only when you encounter a cell that meet your condition.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


你可以这样做:

You could do it like this:
   checking = true;

      for (int i = 0; i < dataGridView1.Rows.Count; i++)
      {
         for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
         {
            var cellValue = dataGridView1.Rows[i].Cells[j].Value;

            if (cellValue == null || 
cellValue == DBNull.Value || string.IsNullOrEmpty(CellValue.ToString()))
            {
                checking = "false";
                return;
            }
         }
      }


这篇关于如何检查datagridview的所有列单元格?如果一个或多个单元格为空,它将返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆