简体   繁体   中英

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

I have a 3X3 grid which on creation has empty(has no values) cells.

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

        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

The following shows a message box of where you were and the cell contents( 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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