我一直在尋找超過一天的方法來創建一個可以添加到C#窗體的嵌套gridview。我發現很多使用ASP.NET的例子,但是我找不到任何用C#完成的例子。我想一個主/詳細的GridView看起來像一個在下面的鏈接: http://ilovedeveloper.blogspot.com/2009/05/nested-gridview-using-c.html一個嵌套的gridview示例
回答
您可以使用模板列創建嵌套GridView控件,你需要建立HTML表格模板列裏面列的第一行使用自定義綁定使用行數據綁定事件填充主網格行。你穿鞋的HTML表格
檢查的第二行內添加嵌套使用網格視圖和下面的鏈接.. http://www.dotnetfox.com/articles/multilevel-nested-gridview-in-Asp-Net-using-C-Sharp-1012.aspx
你有很多資源,誰提供了這一點:
Walkthrough: Creating a Nested GridView Control
Nested GridView Example in ASP.Net using C# and VB.Net
我後面實際上是純粹在C#中創建主/細節GridView。我想在Windows窗體中使用這個控件。 – mrida
IMAGE
的.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>
<!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 runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
.Grid td
{
background-color: #A1DCF2;
color: black;
font-size: 10pt;
line-height:200%
}
.Grid th
{
background-color: #3AC0F2;
color: White;
font-size: 10pt;
line-height:200%
}
.ChildGrid td
{
background-color: #eee !important;
color: black;
font-size: 10pt;
line-height:200%
}
.ChildGrid th
{
background-color: #6C6C6C !important;
color: White;
font-size: 10pt;
line-height:200%
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function() {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/minus.png");
});
$("[src*=minus]").live("click", function() {
$(this).attr("src", "images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="CustomerID" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />
<asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvCustomers.DataSource = GetData("select top 10 * from Customers");
gvCustomers.DataBind();
}
}
private static DataTable GetData(string query)
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select top 3 * from Orders where CustomerId='{0}'", customerId));
gvOrders.DataBind();
}
}
}
爲什麼downvotes .. – Sasidharan
爲我工作,所以你有我的upvote。 – BryPie
LOL Sasidharan!我想你會得到低估,因爲你沒有仔細閱讀OP的問題。 OP明確提到OP是專門尋找WindowsForm網格而不是ASP.Net !.希望這回答你的問題。 – Vincy
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database
protected void BindGridview()
{
con.Open();
SqlCommand cmd = new SqlCommand("select TOP 4 CountryId,CountryName from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvParentGrid.DataSource = ds;
gvParentGrid.DataBind();
}
protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
GridView gv = (GridView)e.Row.FindControl("gvChildGrid");
int CountryId = Convert.ToInt32(e.Row.Cells[1].Text);
SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryId, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gv.DataSource = ds;
gv.DataBind();
}
}
請不要發佈代碼的答案。請給你的答案添加一些解釋。沒有任何解釋,你的帖子是否應該是答案還不清楚。 – honk
- 1. 顯示嵌套gridview
- 2. 不使用在單個GridView中顯示帶cont62的嵌套行的嵌套Gridview
- 3. 嵌套的GridView
- 4. 嵌套的GridView
- 5. 嵌套XML示例?
- 6. 嵌套gridview C#
- 7. asp.net C#Gridview嵌套gridview
- 8. Android嵌套按鈕示例?
- 9. 實例化一個嵌套的字典
- 10. 攻絲嵌套GridView
- 11. 在GridView中編輯嵌套的GridView
- 12. 嵌套類中的GridView
- 13. 嵌套GridView的樣式asp.net
- 14. 嵌套的GridView問題
- 15. 如何讓嵌套的GridView
- 16. 搜索嵌套的Gridview ASP.net
- 17. Scala中嵌套重置的示例
- 18. 嵌套的GridView創建一個額外的列
- 19. 如何gridview行刪除嵌套gridview
- 20. 在GridView中嵌套ListView
- 21. GridView嵌套在DataList中
- 22. 嵌套Gridview與小計/ grandtotal
- 23. 添加排序到一個嵌套的Gridview ASP.net與C#
- 24. 嵌套在另一個嵌套結果
- 25. Sencha Touch - 需要嵌套列表示例
- 26. 嵌套模型表單p1 Railscasts示例
- 27. Rails:三重嵌套窗體示例
- 28. Ansible複雜嵌套循環示例
- 29. 嵌套網格示例 - 不同列
- 30. log4net嵌套記錄器.config示例
[ExtGridView](http://www.codeproject.com/Articles/12299/ExtGridView)可能是有趣的,我沒有使用它,但我相信它是在C#中。 –
我後面實際上是純粹用C#創建主/細節GridView。我想在Windows窗體中使用這個控件。 – mrida