Hello,

I am new in VB2008. Could you guys please advice how I can programmtically delete/remove/clear the content of a cell of DataGridView?

Say,for example,I have the following code that populates with data in DataGridView. Now,how can I clear/delete the "Test4" text from the cell of DataGridView? The GridView is not bound to any database.

Any advice would be appreaciated.

Thank you.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        dt.Columns.Add("income")
        dt.Columns.Add("income1")
        dt.Columns.Add("sum")
        dt.Rows.Add("Test1")
        dt.Rows.Add("Test2", "Test3")
        dt.Rows.Add("", "Test4")

        Me.DataGridView1.DataSource = dt
    End Sub
End Class

Edited 4 Years Ago by elitely

if u want a clear cell that use "" instead of Test4...

It will be good enough if u be more specific....n number of people will be able to help u....

Hello,thanks for your response. I just want to remove the text of a cell of DataGrid looping through a column. That is all there is to it. For example,I am using the following code to loop through the GridView and if I I get the text "Test4",I want to remove it from the cell of the GridView :

Public Class Form1


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("income")
            dt.Columns.Add("income1")
            dt.Columns.Add("sum")
            dt.Rows.Add("Test1", "Test")
            dt.Rows.Add("Test2", "Test3")
            dt.Rows.Add("", "Test4")
            Me.DataGridView1.DataSource = dt
        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim celldata As String

            For Each r As DataGridViewRow In Me.DataGridView1.Rows

                celldata = r.Cells(1).Value
                MessageBox.Show(celldata)
            Next
        End Sub

    End Class

You can access individual cells in a datagridview via the rows and cells properties. For example, to clear the cell at (2,2) you would do

DataGridView1.Rows(2).Cells(2).Value = ""

Hi,thanks for your response,guys. I appreciate it. I solved the problem with the following code :

'This removes the text from the cell when found

    Dim x As Integer = 0
    While x < DataGridView1.Rows.Count
        Dim y As Integer = 0
        While y < DataGridView1.Rows(x).Cells.Count
            Dim c As DataGridViewCell = DataGridView1.Rows(x).Cells(y)
            If Not c.Value Is DBNull.Value Or Nothing Then
                If CType(c.Value, String) = "Test4" Then
                    DataGridView1.Rows(x).Cells(1).Value = ""
                End If
            End If
            System.Math.Min(System.Threading.Interlocked.Increment(y), y - 1)
        End While
        System.Math.Min(System.Threading.Interlocked.Increment(x), x - 1)
    End While
    Label1.Text = ("Deleted!!")
This question has already been answered. Start a new discussion instead.