【发布时间】: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