开发者

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.


You can create a integer value EmptyFieldsCount, and update it every time the user updates a cell.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