User1204501
User1204501

Reputation: 831

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.

My DataGrid consists of 4 columns and the number of rows vary depending on the records.

Example message: This cell is null because it is not applicable.

I would really appreciate some help.

Cheers

Upvotes: 0

Views: 851

Answers (2)

Rohit
Rohit

Reputation: 10236

Assuming you are using WinForms I think the only way is to loop through your Data Grid View rows..Here is a sample code

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

Upvotes: 1

Ravi
Ravi

Reputation: 853

There are different ways to do that.

Server Side

You can use DataGrid.ItemDataBound event and check the Data at RunTime

ClientSide

You can also call a ClientSide function to loop through all empty cell and replace the string e.g.

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

Upvotes: 1

Related Questions