edit gridview without using SqlDataSource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agarwalsunitadhn
    New Member
    • Jan 2008
    • 82

    edit gridview without using SqlDataSource

    I am creating gridview using connectionstrin g. i am passing different queries to bind grid as per condition. Now the problem I am facing on editing the grid. I am not able to use boundfield or template field because data coming from different table as per condition and number of field are also different.

    I can edit the data with the following code:

    Code:
     Public Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
            GridView1.EditIndex = e.NewEditIndex
            Call LoadGrid()
    
            Dim f1 As String = GridView1.Rows(e.NewEditIndex).Cells(1).Text
            Dim f2 As String = GridView1.Rows(e.NewEditIndex).Cells(2).Text
            Dim f3 As String = GridView1.Rows(e.NewEditIndex).Cells(3).Text
            lbl1.Text = f1
            lbl2.Text = f2
            lbl3.Text = f3
    
                  ''Dim dc As TextBox = GridView1.Rows(e.NewEditIndex).Cells(1).FindControl("bdistcode")
            ''lblDn.Text = dc.Text
        End Sub
    
        Public Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
            If (Session("sid").ToString() = "d" Or Session("sid").ToString() = "s" Or Session("sid").ToString() = "b") Then
                Dim f1 As String = GridView1.Rows(e.RowIndex).Cells(1).Text
                Dim f2 As String = GridView1.Rows(e.RowIndex).Cells(2).Text
                Dim f3 As String = GridView1.Rows(e.RowIndex).Cells(3).Text
                         Select Case (Session("sid").ToString())
                    Case "d"
                        constr = "update D set dc=@f1,dn_h=@f2 where dn=@f3"
                    Case "s"
                        constr = "update sd set sd_c=@f1,sd_nm=@f2 where id=@f3"
                    Case "b"
                        constr = "update bl set blc=@f1,bln=@f2 where id=@f3"
                End Select
                cmd = New SqlCommand(constr, con)
                Try
                    If con.State = ConnectionState.Closed Then con.Open()
    
                    cmd.Parameters.Clear()
                    cmd.Parameters.Add(New SqlParameter("@f1", SqlDbType.NVarChar)).Value = f1.ToString()
                    cmd.Parameters.Add(New SqlParameter("@f2", SqlDbType.NVarChar)).Value = f2.ToString()
                    If (Session("sid").ToString() = "d") Then
                        cmd.Parameters.Add(New SqlParameter("@f3", SqlDbType.VarChar)).Value = f3.ToString()
                    Else
                        cmd.Parameters.Add(New SqlParameter("@f3", SqlDbType.Int)).Value = Convert.ToInt32(f3)
                    End If
    
                    Dim rowno As Integer = cmd.ExecuteNonQuery()
                Catch ex As Exception
                    lblMessage.Text = lblMessage.Text & "Error" & ex.Message & "Field Value f1="
                    If con.State = ConnectionState.Open Then con.Close()
                Finally
                    If con.State = ConnectionState.Open Then con.Close()
                End Try
    
                'lblMessage.Text = "Record Updated Successfully"
                'lblMessage.Visible = True
    
                GridView1.EditIndex = -1
                Call LoadGrid()
            End If
    
           
        End Sub
    Last edited by Niheel; May 31 '10, 06:13 AM. Reason: spelling, punctuation, added code tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I think, if I were you, to keep things simple (KISS), I would use 1 GridView per data source.

    -Frinny

    Comment

    Working...