0

I am working on my first web project and I need help adding IF logic when using GridView. I have a page (CustomerListPage) that has a GridView item that displays a list of customers(rows) that can be clicked. Once the customer is clicked, the user is redirected to a page called "CustomerUsagePage" which shows the most recent record of this customer. This is what my GridView code looks like for my CustomerListPage:

 if (GridView1.SelectedRow.RowIndex == 0)
   {
       //string selectedUser = GridView1.SelectedRow.Cells[0].ToString();
       Response.Redirect("CustomerUsagePage.aspx?Customer_Name=" );
       BindGridview();
   }

The code for my CustomerUsagePage looks like this:

private void BindControlvalues()
{

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from dbo.CustomerTable", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    con.Close();
    DataSet ds = new DataSet();
    da.Fill(ds);
    Label4.Text = ds.Tables[0].Rows[0][1].ToString();
    Label1.Text = ds.Tables[0].Rows[0][0].ToString();
    Label2.Text = ds.Tables[0].Rows[0][3].ToString();
    Label3.Text = ds.Tables[0].Rows[0][4].ToString();
}

My problem is that I can only add IF logic the page where my GridView is located. I would like to click on a customer and have his data appear on my CustomerUsagePage with an if statement. Is it possible? I know I can do this by adding a page per customer but I dont want to go down that route, it seems to tedious. Does anyone have any suggestions?

2
  • What do you mean "add IF logic"? What are you trying to check? Commented Sep 26, 2012 at 14:10
  • I want to check which customer has been selected by checking the index of the rows. Ex: if row[1] {//Display info} Commented Sep 26, 2012 at 14:32

1 Answer 1

2

You can use GridView-FormView (Master/Detail) Control

Link : http://www.codeproject.com/Articles/16780/GridView-FormView-Master-Detail-Control.

Or you can use classic behavior with ItemCommand event

void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName=="Add")
    {
      int index = Convert.ToInt32(e.CommandArgument);

      GridViewRow row = CustomersGridView.Rows[index];

      .....//Adjust your Response
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

would this code go on the CustomerUsagePage? As is, it seems like it would go in the CustomerListPage where the GridView is.
I may be wrong, but this does not seem to be where I'm stuck. I already have if statements on my GridView item. The problem is, I am redirecting to the same page (CustomerUsagePage). I think this is the page where the IF statements should go. This way, the page checks which row has been selected to then know which data to display.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.