Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one
I have asp.net c# website I have on my form:
1. sqldatasource1
2.gridview1
3.textbox1(for input a year) and dropdownlist1(for select a month)
4.textbox2(for input a year) and dropdownlist2(for select a month)
5.two label
6.I want when I select a month and year, the specific data in gridview1 display in label1 and label2
7.then I want to calculate that data e.g "+" or "*" or ...
8. I do this in vb.net my code for get data is :

 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
            ElseIf Val(Label1.Text) = 0 Then
                TextBoxX2.Text = "there is no data"
            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 = '" & TextBoxX5.Text & "' and mah_tadieh = '" & ComboBoxEx4.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
                 ElseIf Val(Label1.Text) = 0 Then
                TextBoxX3.Text = "there is no data"
            End If
        End Sub


9. my code for calculate data:



     Public Sub boro()
            Dim a As Decimal
            Dim b, c As Decimal
            Dim fo_labl As Decimal
            a = TextBoxX1.Text
            b = Val(TextBoxX2.Text)
            c = Val(TextBoxX3.Text)
            If b = 0 Or c = 0 Then
                MessageBox.Show("no data", "notice", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                LabelX5.Text = (c / b) * a
            End If
            fo_labl = Val(LabelX5.Text)
            LabelX5.Text = FormatNumber(fo_labl, NumDigitsAfterDecimal:=0, GroupDigits:=TriState.True)
        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()
            LabelX6.Text = TextBoxX2.Text
        End Sub

        Private Sub ComboBoxEx4_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxEx4.SelectedIndexChanged
            get_data2()
            LabelX7.Text = TextBoxX3.Text
        End Sub
        Private Sub ButtonX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX3.Click
            Me.Close()
        End Sub

        Private Sub TextBoxX1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxX1.KeyPress
            e.Handled = Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 8)
        End Sub

        Private Sub TextBoxX1_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxX1.TextChanged
            Try
                TextBoxX1.Text = Format(CULng(TextBoxX1.Text), "#,#")
                TextBoxX1.SelectionStart = TextBoxX1.Text.Length
            Catch ex As Exception

            End Try
        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

        Private Sub TextBoxX4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxX4.TextChanged
            get_data1()
            LabelX6.Text = TextBoxX2.Text
        End Sub

        Private Sub TextBoxX5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxX5.TextChanged
            get_data2()
            LabelX7.Text = TextBoxX3.Text
        End Sub


        Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
            If TextBoxX1.Text = "" Then
                MessageBox.Show("please fill", "notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            ElseIf TextBoxX4.Text = "" Then
                MessageBox.Show("please fill", "notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            ElseIf TextBoxX5.Text = "" Then
                MessageBox.Show("please fill", "notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            ElseIf ComboBoxEx3.Text = "" Then
                MessageBox.Show("please fill", "notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            ElseIf ComboBoxEx4.Text = "" Then
                MessageBox.Show("please fill", "notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
           Else
                boro()
            End If
        End Sub
    End Class

10. now I want do same thing for asp.net but my problem is that gridview dose not have same property e.g 
 i = ShakhesDataGridView.CurrentRow.Index
                TextBoxX2.Text = ShakhesDataGridView.Item(2, i).Value


11.by the way i know about asp.net and c# a liitle and i am an amateur
Posted
Updated 2-Oct-15 1:35am
v3
Comments
Richard Deeming 2-Oct-15 8:46am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
ZurdoDev 2-Oct-15 9:15am    
Where are you stuck?
rezaeti 2-Oct-15 17:16pm    
10. now I want do same thing for asp.net but my problem is that gridview dose not have same property e.g
i = ShakhesDataGridView.CurrentRow.Index
TextBoxX2.Text = ShakhesDataGridView.Item(2, i).Value


11.by the way i know about asp.net and c# a liitle and i am an amateur

1 solution

I think you are looking for GridView.SelectedIndexChanged Event[^].
C#
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
 
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