2013-10-10 61 views
0

我有一個DataGrid其中包含一些null單元格或具有空白空間的單元格,我想在其中向用戶顯示一些消息。如何在C#中的DataGrid中檢測空單元或空單元格

我的DataGrid由4列組成,行數取決於記錄。

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

我真的很感謝一些幫助。

乾杯

回答

1

假設你正在使用的WinForms,我認爲唯一的辦法是遍歷你Data Grid View rows ..Here是一個示例代碼

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 
      } 
     } 
    } 
1

有不同的方法來做到這一點。

服務器端

您可以使用DataGrid.ItemDataBound事件,並在運行時檢查數據

客戶方

您也可以調用客戶方函數來遍歷所有空單元格,更換字符串例如

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