We help IT Professionals succeed at work.

asp.net gridview(button field)

onebite2
onebite2 asked
on
I have a grid view which displays 3 columns(Date,orders,amount)..when clicked on Date  it should show all details I mean products sold on that date in other grid with 2 columns(Date and products).

I could bind the other grid too but instead of bringing only particular date when clicked on that it is bringing the entire data.....I mean when I click on date:1/21/2008 it should bring orders sold only on that date in the second grid.....instead it is showing me all the orders on all dates.....how should I pass this to the second grid???

Here is my sample code for second grid.
I appreciate if I get some help....
Thanks!
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        GetData();
        
 
    }
    public void GetData()
    {
        DataTable dt = new DataTable();
      
        string sql = " select  Convert(Varchar(15),a.DateStarted, 101)as Date, b.product " +
                     " FROM Orders a " +
                     " INNER JOIN  OrdersUserIDs b ON a.OrderID = b.OrderID " +
                     " group by Convert(Varchar(15),a.DateStarted, 101), b.product " +
                     " order by Convert(Varchar(15),a.DateStarted, 101)";
       
        List<SqlParameter> Params = new List<SqlParameter>(0);
 
        
         
 
        dt = DataAccess.LoadDataTable(sql, Params);
        dt.Select("Date ='" + GridView1.SelectedValue + "'");
        GridView2.DataSource = dt;
        GridView2.DataBind();
 
 
    }
 
Data.cs:
 
public DataTable LoadDataTable(string sql, List<SqlParameter> inParameters)
    
    {
        int paramCount, index;
        string retVal = string.Empty;
        DataTable dt = new DataTable();
 
        //Database db = DatabaseFactory.CreateDatabase();
        //DbCommand dbcmd = db.GetSqlStringCommand(sql);
 
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString);
        SqlCommand cmd = new SqlCommand(sql, con);
 
        paramCount = inParameters.Count;
        index = 0;
        while (index < paramCount)
        {
            cmd.Parameters.Add(inParameters[index++]);
        }
 
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
 
        adapter.Fill(dt);
        return dt;
    }

Open in new window

Comment
Watch Question

Commented:
The dt.Select() method returns a new DataTable with the filtered results I believe but it does not change the actual DataTable that you call it on. Change the lines as shown below:
// Change these lines
dt.Select("Date ='" + GridView1.SelectedValue + "'");
GridView2.DataSource = dt;
 
// To this
GridView2.DataSource = dt.Select("Date ='" + GridView1.SelectedValue + "'");

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.