DataGridView and SQL Selection
Dear firends,
I want to put the value "Da" to all the Cells of the CurrentRow where the "Cell(5).Value=SOMETHING" and the "ColumnHeaderText=SOMETHING" it's found.
Code:
For Each rand As DataGridViewRow In Me.dgv_lista_repere.Rows
For Each coloana As DataGridViewColumn In dgv_lista_repere.Columns
Try
conectare_db()
Dim rezultat As String = "SELECT lista_indicatori_cursanti.cnp, nomenclator_indicatori.denumire FROM lista_indicatori_cursanti, nomenclator_indicatori WHERE nomenclator_indicatori.denumire=(SELECT nomenclator_indicatori.denumire FROM nomenclator_indicatori WHERE nomenclator_indicatori.id=lista_indicatori_cursanti.id_indicator AND lista_indicatori_cursanti.cnp='" & rand.Cells(5).Value & "');"
command_exec = New SqlClient.SqlCommand(rezultat, conexiune)
data_reader = command_exec.ExecuteReader
If (data_reader.Read = True) Then
Dim cnp_cursant As String = data_reader(0)
Dim denumire_indicator As String = data_reader(1)
If rand.Cells(5).Value = cnp_cursant AndAlso coloana.HeaderText = denumire_indicator Then
rand.Cells(coloana.Index).Value = "Da"
End If
Else
End If
deconectare_db()
Catch ex As Exception
deconectare_db()
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Next coloana
Next rand
Re: DataGridView and SQL Selection
ALEX
it is difficult for me to understand your code as it is in a language other than english
i understood your requirement is this way.
u want to replace the value of the cell if the
Code:
Cell(5).Value = 'x'
and when the row is Current row that means selected
if right pl reply i can help u
Re: DataGridView and SQL Selection
Yes it's true English it's not my best Goal...
The problem is like that.
The current DataGridView contains data from 3 tables...
1. I want to read the value of the Cell(5) from the current Row (For Each Loop)
2. In the same time i want to read the every ColmnHeaderText (For Each Loop).
3. If the value of Cell(5) and the ColumnHeaderText it's found in the DataBase then to Write in the INTERSECTION Cell the value "Da" (alias Yes in English).
My problem with the current code:
If the condition is meet.... only the first cell si marked with "Da" and afert that is going to the next Row... it's not verifying all the Columns.
Re: DataGridView and SQL Selection
no one can help me with this ?
Re: DataGridView and SQL Selection
alex i was busy with my work & out of station for 3 days i will answer please give me some time
Re: DataGridView and SQL Selection
Alex cross check this & reply
here i have used a datagridview + button control & loaded some temp data in to datagrid view cells
& cross checked each value with some other value & made "DA"
vb Code:
Public Class alex
Private Sub alex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'LOAD TEMP DEMO VALUES
Dim I As Int16 = 0
Dim J As Int16 = Me.DataGridView1.Columns.Count
Dim k As Int16
Dim c As String = "C"
With Me.DataGridView1
.Rows.Add(10)
' i am adding some temp values here in to the cells
For k = 0 To J - 1
For I = 0 To 10
.Rows(I).Cells(k).Value = c & I
Next
Next
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'now cross check the values of column header
'& cell0 values (in your case cell5) with some other value
'if both values are equal then make cell(1) value as "DA"
Dim I, J As Int16
Dim ValueFoundinDatabase As String = "C1" 'ASSUME this is the comparison values u found from database
Dim ValueFoundinDatagridView_ColumnHeaderText As String = Me.DataGridView1.Columns(0).HeaderText
Dim ValueFoundinDatagridView_Cell5 As String
With Me.DataGridView1
J = .Rows.Count
For I = 0 To J - 1
'now DA making
ValueFoundinDatagridView_Cell5 = .Rows(I).Cells(0).Value
'here u need to check the Column header text + cell(5) value
'with database values
If ValueFoundinDatagridView_Cell5 = ValueFoundinDatabase AndAlso _
ValueFoundinDatagridView_ColumnHeaderText = ValueFoundinDatabase Then
.Rows(I).Cells(1).Value = "DA"
End If
Next
End With
End Sub
End Class