简体   繁体   English

检查DatagridView是否为空(没有值)?

[英]Check DatagridView is empty(has no values) or not?

I have a 3X3 grid which on creation has empty(has no values) cells. 我有一个3X3网格,该网格在创建时具有空(无值)单元格。

I want to check whether user has key-in some some data into the cell's or not. 我想检查用户是否将一些数据键入到单元格中。

Its not binded to any data-source. 它没有绑定到任何数据源。 I don't want to iterate through each cell and check for the data and break on the first data found. 我不想遍历每个单元格并检查数据并中断找到的第一个数据。

I want some small solution for the same. 我想要一些小的解决方案。 As same thing has to be done on many forms. 同样,必须在许多形式上执行相同的操作。 I will make some generic routine or extension method for the same. 我将为此做一些通用的例程或扩展方法。

PS: What I have is a grid with three paramter PS:我有一个带有三个参数的网格

        ParamA        ParamB         ParamC
Short
Medium
Long

when user fill any of the data. 当用户填写任何数据时。 I have to add it to a collection. 我必须将其添加到集合中。 If no data is key inned then do nothing. 如果没有数据是关键,则什么也不做。

Consider using the KeyPress event or perhaps even better would be the CellEndEdit on MSDN 考虑使用KeyPress事件,或者甚至更好的方法是使用MSDN上的CellEndEdit

The following shows a message box of where you were and the cell contents( Correction ): 下面显示了您所在的位置以及单元格内容的消息框( Correction ):


    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {

        string message = string.Format("Cell End Edit: just left row: {0} and column {1}.\n Value entered:{2}", e.RowIndex, e.ColumnIndex,
                                       dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString());
        MessageBox.Show(this, message, "You were here");

    }

For reuse you may create your own user control or create a base form class. 为了重用,您可以创建自己的用户控件或创建基本表单类。

您可以创建一个整数值EmptyFieldsCount ,并在每次用户更新单元格时对其进行更新。

With 1 column is check box if checked is true then: 带有1列的复选框是否为true,则:

 Dim dgv1 As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
      Dim headerText As String = DataGridView1.Columns(e.ColumnIndex).HeaderText
      If dgv1.Cells(0).Value = True Then
        If Not headerText.Equals("Short Desc") Then Return
        If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
          DataGridView1.Rows(e.RowIndex).ErrorText = "Short Desc Can not be blank ! "
          e.Cancel = True
        End If
      End If

Without checkbox: 没有复选框:

 Dim dgv1 As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
      Dim headerText As String = DataGridView1.Columns(e.ColumnIndex).HeaderText
        If Not headerText.Equals("Short Desc") Then Return
        If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
          DataGridView1.Rows(e.RowIndex).ErrorText = "Short Desc Can not be blank ! "
          e.Cancel = True
      End If**

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

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