I am using Gridview and on that data is binded conditionally.t he problem is that at the time of editing whole row of the grid is in editable mode and i want only few of the columns editable. my code is as follows
Code:
Private Sub LoadGrid()
Select Case (Session("sid").ToString())
Case "d"
constr = "select dc ,dnh,dn from dst order by dc"
Case "s"
constr = "select sc,sn,id from sd order by sc"
End Select
cmd = New SqlCommand
cmd.Parameters.Clear()
Try
If con.State = ConnectionState.Closed Then con.Open()
cmd.CommandText = constr
cmd.Connection = con
Myreader = cmd.ExecuteReader()
Me.GridView1.DataSource = Myreader
Me.GridView1.DataBind()
If (Session("sid").ToString() = "d" Or Session("sid").ToString() = "s" ) Then
UndideGrid(3)
End If
Catch ex As Exception
If con.State = ConnectionState.Open Then con.Close()
Finally
If con.State = ConnectionState.Open Then con.Close()
End Try
End Sub
Public Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
Call LoadGrid()
lblMessage.Text = ""
End Sub
Public Sub UndideGrid(ByVal x As Integer)
If (GridView1.Columns.Count > 0) Then
GridView1.Columns(x).Visible = False
Else
GridView1.HeaderRow.Cells(x).Visible = False
Dim gvr As GridViewRow
For Each gvr In GridView1.Rows
gvr.Cells(x).Visible = False
Next
End If
End Sub
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()
End Sub
Public Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim idx As Integer = e.RowIndex
Dim row1 As GridViewRow = GridView1.Rows(idx)
If (Session("sid").ToString() = "d" Or Session("sid").ToString() = "s") Then
Dim f1 As TextBox = CType(row1.Cells(1).Controls(0), TextBox)
Dim f2 As TextBox = CType(row1.Cells(2).Controls(0), TextBox)
Dim f3 As TextBox = CType(row1.Cells(3).Controls(0), TextBox)
lblMessage.Text = "Field Value f1=" & f1.Text & " f2=" & f2.Text & " f3=" & f3.Text
Select Case (Session("sid").ToString())
Case "d"
constr = "update Dst set dnh=@f2 where dc=@f1"
Case "s"
constr = "update sd set sn=@f2 where sc=@f1 and 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("@f2", SqlDbType.NVarChar)).Value = f2.Text
cmd.Parameters.Add(New SqlParameter("@f1", SqlDbType.NVarChar)).Value = f1.Text
If (Session("sid").ToString() = "d") Then
Else
cmd.Parameters.Add(New SqlParameter("@f3", SqlDbType.Int)).Value = Convert.ToInt32(f3.Text)
End If
Dim rowno As Integer = cmd.ExecuteNonQuery()
Catch ex As Exception
lblMessage.Text = lblMessage.Text & "Error" & ex.Message
If con.State = ConnectionState.Open Then con.Close()
Finally
If con.State = ConnectionState.Open Then con.Close()
End Try
GridView1.EditIndex = -1
Call LoadGrid()
End Sub
Protected Sub rdDist_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rdDist.CheckedChanged
Label18.Text = "District Details"
Session("sid") = "d"
Call LoadGrid()
'Call LoadGrid()
End Sub
Protected Sub rdSubDiv_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rdSubDiv.CheckedChanged
Label18.Text = "SubDivision Details"
Session("sid") = "s"
Call LoadGrid()
lblMessage.Text = ""
End Sub
End Class
Comment