素材巴巴 > 程序开发 >

GridView 内嵌 TreeView

程序开发 2023-09-03 13:03:33

介绍

1-第一步 在数据库中创建下面两个表.

one.jpg

two.jpg

2-在vs中创建一个工程,然后,从工具栏上拖拽一个GridView控件到窗口中.

Tree1.jpg

下面是工程代码

           
protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

SqlConnection Con = new SqlConnection(Cn);

string sel = "select ParentName,ParentID,Dateof from ParentTable";

SqlCommand cmd = new SqlCommand(sel, Con);

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("ParentID", typeof(string)));

dt.Columns.Add(new DataColumn("ChildId", typeof(string)));

dt.Columns.Add(new DataColumn("Name", typeof(string)));

dt.Columns.Add(new DataColumn("Dateof", typeof(string)));

Con.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())

{

DataRow dr = dt.NewRow();

int Pid = Convert.ToInt32(reader["ParentID"]);

dr["ParentID"] = (reader["ParentID"]);

dr["ChildId"] = null;

dr["Name"] = (reader["ParentName"]);

dr["Dateof"] = reader["Dateof"]; 

dt.Rows.Add(dr);

DataTable Mytable = Test(Pid.ToString());

foreach (DataRow dro in Mytable.Rows)

{

DataRow dr2 = dt.NewRow();

dr2["ChildId"] = dro["ChildId"];

dr2["Name"] = dro["ChildName"];

dr2["Dateof"] = dro["Dateof"];

dt.Rows.Add(dr2);

}


}

reader.Close();

Con.Close();

GridView1.DataSource = dt;

GridView1.DataBind();

}

}

protected DataTable Test(string ParentId)

{

SqlConnection Con = new SqlConnection(Cn);

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("ChildName", typeof(string)));

dt.Columns.Add(new DataColumn("ChildId", typeof(string)));

dt.Columns.Add(new DataColumn("Dateof", typeof(string)));

string sel2 = "select Dateof,ChildName,ChildId from ChildTable where ParentId=" + ParentId;

SqlCommand cmd_2 = new SqlCommand(sel2, Con);

Con.Open();

SqlDataReader reader2 = cmd_2.ExecuteReader();

while (reader2.Read())

{

DataRow dr2 = dt.NewRow();

dr2["ChildName"] = reader2["ChildName"];

dr2["ChildId"] = reader2["ChildId"]; 

dr2["Dateof"] = reader2["Dateof"];

dt.Rows.Add(dr2);

}

return dt;

}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.Header)

{

e.Row.Cells[1].Visible = false;

e.Row.Cells[2].Visible = false;

}

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Cells[1].Visible = false;

e.Row.Cells[2].Visible = false;

Button MinButton = (Button)e.Row.Cells[0].FindControl("MinBT");

MinButton.CommandArgument = e.Row.RowIndex.ToString();

Button addButton = (Button)e.Row.Cells[0].FindControl("PluseBT");

addButton.CommandArgument = e.Row.RowIndex.ToString();

// e.Row.Cells[3].Visible = false;

}

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

string ShowHide=e.Row.Cells[1].Text;

ShowHide = ShowHide.Replace(" ", "");

if (ShowHide.Trim().Length == 0)

{

Button Bt_Min=(Button)e.Row.Cells[0].FindControl("MinBT");

Bt_Min.Visible=false;

Button Bt_plus=(Button)e.Row.Cells[0].FindControl("PluseBT");

Bt_plus.Visible=false;



}



}

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "_Show")

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = GridView1.Rows[index];

int G_Count = GridView1.Rows.Count;

for (int i = index + 1; i < G_Count; i++)

{

if (GridView1.Rows[i].Cells[1].Text == " ")

{

GridView1.Rows[i].Visible = true;

}

else

{

Button Bt_Min = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min.Visible = true;

Button Bt_plus = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus.Visible = false;

break;

}
Button Bt_Min1 = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min1.Visible = true;

Button Bt_plus1 = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus1.Visible = false;


}

}

if (e.CommandName == "_Hide")

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = GridView1.Rows[index];

int G_Count=GridView1.Rows.Count;

for (int i = index + 1; i < G_Count; i++)

{

if (GridView1.Rows[i].Cells[1].Text == " ")

{

GridView1.Rows[i].Visible = false;

}

else

{

Button Bt_Min = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min.Visible = false;

Button Bt_plus = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus.Visible = true;

break;



}
Button Bt_Min1 = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min1.Visible = false;

Button Bt_plus1 = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus1.Visible = true;


}



}

Sa1.jpg

折叠结点

Sa2.jpg

源码下载:http://www.5login.com/admin/upload/files/Default7.zip 由5Login提供


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。