C# 嵌套的gridview示例

C# 嵌套的gridview示例,c#,winforms,gridview,C#,Winforms,Gridview,我已经寻找了一天多的方法来创建可以添加到C#windows窗体中的嵌套gridview。我发现了许多使用ASP.NET的示例,但是我找不到任何使用C#的示例。我想要一个主/细节网格视图,它看起来像以下链接中的主/细节网格视图: 您可以使用template列创建嵌套的gridview,您需要在template列中构建HTML表,列的第一行是使用row databound事件使用自定义绑定填充主网格行。应该在html表的第二行中添加嵌套网格视图 检查以下链接。。 您有很多资源可以提供: 图像

我已经寻找了一天多的方法来创建可以添加到C#windows窗体中的嵌套gridview。我发现了许多使用ASP.NET的示例,但是我找不到任何使用C#的示例。我想要一个主/细节网格视图,它看起来像以下链接中的主/细节网格视图:

您可以使用template列创建嵌套的gridview,您需要在template列中构建HTML表,列的第一行是使用row databound事件使用自定义绑定填充主网格行。应该在html表的第二行中添加嵌套网格视图

检查以下链接。。

您有很多资源可以提供:

图像

.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>

使用.on而不是.live更新了jquery

<script type="text/javascript">
    $(document).on("click", "[src*=plus]", function () {
        $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
        $(this).attr("src", "../../_Images/minus.png");
    });
    $(document).on("click", "[src*=minus]", function () {
        $(this).attr("src", "../../_Images/plus.png");
        $(this).closest("tr").next().remove();
    });
</script>

$(文档)。在(“单击”、“[src*=plus]”上,函数(){
(“+$(this).next().html()+”)之后的“$”;
$(this.attr(“src”,“../../../\u Images/minus.png”);
});
$(文档)。在(“单击”、“[src*=减号]”上,函数(){
$(this.attr(“src”,“../../../\u Images/plus.png”);
$(this).closest(“tr”).next().remove();
});

可能有兴趣,我没有使用过它,但我相信它是在C#中。我真正想要的是纯粹在C#中创建一个主/细节网格视图。我想在windows窗体中使用此控件。我真正想要的是纯粹在C#中创建一个主/详细GridView。我想在windows窗体中使用此控件。LOL Sasidharan!我猜你的得票是因为你没有仔细阅读OP的问题。OP已经明确提到OP是专门寻找WindowsFormGrid而不是ASP.Net!。希望这能回答您的问题。这是一个很好的解决方案,但问题在于它无法在运行时加载子行,并且需要将所有数据一次加载到内存中。请不要只发布代码答案。请给你的答案加上一些解释。在没有任何解释的情况下,你的帖子是否应该是答案还不清楚。
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();
}
}
<script type="text/javascript">
    $(document).on("click", "[src*=plus]", function () {
        $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
        $(this).attr("src", "../../_Images/minus.png");
    });
    $(document).on("click", "[src*=minus]", function () {
        $(this).attr("src", "../../_Images/plus.png");
        $(this).closest("tr").next().remove();
    });
</script>