I have created a gridview that is dynamic and binds the grid dynamically based on a table selected in a dropdown. I have worked through most of the issues like dynamically creating the update, delete statements etc except for one problem I'm having in retrieving the cell value while in the rowupdating event. I can retrieve it every where else; but not in this event. I am not setting up an item template or assigning names so as to keep the process very dynamic where it can be run on one of 25 tables selected all with different fields where I only define the fields in the select, update, and delete statements with generate fields=true.
The code follows but GridView1.Rows(rowIndex).Cells(i).Text is always empty if your on the row being updated. And this e.NewValues(0).ToString() is always an error so I am unable to get the value to plug into the dynamic update statement.
If you on a different row; no problem it will return the value.
Thanks for any help.
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Public Sub BindGridView()
Try
Dim connection As Data.SqlClient.SqlConnection
Dim sql As String
Dim begin As Integer = 0
connection = New Data.SqlClient.SqlConnection("server=USG1\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Test")
connection.Open()
Dim command2 As SqlCommand = New SqlCommand("Select * from Keys where TableName='" & "Country_Table" & "'")
Dim reader As Data.SqlClient.SqlDataReader
Dim da As New Data.SqlClient.SqlDataAdapter()
Dim dt As New DataTable()
Dim cb As Data.SqlClient.SqlCommandBuilder
command2.Connection = connection
da.SelectCommand = command2
da.Fill(dt)
cb = New Data.SqlClient.SqlCommandBuilder(da)
reader = da.SelectCommand.ExecuteReader()
sql = "Select Top 10"
While reader.Read()
If begin = 1 Then
sql = sql & "+"
End If
If Trim(reader(2).ToString()) = "int" Then
sql = sql & " " & Trim(reader(1).ToString()) & " "
Else
sql = sql & " [" & Trim(reader(1).ToString()) & "] "
End If
Try
Catch exc As Exception
End Try
begin = 1
End While
sql = sql & " as ID, * from " & "Country_Table"
reader.Close()
Dim command As SqlCommand = New SqlCommand(sql, connection)
command.Connection = connection
Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(command)
Dim dataSource As DataSet = New DataSet()
dataAdapter.Fill(dataSource)
GridView1.DataSource = dataSource.Tables(0)
GridView1.DataBind()
Catch ex As Exception
Throw ex
Finally
If GridView1.Rows.Count = 0 Then
MsgBox("No records to show!")
End If
End Try
RowUpdating
Dim rowIndex As Integer = e.RowIndex
Dim row As GridViewRow = GridView1.Rows(rowIndex)
sql = sql & 'FieldA'
sql = sql & "=" & GridView1.Rows(rowIndex).Cells(i).Text
ASKER