Datagrid树(两个级别) - 动态生成的网格无法更改

时间:2010-07-22 10:31:35

标签: c# .net javascript asp.net datagrid

我正在尝试使用两个级别实现树视图数据网格。

我的数据绑定如下:

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();
            }
        }
    }

我的数据网格的ItemDataBound函数如下:

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.                       
        }

    }

我的原始数据网格有一个带复选框的模板列,我生成的数据网格也有两列,一列是人员名称,另一列是带复选框的模板列。 我唯一坚持的是,我想检查我为每一行制作的NewDg girds中的所有复选框。

即使我为网格设置了唯一的ID,

FindControl(id)函数对我的dataGrid也不起作用(找到null)。

这就是为什么我在列表中填充网格并将其存储在会话中。

这是我的checkedChange事件连接到我的DataGrid1(父网格)中AutoPostback = true的复选框。

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;
                }
}
}
}

这不起作用。我调试了,我的List已填充,我有正确数量的datagrids但复选框没有改变。我错过了什么?我需要以一种方式解决这个问题,我可以获取所选项目并将其插入我的数据库中。我想知道为什么不能改变动态生成的dataGrid?

谢谢,

1 个答案:

答案 0 :(得分:0)

我通过在项目模板中使用带有数据网格的转发器来做类似的事情。

基于点击转发器中的行/行上的按钮,我将“切换”模板中的面板可见性,在该回发期间显示或隐藏网格,我还将数据绑定子网格。所以我只对可见网格进行数据绑定,而不是所有行中的所有网格。

它看起来像这样(对不起,如果不是从头部撞击这样做的话):

<asp:Repeater ...>
  <ItemTemplate>
    <tr>
    <td> ... </td>
    <td>
       <asp:Panel ..>
          <asp:GridView ...>
             ...
          </asp:GridView>
       </asp:Panel>
    </td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

在页面加载时,如果!IsPostback绑定转发器。 在转发器行中的项目的行事件:    1.切换该行可见的面板    2.获取数据    3.传递到行内的网格。    4.数据绑定网格

获取不同的视图只需在转发器的项模板中使用不同的面板。

似乎效果很好,效率非常高。

此外: 我在一个单独的自定义控件中有子网格,以保持代码整洁,并防止大量代码隐藏在文件后面的1个代码中。