Search code examples
c#asp.netimagegridviewnested-gridview

Nested Gridview Showing Same Data on every row DataBound & downloading a file within Child Gridview


i have a parent and a child GridView.
The Child gridview is populated by a value stored in parent DataSet Table.

child Gridview with Same Data

i need my Rows to fetch data according to the ReportCode.

Here is my code:-

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            DataSet dstReportsCodes = getReportCode();
            dst = new DataSet();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string pub_id = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                dm.Open();
                //dst = dm.ApprovalExpense("SelectForReport", "Exp-4OWTR", "", "", "", "", "", "");    // which are the expenses
                if (dstReportsCodes.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < dstReportsCodes.Tables[0].Rows.Count; i++)
                    {
                        dst = null;
                        dst = dm.ApprovalExpense("SelectForReport", Convert.ToString(dstReportsCodes.Tables[0].Rows[i]["ReportCode"]), "", "", "", "", "", "");
                        GridView pubTitle = (GridView)e.Row.FindControl("GridView2");
                        pubTitle.DataSource = dst.Tables[0];
                        pubTitle.DataBind();
                    }
                }

                dm.Close();
            }
        }
        catch (Exception ex) { lblErrorText.Text = ex.ToString(); }
    }

NOTE : getReportCode() is the function which is returning a dataset with Report Codes, the ReportCode is fetched like Report Code Image

Now these report codes are used to fetch data for every row of Child Gridview, the details are coming in OK but it is binding same data everytime.

a little help would be highly appreciated, thanx in advance.


EDIT
Now i need to download a file which in present in child gridview.
what is the best possible way to do that/
what argument should i pass ? (like its address or what?)
i need the file to be downloaded without the child gridview being collapsed. child grid image


Solution

  • When you RowDataBound you already have those row items that your are getting from getReportCode() method. All you have to do is read those items when you iterate using RowDataBound. In your cases, you can use below code to read ReportCode.

    string reportCode = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ReportCode"));
    

    Next, All you have to do is pass reportCode as parameter in your ApprovalExpense method.

    dst = dm.ApprovalExpense("SelectForReport", reportCode, "", "", "", "", "", "");
    GridView pubTitle = (GridView)e.Row.FindControl("GridView2");