locked
How to fill empty datagridview cells (bound) with a text vb.net RRS feed

  • Question

  • Hello, I have a specific column in a bound datagridview that I need to check if there are blank cells and if there are, these blank cells will be filled with the text "EMPTY". Any ideas?
    Sunday, August 16, 2020 7:54 AM

All replies

  • If you mean null values, then consider the DataGridViewCellStyle.NullValue property.

    Or handle the DataGridView.CellFormatting event, analyse the argument, detect the column by name or index, check the value of e.Value, set the new value and e.FormattingApplied. Check the sample ShortFormDateFormat function: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.cellformatting.




    • Edited by Viorel_MVP Sunday, August 16, 2020 8:44 AM
    Sunday, August 16, 2020 8:41 AM
  • Can a single column have multiple data types? This column is a "Value" column. It can either have numbers or a text saying EMPTY
    Sunday, August 16, 2020 10:00 AM
  • You can "NullValue" for the column


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 16, 2020 3:52 PM
  • Hi Ash.22,

    You can refer to the following code to check the empty datagridview cell and set it's Text to "EMPTY".

            For Each rw As DataGridViewRow In Me.dataGridView1.Rows
    
                For i As Integer = 0 To rw.Cells.Count - 1
    
                    If rw.Cells(i).Value Is Nothing OrElse rw.Cells(i).Value = DBNull.Value OrElse String.IsNullOrWhiteSpace(rw.Cells(i).Value.ToString()) Then
                        rw.Cells(i).Value = "EMPTY"
                    End If
                Next
            Next

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 19, 2020 8:16 AM