Thursday, 8 December 2011

How to make a Event Function of Drop Down List which is Inside the GridView.

Some time a user faced the Problem that how to make event function of Drop Down list and TextBox Events function which are inside the Gridview. And how the Drop Down list Control Finds the Row index. For this Solution It can be done by given blow Function.

Protected Sub DdlWeight_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim master As DropDownList = sender
        Dim DdlWeigh1 As String
        Dim oldAmount As Double
        Dim diff As Double
        Dim qty1 As String
        Dim GdvPrice As GridViewRow = master.Parent.Parent

'the Given below Lines Important to find Control in 'a same rowindex. It will Automatically run even 'function of same row index in which Drop Down list 'exist not another row Control. As Given Below:

        Dim Price As Label = GdvPrice.FindControl("ProdPrice")

        Dim QTY As TextBox = GdvPrice.FindControl("txtqty")

        Dim DdlWeight As DropDownList = GdvPrice.FindControl("DdlWeight")

        Dim Amount As Label = GdvPrice.FindControl("lblAmount")

        oldAmount = Val(Amount.Text)
        If DdlWeight.SelectedValue <> 0 Then
            qty1 = QTY.Text
            DdlWeigh1 = DdlWeight.SelectedItem.Text

            Dim GmKG As Array
            GmKG = Split(DdlWeigh1, " ")
            If GmKG(1) = "GM" Then
                Amount.Text = ((Val(Price.Text) / 1000) * (Val(GmKG(0)) * Val(qty1))).ToString()
            ElseIf GmKG(1) = "KG" Then
                Amount.Text = (Val(Price.Text) * (Val(GmKG(0)) * Val(qty1))).ToString()
            End If

            If oldAmount < Convert.ToDouble(Amount.Text) Then
                diff = Convert.ToDouble(Amount.Text) - oldAmount
                LblTotalAmt.Text = (diff + Convert.ToDouble(Val(LblTotalAmt.Text))).ToString()
                diff = oldAmount - Convert.ToDouble(Amount.Text)
                LblTotalAmt.Text = (Convert.ToDouble(LblTotalAmt.Text) - diff).ToString()
            End If

            Amount.Text = "0"
            diff = oldAmount - Convert.ToDouble(Amount.Text)
            LblTotalAmt.Text = (Convert.ToDouble(LblTotalAmt.Text) - diff).ToString()

        End If
    End Sub

No comments:

Post a Comment