【问题标题】:Datagrid tree (two levels) - dynamically generated grids cannot be altered数据网格树(两级) - 动态生成的网格不能更改
【发布时间】:2011-03-19 11:36:26
【问题描述】:

我正在尝试实现具有两个级别的树视图数据网格。

我正在按如下方式绑定我的数据:

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 网格中的所有复选框。

FindControl(id) 函数对我的 dataGrids 不起作用(发现 null),即使我为网格设置了唯一的 Id。

这就是我在列表中填充网格并将其存储在会话中的原因。

这是我的 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;
                }
}
}
}

这不起作用。我调试了,我的列表被填充了,我有正确数量的数据网格,但复选框没有改变。我错过了什么??我需要以一种可以获得所选项目并将它们插入我的数据库的方式来解决这个问题。我想知道为什么动态生成的dataGrids不能更改??

谢谢,

【问题讨论】:

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


    【解决方案1】:

    我只是通过在项目模板中使用带有数据网格的中继器来完成类似的操作。

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

    它看起来像这样(很抱歉,如果它没有从头部 ram 上做这件事):

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

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

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

    似乎工作得很好,而且效率很高。

    还有: 我将子网格放在单独的自定义控件中,以保持代码整洁并防止 1 个代码隐藏文件中出现大量代码。

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 1970-01-01
      • 2021-01-05
      • 2011-05-21
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      相关资源
      最近更新 更多