We help IT Professionals succeed at work.

asp.net gridview(button field)

onebite2 asked
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....
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    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;
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.CommandType = CommandType.Text;
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        return dt;

Open in new window

Watch Question

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.