简体   繁体   English

如何在C#中检测DataGrid中的空单元或空单元

[英]How to detect Null or empty cells in DataGrid in C#

I have a DataGrid which consists of some null cells or cells with empty space in which I would like to display some message to the user. 我有一个DataGrid ,它由一些null单元格或具有空白空间的单元格组成,我想在其中向用户显示一些消息。

My DataGrid consists of 4 columns and the number of rows vary depending on the records. 我的DataGrid由4列组成,行数取决于记录。

Example message: This cell is null because it is not applicable . 消息示例: This cell is null because it is not applicable

I would really appreciate some help. 我真的很感谢您的帮助。

Cheers 干杯

Assuming you are using WinForms I think the only way is to loop through your Data Grid View rows ..Here is a sample code 假设您使用的是WinForms,我认为唯一的方法是循环遍历Data Grid View rows这是示例代码

 foreach (DataGridViewRow row in this.dataGridView1.Rows)
 {
   for (int i = 0; i < row.Cells.Count; i++)
     {
       if (row.Cells[i].Value == null || row.Cells[i].Value == DBNull.Value ||   
        String.IsNullOrWhitespace(row.Cells[i].Value.ToString())
            {
                //Show your message
            }
      } 
  }

There are different ways to do that. 有不同的方法可以做到这一点。

Server Side 服务器端

You can use DataGrid.ItemDataBound event and check the Data at RunTime 您可以使用DataGrid.ItemDataBound事件并在运行时检查数据

ClientSide 客户端

You can also call a ClientSide function to loop through all empty cell and replace the string eg 您也可以调用ClientSide函数来循环所有空单元格并替换字符串,例如

function UpdateEmptyCells() {
$("#DataGrid table tr:gt(0) td").each(function (e, r) {
    if ($(this).text() === '') {
        $(this).text('EMPTY MESSAGE');
    }
});   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM