I have a function which loops through all the rows of my GridView and compares some of the cell values with a value the user wants to insert into the table. The point is to make sure the value they want to insert is not a duplicate value.
This function was working fine until I recently made most of the columns into EditItemTemplate controls so that I could access them on update.
Now, depending on how I try to do it, I either get a blank string or I get a "Object reference not set to an instance of an object" error.
Here's how I was originally finding the specific cell (which now apparently does nothing):
GridView1.Rows[rowIndex].Cells[2].Text
And here's how I'm doing it now (but which gives me the reference error):
GridView1.FindControl("storeNumberTB").ToString()
I suspect the issue is that I'm referencing a TextBox control within EditItemTemplate rather than just the label(?) which is what I presumably need to look at as I loop through the rows. But I can't figure out how to access the value.
So I just need to get whatever text is at a particular cell on every row as it loops through.
Here's the entire function as it is now in case that helps:
foreach (GridViewRow row in GridView1.Rows)
{
if (GridView1.FindControl("storeNumberTB").ToString() == userInputValue)
{
duplicate++;
}
}
Here's the relevant part of my ASPX code for the GridView:
<asp:TemplateField HeaderText="Store">
<ItemTemplate >
<asp:Label runat="server" Text='<%# Eval("Store") %>' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="storeNumberTB" runat="server" Text='
<%# Eval("Store")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
UPDATE: row.Cells[3].Text returns an empty string. No clue why. Any idea how to access the value in the cell in my case?