wsyeager
Well-known member
In my page_load event, I have the following code:
<code>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not Page.IsPostBack Then
BindGrid()
Else
GridView1.DataSource = DirectCast(Session.Item("dtCustomers"), DataTable)
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
</code>
While debugging, I have the following in the Immediate Window after the postback:
?GridView1.Rows.Count
10
Once I clicked the Edit button, it fired the RowEditing event which I have the following code:
<code>
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
GridView1.DataBind()
End Sub
</code>
Right at the End Sub, I had another breakpoint to examine the rows in the gridview to make sure they
were still there as follows:
?GridView1.Rows.Count
10
My html of the gridview looks like this:
<code>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="CustomerID" SortExpression="CustomerID">
<ItemTemplate>
<asp:HyperLink ID="hylCustomerID" runat="server" Target="_blank" Text=<%#
Bind("CustomerID") %>></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Region" HeaderText="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" />
</Columns>
</asp:GridView>
</code>
Inside the RowUpdating event, I have the following code (which was shortened for brevity):
<code>
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim dtCustomers As New DataTable
Try
Dim dr As DataRow = dtCustomers.NewRow
dr.Item("CustomerID") = GridView1.Rows(e.RowIndex).Cells(0).Text
dr.Item("CompanyName") = GridView1.Rows(e.RowIndex).Cells(1).Text
........
dtCustomers.Rows.Add(dr)
dtCustomers.Rows(0).SetModified()
Dim cls1 As New Class1
Dim intRecdsAffected As Int16 = cls1.UpdateCustomer(dtCustomers)
cls1 = Nothing
GridView1.EditIndex = -1
BindGrid()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
</code>
When I try to examine the following line of code in the above event
"GridView1.Rows(e.RowIndex).Cells(0).Text", I get an empty string. Note below:
?gridview1.Rows(e.RowIndex).Cells(1).Text
""
?gridview1.Rows(e.RowIndex).Cells(0).Text
""
?gridview1.Rows(e.RowIndex).Cells(2).Text
""
?gridview1.Rows(e.RowIndex).Cells(3).Text
""
?gridview1.Rows(e.RowIndex).Cells(4).Text
""
?gridview1.Rows(e.RowIndex).Cells(5).Text
""
?GridView1.Rows.Count
10
Notice that I still have the 10 records in the gridview count at this point. I am not using any
DataSource controls, so I cant use the "NewValues" property of the gridview.
How can I properly retrieve the values inside the grid at this point? It seems very simple, but I am
obviously missing something here.
<code>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not Page.IsPostBack Then
BindGrid()
Else
GridView1.DataSource = DirectCast(Session.Item("dtCustomers"), DataTable)
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
</code>
While debugging, I have the following in the Immediate Window after the postback:
?GridView1.Rows.Count
10
Once I clicked the Edit button, it fired the RowEditing event which I have the following code:
<code>
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
GridView1.DataBind()
End Sub
</code>
Right at the End Sub, I had another breakpoint to examine the rows in the gridview to make sure they
were still there as follows:
?GridView1.Rows.Count
10
My html of the gridview looks like this:
<code>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="CustomerID" SortExpression="CustomerID">
<ItemTemplate>
<asp:HyperLink ID="hylCustomerID" runat="server" Target="_blank" Text=<%#
Bind("CustomerID") %>></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Region" HeaderText="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" />
</Columns>
</asp:GridView>
</code>
Inside the RowUpdating event, I have the following code (which was shortened for brevity):
<code>
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim dtCustomers As New DataTable
Try
Dim dr As DataRow = dtCustomers.NewRow
dr.Item("CustomerID") = GridView1.Rows(e.RowIndex).Cells(0).Text
dr.Item("CompanyName") = GridView1.Rows(e.RowIndex).Cells(1).Text
........
dtCustomers.Rows.Add(dr)
dtCustomers.Rows(0).SetModified()
Dim cls1 As New Class1
Dim intRecdsAffected As Int16 = cls1.UpdateCustomer(dtCustomers)
cls1 = Nothing
GridView1.EditIndex = -1
BindGrid()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
</code>
When I try to examine the following line of code in the above event
"GridView1.Rows(e.RowIndex).Cells(0).Text", I get an empty string. Note below:
?gridview1.Rows(e.RowIndex).Cells(1).Text
""
?gridview1.Rows(e.RowIndex).Cells(0).Text
""
?gridview1.Rows(e.RowIndex).Cells(2).Text
""
?gridview1.Rows(e.RowIndex).Cells(3).Text
""
?gridview1.Rows(e.RowIndex).Cells(4).Text
""
?gridview1.Rows(e.RowIndex).Cells(5).Text
""
?GridView1.Rows.Count
10
Notice that I still have the 10 records in the gridview count at this point. I am not using any
DataSource controls, so I cant use the "NewValues" property of the gridview.
How can I properly retrieve the values inside the grid at this point? It seems very simple, but I am
obviously missing something here.