views:

408

answers:

1

I have a code:

foreach (GridViewRow dr in gvCategories.Rows)<br/>
{  
   <br/>
   if (dr.Cells[0].Text == txtEnterCategory.Text.Trim())<br/>
   <br/>
    isError=true;
   <br/>
   <br/> 
}

Debugging: dr.Cells[0].Text is always "", even there are records. How to use a loop to check each row to find if a record exists in the gridview not using sql?

More code:

in .ascx:

<.Columns> TemplateField HeaderText="Category Name" SortExpression="Category"> ' OnTextChanged="TextBox_TextChanged"> ' OnTextChanged="TextBox_TextChanged">

        </asp:TemplateField>
        <asp:TemplateField HeaderText="In Footer?" SortExpression="ShowInFooter">
            <ItemTemplate>
                <asp:Label ID="lblShowInFooter" runat="server" 
                    Text='<%# (bool)Eval("ShowInFooter") ? "Yes" : "No" %>'></asp:Label>
            </ItemTemplate>
          <EditItemTemplate>
            <asp:CheckBox ID="lblShowInFooter" runat="server"
              Checked='<%# (bool)Eval("ShowInFooter")%>'>
            </asp:CheckBox>

          </EditItemTemplate>

        </asp:TemplateField>


         <asp:CommandField ShowCancelButton="true" ShowDeleteButton="true" ShowEditButton="true" CausesValidation="false" ItemStyle-CssClass="commandfield" />
    </Columns>

in ascx.cs:

bool isError = false;

    foreach (GridViewRow dr in gvCategories.Rows)
            {
                if (dr.Cells[0].Text == txtEnterCategory.Text.Trim())
                {
                    lblErrorMessage.Text = "This category already exits. Please enter a new category!";
                    lblErrorMessage.Visible = true;

                    isError=true;

                }


            }
+1  A: 

Since you've got a TemplateField containing a Label in the leftmost column, rather than a BoundField, you get the value out like this:

foreach (GridViewRow dr in gvCategories.Rows)
{
    Label l = (Label)dr.Cells[0].Controls[1];

    if (l.Text.Trim() == txtEnterCategory.Text.Trim())
    {
        lblErrorMessage.Text = "This category already exits. Please enter a new category!";
        lblErrorMessage.Visible = true;

        isError=true;
    }

Don't ask me why it's Controls[1], but it is. Every control in a TemplateField's ItemTemplate seems to create a pair of controls, so for example, if you had this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="txtFoo" runat="server" />
        <asp:TextBox ID="txtBar" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

txtFoo would be at Controls[1] and txtBar would be at Controls[3].

Cory Grimster
well, it's label actually, which turns to texbox when u click EDIT. So now I get casting error:Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox'.
Stewie Griffin
Try Label l = (Label)dr.Cells[0].Controls[1]; instead?
Cory Grimster
This worked ! Thanks ! get points ;)
Stewie Griffin
Great, I'm glad it worked :) I'll edit the answer to reflect the fact that you've got a Label in there instead of a TextBox.
Cory Grimster