Cấp bậc tác giả:

DOTNET

Tạo Grid dạng Treeview(Web ASP.NET)

Được viết bởi QuangIT ngày 31/07/2012 lúc 01:45 PM
Tạo Grid dạng Treeview(Web ASP.NET)
  • 0
  • 10377
Tải tệp tin: Click ở đây

Tạo Grid dạng Treeview(Web ASP.NET)

Tạo Database: 

ParentTable




ChildTable






Tạo Project và kéo thả GridView vào web form


Đây là code giao diện:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None"
OnRowCreated="GridView1_RowCreated"
OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand"
Width="526px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Root">
<ItemTemplate>
<asp:Button ID="PluseBT" runat="server" Text="+" Visible="False" Height="16px" CommandName="_Show" />
<asp:Button ID="MinBT" runat="server" Text="-" Height="17px" Width="14px" CommandName="_Hide" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ParentID" HeaderText="Parent Id" />
<asp:BoundField DataField="ChildId" HeaderText="Child Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Dateof" HeaderText="Dateof" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

<br />
Bản quyền thuộc về DNGAZ.com</div>
</form>
</body>
</html>
Đây là Code thực thi sự kiện:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String Cn = @"Data Source=ADMINCNTTVN-PC\SQLEXPRESS;" +
"database=TreeView;" +
"Integrated Security=SSPI;";

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)
{
String Cn = @"Data Source=ADMINCNTTVN-PC\SQLEXPRESS;" +
"database=TreeView;" +
"Integrated Security=SSPI;";
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("&nbsp;", "");
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 == "&nbsp;")
{
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 == "&nbsp;")
{
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;
}
}
}
}

Đây là kết quả gặt hái được:

Nguồn bài viết: Dngaz.com

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML