Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
   Public Sub display_data()
        cn.Open()
        Dim dt As New DataTable("shakhes")
        Dim rs As New SqlDataAdapter("select * from shakhes", cn)
        rs.Fill(dt)
        ShakhesDataGridView.DataSource = dt
        ShakhesDataGridView.Refresh()
        Label1.Text = dt.Rows.Count
        rs.Dispose()
        cn.Close()
        ShakhesDataGridView.Columns.Item(0).HeaderText = "year"
        ShakhesDataGridView.Columns.Item(1).HeaderText = "month "
        ShakhesDataGridView.Columns.Item(2).HeaderText = "price"
    End Sub

    Public Sub get_data1()
        cn.Open()
        Dim dt As New DataTable("shakhes")
        Dim rs As New SqlDataAdapter("select * from shakhes where sal_tadieh = '" & TextBoxX4.Text & "' and mah_tadieh = '" & ComboBoxEx3.Text & "'", cn)
        rs.Fill(dt)
        ShakhesDataGridView.DataSource = dt
        ShakhesDataGridView.Refresh()
        Label1.Text = dt.Rows.Count
        rs.Dispose()
        cn.Close()
        If Val(Label1.Text) = 1 Then
            Dim i As Integer
            i = ShakhesDataGridView.CurrentRow.Index
            TextBoxX2.Text = ShakhesDataGridView.Item(2, i).Value
            If ShakhesDataGridView.Item(2, i).Value = 0 Then
                LabelX6.Text = "there is no data"
            Else
                LabelX6.Text = TextBoxX2.Text
            End If
        End If
    End Sub

    Public Sub get_data2()
        cn.Open()
        Dim dt As New DataTable("shakhes")
        Dim rs As New SqlDataAdapter("select * from shakhes where sal_tadieh = '" & TextBoxX4.Text & "' and mah_tadieh = '" & ComboBoxEx3.Text & "'", cn)
        rs.Fill(dt)
        ShakhesDataGridView.DataSource = dt
        ShakhesDataGridView.Refresh()
        Label1.Text = dt.Rows.Count
        rs.Dispose()
        cn.Close()
        If Val(Label1.Text) = 1 Then
            Dim i As Integer
            i = ShakhesDataGridView.CurrentRow.Index
            TextBoxX3.Text = ShakhesDataGridView.Item(2, i).Value
            If ShakhesDataGridView.Item(2, i).Value = 0 Then
                LabelX7.Text = "there is no data"
            Else
                LabelX7.Text = TextBoxX3.Text
            End If
        End If
    End Sub

Private Sub tadieh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'TODO: This line of code loads data into the 'Ejra_dbDataSet.shakhes' table. You can move, or remove it, as needed.
       Me.ShakhesTableAdapter.Fill(Me.Ejra_dbDataSet.shakhes)
       expo()
       display_data()
   End Sub

   Private Sub ComboBoxEx3_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxEx3.SelectedIndexChanged
       get_data1()
   End Sub

   Private Sub ComboBoxEx4_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxEx4.SelectedIndexChanged
       get_data2()
   End Sub

Private Sub ShakhesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Me.Validate()
       Me.ShakhesBindingSource.EndEdit()
       Me.TableAdapterManager.UpdateAll(Me.Ejra_dbDataSet)
   End Sub

Hi everybody
I make app vb.net that has one button and a data grid view and two combo box ex and two textboxes and two labels
I save some data in a database and I want when change items of combo box and text box the desirable data row showed in data grid view
The problem is that when current row index exist the label. Text show the desirable row but when current row index is not exist the before row showed in label.text but I want to changes label.text or message box or label. Text shows a message for user
What suggestion is there? And I must in which event write some codes?
My code that do not work is :
Posted
Updated 16-Aug-15 2:35am
v5
Comments
rezaeti 16-Aug-15 8:17am    
is there any one?
Patrice T 16-Aug-15 8:30am    
Learn patience, it is Sunday !
rezaeti 16-Aug-15 8:32am    
excuse me i forget that it is sunday beacuse in my country friday is HOLIDAY then I ask my question tomarrow
Patrice T 16-Aug-15 8:39am    
I suggest not ask again tomorrow.
Questions repost is considered bad behaviour.
Hours and days delay of wait are common for responses.
rezaeti 16-Aug-15 8:41am    
ok my firend

1 solution

Solution principle:

You have understood that when the row you looking for don't exist, you display wrong data.
The solution is quite simple, Check if actual row is the right one before displaying data or alert message.
Pseudo code:
VB
If (row found) Then
    display Data
Else
    display Alert message
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900