Tôi đã tìm kiếm nhiều hơn một ngày để tạo một khung lưới lồng nhau có thể được thêm vào biểu mẫu C# windows. Tôi đã tìm thấy nhiều ví dụ đang sử dụng ASP.NET tuy nhiên tôi không thể tìm thấy bất kỳ được thực hiện với C#. Tôi muốn có một GridView chính/chi tiết trông giống như trong liên kết sau: http://ilovedeveloper.blogspot.com/2009/05/nested-gridview-using-c.htmlVí dụ về chế độ xem lưới lồng nhau
Trả lời
bạn có thể sử dụng cột mẫu để tạo GridView lồng nhau, bạn cần tạo bảng HTML bên trong cột mẫu trong hàng đầu tiên của cột được lấp đầy với các hàng lưới chính bằng cách sử dụng tùy chỉnh ràng buộc bằng cách sử dụng sự kiện databound hàng. bạn giày thêm tổ xem lưới bên trong hàng thứ hai của bảng html
kiểm tra với đường dẫn sau .. http://www.dotnetfox.com/articles/multilevel-nested-gridview-in-Asp-Net-using-C-Sharp-1012.aspx
Bạn có nhiều tài nguyên người này quy định:
Walkthrough: Creating a Nested GridView Control
Nested GridView Example in ASP.Net using C# and VB.Net
Những gì tôi thực sự sau là tạo ra một GridView chủ/chi tiết hoàn toàn trong C#. Tôi muốn sử dụng điều khiển này ở dạng cửa sổ. – 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();
}
}
}
lý do tại sao downvotes .. – Sasidharan
Làm việc cho tôi, vì vậy bạn đã có upvote của tôi. – BryPie
LOL Sasidharan! Tôi đoán bạn đang nhận được downvotes vì bạn không đọc câu hỏi của OP rất cẩn thận. OP đã đề cập rõ ràng rằng OP đặc biệt tìm kiếm lưới WindowsForm chứ không phải ASP.Net !. Hy vọng điều này trả lời câu hỏi của bạn. – 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();
}
}
Vui lòng không đăng câu trả lời chỉ bằng mã. Vui lòng thêm một số giải thích cho câu trả lời của bạn. Không có bất kỳ lời giải thích nào là không rõ liệu bài viết của bạn có phải là câu trả lời hay không. – honk
- 1. Loại bỏ chế độ xem lồng nhau
- 2. Ví dụ về đặt lại lồng nhau trong Scala
- 3. Ví dụ cụ thể về chia sẻ trạng thái giữa nhiều chế độ xem (WPF MVVM)
- 4. Ví dụ về việc sử dụng tab Android có Chế độ xem thay vì Hoạt động?
- 5. Sự kiện trong Backbone.js các chế độ xem lồng nhau
- 6. Backbone.js: Định tuyến cho các chế độ xem lồng nhau
- 7. Tạo chế độ xem lưới trên iOS
- 8. cách xóa chế độ xem lưới?
- 9. Chế độ xem lưới Android Cuộn ngang
- 10. Rspec newbie: Ví dụ nhanh về kiểm tra bộ điều khiển lồng nhau?
- 11. Chế độ xem lưới ASP.NET so với chế độ xem danh sách
- 12. Ví dụ hay về MapServer/OpenLayers
- 13. Ai đó có thể đưa ra một ví dụ chính xác về triển khai chế độ xem web trong android
- 14. Ví dụ về chế độ xem chung dựa trên lớp Django: vị trí của ** kwargs đến từ đâu?
- 15. Ví dụ về Winsock?
- 16. Ví dụ về JGraphT
- 17. Ví dụ về D.ccurl
- 18. Ví dụ về MultiSelectListPreference
- 19. Ví dụ về pysmb
- 20. Cuộn chế độ xem sai (ví dụ như Yahoo thời tiết)
- 21. Liên kết lồng nhau, cách chuẩn bị biến cho chế độ xem, đường ray
- 22. Làm cách nào để thiết lập chế độ xem lồng nhau trong AngularJS?
- 23. Ví dụ về Fastcall GCC
- 24. Tìm kiếm ví dụ đơn giản về MVVM Light
- 25. ví dụ hay về Javadoc
- 26. Liên kết tới tệp (ví dụ: PDF) trong chế độ xem CakePHP
- 27. Cách "xoay" một lớp/chế độ xem (ví dụ như bạn đang ở trong enigmo)
- 28. Ví dụ về Java BitSet
- 29. Ví dụ về các ví dụ điển hình
- 30. Kéo các mục trong chế độ xem lưới
[ExtGridView] (http://www.codeproject.com/Articles/12299/ExtGridView) có thể được quan tâm, tôi đã không sử dụng nó nhưng tôi tin rằng nó trong C#. –
Những gì tôi thực sự sau là tạo ra một GridView chủ/chi tiết hoàn toàn trong C#. Tôi muốn sử dụng điều khiển này ở dạng cửa sổ. – mrida