Wrox Programmer Forums
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 28th, 2009, 01:58 AM
Authorized User
 
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
Question Looping through GridView

Hi All

I am developing a web appln using VS 2005(using C# as a language) and SQL Server 2005. I have a gridview in my page with add, edit , delete,update button in it. In the footer row i have a dropdown with the following list items: Self, Mother, Father,Spouse,Daughter and Son. Only son and daughter can be selected more than once here. On Clicking on add, i am saving values into viewstate, same for edit/update/delete. only on clicking on submit button, i am inserting into database.

I want to give an alert to the user if the grid contains rows with multiple Self,Mother,Father and Spouse.
There is a submit button outside the grid and on this event i wanna restrict user.

Code:
protectedvoid btnSubmit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gridRelation.Rows)
{
/// code needed
}
}
 


design page code:
Code:
<asp:GridViewID="gridRelation"Width="100%"CellPadding="2"CellSpacing="1"BorderWidth="0"BackColor="#c1c1c1"
runat="server"AutoGenerateColumns="false"
ShowFooter="true"CssClass="textbox"FooterStyle-BackColor="#ffffff"HeaderStyle-BackColor="#e1e1e1"
AllowPaging="false"PageSize="10"EmptyDataRowStyle-ForeColor="Red"
OnRowCommand="gridRelation_RowCommand"
OnRowDataBound="gridRelation_RowDataBound"
OnRowDeleting="gridRelation_RowDeleting"
OnRowUpdating="gridRelation_RowUpdating"
OnRowEditing="gridRelation_RowEditing"
OnRowCancelingEdit="gridRelation_RowCancelingEdit">
<Columns>
<asp:TemplateFieldAccessibleHeaderText="SlNo"HeaderText="SlNo"HeaderStyle-HorizontalAlign="center">
<ItemStyleHorizontalAlign="center"BackColor="#ffffff"/>
<ItemTemplate> 
<%# Container.DataItemIndex + 1 %>

