views:

45

answers:

0

I am trying to implement a tree view datagrid with two levels.

I am binding my data as follows:

private void BindData()
    {
        string sqlQuery = "SELECT dept_code, dept_name FROM Department";
        conn = new SqlConnection();
        conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\saher\Documents\TreeTest\TreeDemo\App_Data\TreeData.mdf;Integrated Security=True;User Instance=True";

        try
        {
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "DepInfo");
            DataGrid1.DataSource = ds;

            DataGrid1.DataBind();

        }
        catch (Exception e)
        {
            Response.Write("An Error Has occured!");
            //Response.End();
            Response.Write(e.ToString());
        }
        finally
        {
            if (conn.State == System.Data.ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }

my ItemDataBound function for my datagrid is as follows:

protected void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        //If your page size is 10, only 10 sub queries will be done.
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string newSqlQuery = "SELECT S.staff_name FROM Staff as S where S.dep_code ='" + e.Item.Cells[1].Text + "'";
            //Here I am grabbing the additional data and putting it
            //into mini datagrids…
            //If you wish to just use labels, or other controls, just
            //bind the data as you
            //wish, and render to html as I did.

            DataSet ds = this.RunQuery(newSqlQuery);
            DataGrid NewDg = new DataGrid();
            NewDg.AutoGenerateColumns = false;
            NewDg.Width = Unit.Percentage(100.00);
            DataGridTemplate temp = new DataGridTemplate(ListItemType.Item, "staffCol");
            TemplateColumn tempCol = new TemplateColumn();
            tempCol.ItemTemplate = temp;
            BoundColumn bound = new BoundColumn();
            bound.DataField = "staff_name";
            NewDg.Columns.Add(tempCol);
            NewDg.Columns.Add(bound);
            NewDg.DataSource = ds;
            NewDg.DataBind();
            SetProps(NewDg);

            subGrids.Add(NewDg); // subGrids is a private ArrayList I have to store the grids.



            /**
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            datagrid.RenderControl(htw);
            string DivStart = "<DIV id=’uniquename" + e.Item.ItemIndex.ToString() + "‘ style=’DISPLAY: none;’>";
            string DivBody = sw.ToString();
            string DivEnd = "</DIV>";
            string FullDIV = DivStart + DivBody + DivEnd;
            int LastCellPosition = e.Item.Cells.Count - 1;
            int NewCellPosition = e.Item.Cells.Count - 2;
            e.Item.Cells[0].ID = "CellInfo" + e.Item.ItemIndex.ToString();
            if (e.Item.ItemType == ListItemType.Item)
            {
                e.Item.Cells[LastCellPosition].Text = e.Item.Cells[LastCellPosition].Text +
                "</td><tr><td bgcolor=’f5f5f5′></td><td colspan=’" +
                NewCellPosition + "‘>" + FullDIV;
            }
            else
            {
                e.Item.Cells[LastCellPosition].Text = e.Item.Cells[LastCellPosition].Text +
                "</td><tr><td bgcolor=’d3d3d3′></td><td colspan=’" +
                NewCellPosition + "‘>" + FullDIV;
            }
            **/
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            datagrid.RenderControl(htw);
            string DivStart = "<DIV id=\"uniquename" + e.Item.ItemIndex.ToString() + "\" style=\"display:none\";’>";
            string DivBody = sw.ToString();
            string DivEnd = "</DIV>";
            string FullDIV = DivStart + DivBody + DivEnd;
            int LastCellPosition = e.Item.Cells.Count - 1;
            int NewCellPosition = e.Item.Cells.Count - 2;
            e.Item.Cells[0].ID = "CellInfo" + e.Item.ItemIndex.ToString();
            if (e.Item.ItemType == ListItemType.Item)
            {
                e.Item.Cells[LastCellPosition].Text = e.Item.Cells[LastCellPosition].Text +
                "</td><tr id =row" + e.Item.ItemIndex.ToString() + "><td bgcolor=’000000′></td><td colspan=’" +
                NewCellPosition + "‘>" + FullDIV;
            }
            else
            {
                e.Item.Cells[LastCellPosition].Text = e.Item.Cells[LastCellPosition].Text +
                "</td><tr id =row" + e.Item.ItemIndex.ToString() + "><td bgcolor=’d3d3d3′></td><td colspan=’" +
                 NewCellPosition + "‘>" + FullDIV;
            }

            //============Set up javascript methods.=============
            e.Item.Cells[0].Attributes["onclick"] = "HideShowPanel('uniquename" +
         e.Item.ItemIndex.ToString() + "'); ChangePlusMinusText('" +
         e.Item.Cells[0].ClientID + "'); SetExpandedDIVInfo('" +
         e.Item.Cells[0].ClientID + "','" + this.txtExpandedDivs.ClientID +
         "', 'uniquename" + e.Item.ItemIndex.ToString() + "');"; 

            e.Item.Cells[0].Attributes["onmouseover"] = "this.style.cursor='pointer'";
            e.Item.Cells[0].Attributes["onmouseout"] = "this.style.cursor='pointer'";

            Session["checkSession"] = subGrids; // I store the ArrayList in the session so that I won't loose the dataGrids after a post back.                       
        }

    }

my original datagrid has a template column with checkboxes and my generated datagrids also have two columns, one for staff name and one is template column with checkboxes. The only thing I am stuck on is that I want to check all checkboxes in the NewDg girds I make for every row.

FindControl(id) function doesn't work (finds null) for my dataGrids even when I have set unique Id's for the grids.

This is why I populate the girds in a list and store it in the session.

this is my checkedChange event connected to the checkboxes with AutoPostback = true in my DataGrid1 (the parent grid).

protected void Check_Change(object s, EventArgs ev)
    {
        ArrayList gridList = (ArrayList)Session["checkSession"];

        foreach (DataGridItem i in DataGrid1.Items)
        {
            string newSqlQuery = "SELECT S.staff_name FROM Staff as S where S.dep_code ='" + i.Cells[1].Text + "'";  

            CheckBox b = (CheckBox)i.Cells[2].Controls[1];

            if (b.Checked)
            {
                DataGrid dg = (DataGrid)gridList[0];

                foreach (DataGridItem item in dg.Items)
                {
                    CheckBox myBox = (CheckBox)item.Cells[0].Controls[0];
                    myBox.Checked = true;
                }
}
}
}

This didn't work. I debugged, my List is populated, I have the right number of datagrids but the checkbox isn't altered. What am I missing?? I need to solve this in a way I can get the chosen Items and insert them in my database. I want to know why the dynamically generated dataGrids cannot be altered??

Thanks,