簡體   English   中英

如何檢查單元格是否為空。 c#/datagridview

[英]How to check if the cell is empty. c#/datagridview

我有一個用於單擊單元格的事件處理程序。 我想檢查我單擊的單元格是否包含某些內容。 如果不是,則發送錯誤消息。 除此之外,我只希望用戶能夠單擊前 2 列,如果單擊了任何其他列,則它不會執行任何操作。 有任何想法嗎?

FirstName   |   LastName   | Monday   | Tuesday   |   Wednesday  |
            |              |          |           |              |
William     |   Oliver     |          |           |              |
James       |   Price      |          |           |              |

因此,如果選擇了名稱,那么它會做一些事情。 如果單擊星期一/星期二等中的空白單元格,則不會發生任何事情。

希望這是有道理的。

代碼:

private void metroDataGrid1_CellClick(object sender, DataGridViewCellEventArgs e) 
{
     //if Statement to see if cell contains anything, if it does then open a new form below...

    frmUserDiary userdiaryclick = new frmUserDiary();
    userdiaryclick.ShowDialog();
}

謝謝

更新代碼:

if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
{
    frmUserDiary userdiaryclick = new frmUserDiary();
    userdiaryclick.ShowDialog();
}

我設法弄清楚了答案。 謝謝

你想要實現的應該是這樣的

private void metroDataGrid1_CellClick(object sender, DataGridViewCellEventArgs e) 
{

//IF Statement to see if cell contains anything, if it does then open a new form below...
         if (metroDataGrid1.CurrentCell != null && metroDataGrid1.CurrentCell.Value != null)
         {
             var cellValue = metroDataGrid1.CurrentCell.Value.ToString();
             frmUserDiary userdiaryclick = new frmUserDiary(); 
             userdiaryclick.ShowDialog();
         }
         else   
         { 
            // do something
         }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM