1

I have a GridView that shows a list of restaurants and it's data from my database. In the last column I have a ButtonField. When the user clicks on the button, then the RowCommand fires and another GridView appears with the selected restaurant's reviews.

The issue I am having is that while the RowCommand event is fired, the second GridView gvReviews does not show up at all.

  • I have tried to set the GridView's visible to true but that does not seem to work.
  • I have tried to use a TemplateField with a Button instead but that doesn't work either.
  • I have tried using if (!IsPostback) statement

Here's a snippet of my GridViews:

<asp:GridView ID="gvRestaurants" runat="server" AutoGenerateColumns="false" OnRowCommand="gvRestaurants_RowCommand">
            <Columns>
                <asp:BoundField DataField="RestaurantID" HeaderText="ID" />
                <asp:BoundField DataField="RestName" HeaderText="Restaurant" />
                <asp:BoundField DataField="RestAddr" HeaderText="Address" />
                <asp:BoundField DataField="RestCity" HeaderText="City" />
                <asp:BoundField DataField="RestState" HeaderText="State" />
                <asp:BoundField DataField="RestZip" HeaderText="Zip Code" />
                <asp:BoundField DataField="CategoryDesc" HeaderText="Category" />
                <asp:ButtonField HeaderText="Reviews" CommandName="viewReviews" Text="View" ButtonType="Button" />
            </Columns>
        </asp:GridView>
        <asp:GridView ID="gvReviews" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="RestaurantID" HeaderText="ID" />
                <asp:BoundField DataField="RestName" HeaderText="Restaurant" />
                <asp:BoundField DataField="ReviewDate" HeaderText="Date of Review" />
                <asp:BoundField DataField="FoodQuality" HeaderText="Food Quality" />
                <asp:BoundField DataField="ServiceRating" HeaderText="Service" />
                <asp:BoundField DataField="AtmosphereRating" HeaderText="Atmosphere" />
                <asp:BoundField DataField="PriceRating" HeaderText="Price" />
                <asp:BoundField DataField="ReviewText" HeaderText="Review" />
            </Columns>
        </asp:GridView>

Here's a snippet of the aspx.cs

protected void btnSearch_Click(object sender, EventArgs e)
        {
            gvRestaurants.Visible = true;
            gvAllRestaurants.Visible = false;
            DataSet ds = p.SearchByCategory(ddCategories.SelectedItem.Value, ddCategories2.SelectedItem.Value);
            gvRestaurants.DataSource = ds;
            gvRestaurants.DataBind();
        }

 protected void gvRestaurants_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "viewReviews")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvRestaurants.Rows[index];

                int restID = int.Parse(row.Cells[0].Text);

                gvReviews.DataSource = p.GetReview(restID);
                gvReviews.DataBind();

                gvRestaurants.Visible = false;
                gvReviews.Visible = true;
            }
            else
            {
                string error = "There are no reviews for this restaurant.";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
            }

Here's a snippet of the methods i used:

public DataSet SearchByCategory(string category1, string category2)
        {
            DBConnect objDB = new DBConnect();
            objCmd.Parameters.Clear();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "GetRestaurantByCategory";

            SqlParameter sqlParameter = new SqlParameter("@theCategory", category1);
            SqlParameter sqlParameter2 = new SqlParameter("@theCategory2", category2);

            sqlParameter.Direction = ParameterDirection.Input;
            sqlParameter.SqlDbType = SqlDbType.VarChar;
            sqlParameter.Size = 50;

            sqlParameter2.Direction = ParameterDirection.Input;
            sqlParameter2.SqlDbType = SqlDbType.VarChar;
            sqlParameter2.Size = 50;

            objCmd.Parameters.Add(sqlParameter);
            objCmd.Parameters.Add(sqlParameter2);

            objDB.GetConnection().Open();
            DataSet ds = objDB.GetDataSetUsingCmdObj(objCmd);
            objDB.CloseConnection();

            return ds;
        }

        public DataSet GetReview(int restaurant)
        {
            DBConnect objDB = new DBConnect();
            objCmd.Parameters.Clear();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "GetReviewByRestaurantID";

            SqlParameter sqlParameter = new SqlParameter("@theRestaurantID", restaurant);

            sqlParameter.Direction = ParameterDirection.Input;
            sqlParameter.SqlDbType = SqlDbType.Int;
            sqlParameter.Size = 4;

            objCmd.Parameters.Add(sqlParameter);

            objDB.GetConnection().Open();
            DataSet ds = objDB.GetDataSetUsingCmdObj(objCmd);
            objDB.CloseConnection();

            return ds;
        }

Here are my stored procedures used:

CREATE PROCEDURE [dbo].GetRestaurantByCategory
    @theCategory varchar(50),
    @theCategory2 varchar(50)

AS
    SELECT rest.RestaurantID, rest.RestName, rest.RestAddr, rest.RestCity, rest.RestState, rest.RestZip, cat.CategoryDesc
    FROM Restaurants rest JOIN Categories cat ON rest.CategoryID = cat.CategoryID
    WHERE CategoryDesc = @theCategory OR CategoryDesc = @theCategory2

CREATE PROCEDURE [dbo].GetReviewByRestaurantID
    @theRestaurantID int

AS
    SELECT rest.RestaurantID, rest.RestName, rev.ReviewDate, rev.FoodQuality, rev.ServiceRating, rev.AtmosphereRating, rev.PriceRating, rev.ReviewText
    FROM Restaurants rest JOIN Reviews rev ON rest.RestaurantID = rev.RestaurantID
    WHERE Rest.RestaurantID = @theRestaurantID
4

2 回答 2

1

Your GridView doesn't show because you bind it to a DataSet instead of binding it directly to a DataTable within this DataSet.

Try changing this line:

gvReviews.DataSource = p.GetReview(restID);

To this:

gvReviews.DataSource = p.GetReview(restID).Tables[0];
于 2019-03-07T08:55:06.597 回答
1

use this query

SELECT rest.RestaurantID, rest.RestName, rev.ReviewDate, rev.FoodQuality, rev.ServiceRating, rev.AtmosphereRating, rev.PriceRating, rev.ReviewText
    FROM Restaurants rest JOIN Reviews rev ON rev.RestaurantID = rest.RestaurantID
    WHERE Rest.RestaurantID = @theRestaurantID
于 2019-03-07T09:14:57.363 回答