Compare the values of an entire column of Datagridview with a variable

Asked

Viewed 1,600 times

3

I would like to compare the value of a string type variable with an entire column of Datagridview.

There is a code that looks at the whole column, more or less like this

textbox.Text = datagridview.columns("nome_da_coluna").value

or I will have to create a loop for this query?

Dim sqlCmd As String = "SELECT COLUNA1, COLUNA2  FROM nome_da_tabela "
cnn = New SqlConnection(strCon)
cmd = New SqlCommand(sqlCmd, cnn)
Dim adpt As New SqlDataAdapter(sqlCmd, cnn)
Dim dts As New DataSet()

Try
    cnn.Open()

    adpt.Fill(dts, "nome_da_tabela")
    datagridview.DataSource = dts

2 answers

3

You will need to implement a loop, as your need is specific. The implementation of Datagridview provides the necessary operations for row-to-row X column-to-column manipulation.

In short the ideal alternative is you extend Datagridview and supply your need offering the desired implementation as a new method in the molds you suggested, let’s see:

seuDataDridView.columns("nome_da_coluna").value

However, I believe that storing the contents of all cells in this column is not a good practice, and may even cause memory overflow if there is too much data.

  • Fábio, good afternoon. Thank you for your information but I would like you to put an example. I know that youDatagridView.Columns("table field"). value refers to the value of the field but if you put it in the code: textbox.text = seuDatagridView.Columns("table field"). value the textbox.text does not accept the value of the datagridview field.

  • user2979215 "seuDatagridView.Columns("table field"). value" does not have the value attribute. I proposed that you implement it. What you really need is to concatenate the value of all cells in that column and assign it to the textbox.text?

0

A possibility is in itself SQL have a field with concatenation of all column values, for example using the FOR XML:

SELECT (SELECT COLUNA1 + ';' + COLUNA2 FROM nome_da_tabela FOR XML PATH(''))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.