</ItemTemplate>
<FooterTemplate>
<asp:HiddenFieldID="hfSlno"runat="server"Value="0"/>
</FooterTemplate>
<FooterStyleHorizontalAlign="center"BackColor="#ffffff"/>
<HeaderStyleBackColor="#e1e1e1"/>
</asp:TemplateField>
<asp:TemplateFieldAccessibleHeaderText="Relation"HeaderText="Relation"HeaderStyle-HorizontalAlign="center">
<ItemStyleHorizontalAlign="center"BackColor="#ffffff"/><ItemTemplate>
<asp:LabelID="lblRelation"runat="server"Text='<%# DataBinder.Eval(Container.DataItem, "RelationType")%>'/>
</ItemTemplate>
<HeaderStyleBackColor="#e1e1e1"/>
<EditItemTemplate>
<asp:DropDownListID="ddlERelation"runat="server"CssClass="textbox">
<asp:ListItemText="Select"Value="Select"></asp:ListItem>
<asp:ListItemText="Self"Value="Self"></asp:ListItem>
<asp:ListItemText="Mother"Value="Mother"></asp:ListItem>
<asp:ListItemText="Father"Value="Father"></asp:ListItem>
<asp:ListItemText="Spouse"Value="Spouse"></asp:ListItem>
<asp:ListItemText="Daughter"Value="Daughter"></asp:ListItem>
<asp:ListItemText="Son"Value="Son"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownListID="ddlFRelation"runat="server"CssClass="textbox">
<asp:ListItemText="Select"Value="Select"></asp:ListItem>
<asp:ListItemText="Self"Value="Self"></asp:ListItem>
<asp:ListItemText="Mother"Value="Mother"></asp:ListItem>
<asp:ListItemText="Father"Value="Father"></asp:ListItem>
<asp:ListItemText="Spouse"Value="Spouse"></asp:ListItem>
<asp:ListItemText="Daughter"Value="Daughter"></asp:ListItem>
<asp:ListItemText="Son"Value="Son"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<HeaderStyleBackColor="#e1e1e1"/>
<ItemStyle/>
<FooterStyleHorizontalAlign="center"BackColor="#ffffff"/>
</asp:TemplateField>
<asp:TemplateFieldAccessibleHeaderText="Name"HeaderText="Name"HeaderStyle-HorizontalAlign="center">
<ItemStyleHorizontalAlign="center"BackColor="#ffffff"/><ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBoxID="txtEName"MaxLength="30"runat="server"CssClass="textbox"Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBoxID="txtFName"runat="server"CssClass="textbox"MaxLength="30"></asp:TextBox></FooterTemplate>
<HeaderStyleBackColor="#e1e1e1"/>
<ItemStyle/>
<FooterStyleHorizontalAlign="center"BackColor="#ffffff"/>
</asp:TemplateField>
<asp:TemplateFieldHeaderStyle-Width="15%">
<ItemStyleBackColor="#ffffff"/>
<EditItemTemplate><asp:ButtonID="bttUpdate"runat="server"CssClass="textboxandbutt"CommandName="Update"Text="Update"/>&nbsp;
<asp:ButtonID="bttCancel"runat="server"CssClass="textboxandbutt"CommandName="Cancel"Text="Cancel"/>
</EditItemTemplate>
<ItemTemplate>
<asp:ButtonID="bttEdit"runat="server"CssClass="textboxandbutt"CommandName="Edit"Text="Edit"/>&nbsp;
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldHeaderStyle-Width="10%">
<ItemStyleBackColor="#ffffff"/>
<ItemTemplate>
<asp:ButtonID="bttDel"runat="server"CssClass="textboxandbutt"CommandName="Delete"Text="Delete"/>
</ItemTemplate>
<FooterTemplate>
<asp:ButtonID="bttAdd"runat="server"CssClass="textboxandbutt"CommandName="Add"Text="Add"/>
</FooterTemplate>
<FooterStyleBackColor="#FFFFFF"/>
</asp:TemplateField>
</Columns>
</asp:GridView>
how to handle this? Please throw light....
How to count the number of rows in gridview that contains a specific text. In My Gridview values are binded into label, this can be clearly seen from the code.

-- Abhishek

Last edited by abhishekkashyap27; March 28th, 2009 at 03:02 AM..
 
Old March 29th, 2009, 06:36 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

The GridViewRow is a normal control so you use FindControl to grab the label and check the value. Then just keep track of if you have already found a Mother and now have another one, same for Father etc.
c# Code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
  // nothing found so far
  bool hasMother = false;
  bool hasFather = false;
  bool hasSelf = false;
  bool hasSpouse = false;

  foreach (GridViewRow row in gridRelation.Rows)
  {
    Label relLabel = (Label)row.FindControl("lblRelation");
    // check exists to discount header rows etc
    if(relLabel != null)
    {
      // get the value
      string relation = relLabel.Text;

      // if we already have a relation, this is the second - error!
      if((relation == "Mother") && hasMother)
        throw new Exception("Too many Mothers");
      else
        hasMother = true; // set as found

      if((relation == "Father") && hasFather)
        throw new Exception("Too many Fathers");
      else
        hasFather = true;

      if((relation == "Self") && hasSelf)
        throw new Exception("Too many Selfs");
      else
        hasSelf = true;

      if((relation == "Spouse") && hasSpouse)
        throw new Exception("Too many Spouses");
      else
        hasSpouse = true;
    }
  }
}

To make it less wordy and more flexible, you could store the names and a count found in a Dictionary<string, int> and check the count at the end, but the principal is the same.

HTH
Phil
 
Old March 29th, 2009, 07:18 AM
Authorized User
 
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
Thumbs up

Thanks a lot Philip..., this is exactly what i was looking for....

-- Abhishek





Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Based on GridView Column stublair C# 2008 aka C# 3.0 0 September 4th, 2008 08:30 AM
Looping ssaranam SQL Server 2005 2 April 17th, 2008 01:40 AM
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
Looping..? dedex C# 2 January 6th, 2005 11:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.