Craiten
Craiten

Reputation: 109

asp.net C# gridview count always zero

I created a button (called "SubmitButtonTournamentName") that fills a gridview (called "GridViewTournaments") with information from a sql database (after i clicked on the button). This gridview shows several data and includes two dropdownlists where I can choose information and a checkbox. Below the gridview is a submit button.

When I click on the button I like to have a validation. If I do not choose an option in one of the dropdown lists it should change its color. This is my button handler that catches the button clicks:

public void BtnHandler(Object sender, EventArgs e) {
        Button btn = (Button)sender;
        switch (btn.CommandName) {
        case "SubmitButtonTournamentName": {
           //fill GridViewTournaments
           // sql stuff...

           try {
                sqlConnection.Open();
                IDataReader dr = idbCommand.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                GridViewTournaments.DataSource = dt;
                GridViewTournaments.DataBind();
                dr.Close();
            } catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine("error: " + ex);
            } finally {
                sqlConnection.Close();
            }
        }
        break;

        case "btnSubmitEntry": {
            //Here I try to validate the input
            bool formError = false;
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5] { new DataColumn("tourn_name"), new DataColumn("start_date"), new DataColumn("status"), new DataColumn("choice"), new DataColumn("doublePartner") });

            int count = GridViewTournaments.Rows.Count;
            System.Diagnostics.Debug.WriteLine("gridview count: " + count);
            foreach (GridViewRow row in GridViewTournaments.Rows) {
                if (row.RowType == DataControlRowType.DataRow) {
                    CheckBox chkEntry = (row.Cells[5].FindControl("chkEntry") as CheckBox);
                    if (chkEntry.Checked) {
                        DropDownList choice = (row.Cells[3].FindControl("ddlChoice") as DropDownList);
                        DropDownList doublePartner = (row.Cells[4].FindControl("ddlDoublePartner") as DropDownList);
                        if (choice.SelectedValue.Equals("-1")) {
                            formError = true;
                            choice.BackColor = Color.Red;
                        }
                        if (doublePartner.SelectedValue.Equals("-1")) {
                            formError = true;
                            doublePartner.BackColor = Color.Red;
                        }
                        string name = row.Cells[0].Text;
                        string startDate = row.Cells[1].Text;//(row.Cells[1].FindControl("lblCountry") as Label).Text;
                        string status = row.Cells[2].Text;
                        dt.Rows.Add(name, startDate, status, choice, doublePartner);
                    }
                }
            }
            if (formError == false) {
                 Server.Transfer("ProccessEntry.aspx", true);
            }
            GridViewTournaments.DataSource = dt;
            GridViewTournaments.DataBind();
        }
        break;
        }
    }

In case "btnSubmitEntry" is clicked I try to get the date from the gridview, change the color and rewrite the gridview with the changed elements.

This is my form:

 <asp:GridView ID="GridViewTournaments" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
            runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="tourn_name" HeaderText="Name" ItemStyle-Width="150" />
                <asp:BoundField DataField="start_date" HeaderText="Start date" ItemStyle-Width="150" />
                <asp:BoundField DataField="status" HeaderText="Status" ItemStyle-Width="150" />
                <asp:TemplateField HeaderText="choice">
                    <ItemTemplate>
                        <asp:Label ID="lblChoice" runat="server" Text='<%# Eval("choice") %>' Visible="false" />
                        <asp:DropDownList ID="ddlChoice" runat="server">
                            <asp:ListItem Text="Choose ..." Value="-1"></asp:ListItem>
                            <asp:ListItem Text="1st Choice" Value="1"></asp:ListItem>
                            <asp:ListItem Text="2nd Choice" Value="2"></asp:ListItem>
                            <asp:ListItem Text="3rd Choice" Value="3"></asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="doublePartner">
                    <ItemTemplate>
                        <asp:Label ID="lblDoublePartner" runat="server" Text='<%# Eval("doublePartner") %>' Visible="false" />
                        <asp:DropDownList ID="ddlDoublePartner" runat="server">
                            <asp:ListItem Text="Some name" Value="1"></asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Want to enter?">
                    <ItemTemplate>
                        <asp:Label ID="lblEnter" runat="server" Text='<%# Eval("enter") %>' Visible="false" />
                        <asp:CheckBox runat="server" ID="chkEntry" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnSubmitEntry" runat="server" Text="Submit" CommandName="btnSubmitEntry" OnClick="BtnHandler" />

The problem:

The foreach loop does not goes trough the gridview because it appears that there are zero rows in it.

I tested the code and this returns zero:

GridViewTournaments.Rows.Count

What I think is, that the "GridViewTournaments" is not binded the to page context so the button handler does not get the information from the gridview.

I know the button reloads the page but I like to get around the postback.

Maybe my solution is unconventionel so please tell me if I am totally be wrong.

Upvotes: 0

Views: 928

Answers (1)

Denys Wessels
Denys Wessels

Reputation: 17039

When you bind your GridView you should wrap it around a if(!Page.IsPostBack){} to make sure that it only gets bound when the page loads and not on every single post back.Also, below is a complete working example of what you're trying to achieve:

Code behind:

public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public partial class GridViewValidation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.BindData();
        }
    }

    private void BindData()
    {
        var u1 = new User { ID = 1, Name = "User1" };
        var u2 = new User { ID = 2, Name = "User2" };
        GridView1.DataSource = new List<User> { u1, u2 };
        GridView1.DataBind();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                DropDownList doublePartner = (row.Cells[2].FindControl("ddlDoublePartner") as DropDownList);
                doublePartner.BackColor = doublePartner.SelectedValue.Equals("-1") ? Color.Red : Color.Transparent;
            }
        }
    }
}

.ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ID" />
        <asp:BoundField DataField="Name" />
                <asp:TemplateField HeaderText="doublePartner">
            <ItemTemplate>
                <asp:DropDownList ID="ddlDoublePartner" runat="server">
                    <asp:ListItem Text="Value -1" Value="-1"></asp:ListItem>
                    <asp:ListItem Text="Value 1" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Value 2" Value="2"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

Output:

enter image description here

Upvotes: 1

Related Questions