在 C# 中将 Nested-GridView 与 DataSource DataTable 绑定
Binding Nested-GridView with DataSource DataTable in c#
我需要在 c#
中使用 DataTable
将 Nested-GridView
与 DataSource
绑定
我需要这个 return 例如
我下面的代码没有成功,因为这是 return
我的问题在RowDataBound event
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
gvOrders.DataSource = cgv; // set child datatable to parent gridview as datasource
gvOrders.DataBind(); // bind the gridview with datasource
}
}
如何获取 gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
的值以使用单个国家/地区代码填充子项 table?
如何解决这个问题?
public partial class _Default : Page
{
DataTable cgv = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Create a datatable as a DataSource of GridViews
DataTable dtParent = new DataTable(); // parent gridview datasource
DataTable dtChild = new DataTable(); // child gridview datasource
dtChild = GetData("SELECT * FROM `countrylanguage` WHERE 1;");
cgv = dtChild; // set child datatable to temporary datatable
dtParent = GetData("SELECT * FROM `countrylanguage`;");
gvCustomers.DataSource = dtParent;
gvCustomers.DataBind();
}
}
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
gvOrders.DataSource = cgv; // set child datatable to parent gridview as datasource
gvOrders.DataBind(); // bind the gridview with datasource
}
}
private static DataTable GetData(string query)
{
string strConnString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(strConnString))
{
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.CommandText = query;
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
}
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="Language" OnRowDataBound="gvParent_RowDataBound">
<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="IsOfficial" HeaderText="IsOfficial" />
<asp:BoundField ItemStyle-Width="150px" DataField="Percentage" HeaderText="Percentage" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="CountryCode" HeaderText="CountryCode" />
<asp:BoundField ItemStyle-Width="150px" DataField="Language" HeaderText="Language" />
</Columns>
</asp:GridView>
-- ----------------------------
-- Table structure for countrylanguage
-- ----------------------------
DROP TABLE IF EXISTS `countrylanguage`;
CREATE TABLE `countrylanguage` (
`CountryCode` char(3) NOT NULL DEFAULT '',
`Language` char(30) NOT NULL DEFAULT '',
`IsOfficial` enum('T','F') NOT NULL DEFAULT 'F',
`Percentage` float(4,1) NOT NULL DEFAULT '0.0',
PRIMARY KEY (`CountryCode`,`Language`),
KEY `CountryCode` (`CountryCode`),
CONSTRAINT `countryLanguage_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of countrylanguage
-- ----------------------------
INSERT INTO `countrylanguage` VALUES ('ABW', 'Dutch', 'T', '5.3');
INSERT INTO `countrylanguage` VALUES ('AFG', 'Balochi', 'F', '0.9');
INSERT INTO `countrylanguage` VALUES ('AGO', 'Ambo', 'F', '2.4');
INSERT INTO `countrylanguage` VALUES ('AIA', 'English', 'T', '0.0');
INSERT INTO `countrylanguage` VALUES ('ALB', 'Albaniana', 'T', '97.9');
INSERT INTO `countrylanguage` VALUES ('AND', 'Catalan', 'T', '32.3');
INSERT INTO `countrylanguage` VALUES ('ANT', 'Dutch', 'T', '0.0');
INSERT INTO `countrylanguage` VALUES ('ARE', 'Arabic', 'T', '42.0');
INSERT INTO `countrylanguage` VALUES ('ARG', 'Indian Languages', 'F', '0.3');
INSERT INTO `countrylanguage` VALUES ('ARM', 'Azerbaijani', 'F', '2.6');
像这样
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string CountryCode = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
cgv.DefaultView.RowFilter = "CountryCode='" + CountryCode.ToString() + "'";
gvOrders.DataSource = cgv;
gvOrders.DataBind();
}
}
我需要在 c#
中使用DataTable
将 Nested-GridView
与 DataSource
绑定
我需要这个 return 例如
我下面的代码没有成功,因为这是 return
我的问题在RowDataBound event
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
gvOrders.DataSource = cgv; // set child datatable to parent gridview as datasource
gvOrders.DataBind(); // bind the gridview with datasource
}
}
如何获取 gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
的值以使用单个国家/地区代码填充子项 table?
如何解决这个问题?
public partial class _Default : Page
{
DataTable cgv = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Create a datatable as a DataSource of GridViews
DataTable dtParent = new DataTable(); // parent gridview datasource
DataTable dtChild = new DataTable(); // child gridview datasource
dtChild = GetData("SELECT * FROM `countrylanguage` WHERE 1;");
cgv = dtChild; // set child datatable to temporary datatable
dtParent = GetData("SELECT * FROM `countrylanguage`;");
gvCustomers.DataSource = dtParent;
gvCustomers.DataBind();
}
}
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
gvOrders.DataSource = cgv; // set child datatable to parent gridview as datasource
gvOrders.DataBind(); // bind the gridview with datasource
}
}
private static DataTable GetData(string query)
{
string strConnString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(strConnString))
{
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.CommandText = query;
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
}
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="Language" OnRowDataBound="gvParent_RowDataBound">
<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="IsOfficial" HeaderText="IsOfficial" />
<asp:BoundField ItemStyle-Width="150px" DataField="Percentage" HeaderText="Percentage" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="CountryCode" HeaderText="CountryCode" />
<asp:BoundField ItemStyle-Width="150px" DataField="Language" HeaderText="Language" />
</Columns>
</asp:GridView>
-- ----------------------------
-- Table structure for countrylanguage
-- ----------------------------
DROP TABLE IF EXISTS `countrylanguage`;
CREATE TABLE `countrylanguage` (
`CountryCode` char(3) NOT NULL DEFAULT '',
`Language` char(30) NOT NULL DEFAULT '',
`IsOfficial` enum('T','F') NOT NULL DEFAULT 'F',
`Percentage` float(4,1) NOT NULL DEFAULT '0.0',
PRIMARY KEY (`CountryCode`,`Language`),
KEY `CountryCode` (`CountryCode`),
CONSTRAINT `countryLanguage_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of countrylanguage
-- ----------------------------
INSERT INTO `countrylanguage` VALUES ('ABW', 'Dutch', 'T', '5.3');
INSERT INTO `countrylanguage` VALUES ('AFG', 'Balochi', 'F', '0.9');
INSERT INTO `countrylanguage` VALUES ('AGO', 'Ambo', 'F', '2.4');
INSERT INTO `countrylanguage` VALUES ('AIA', 'English', 'T', '0.0');
INSERT INTO `countrylanguage` VALUES ('ALB', 'Albaniana', 'T', '97.9');
INSERT INTO `countrylanguage` VALUES ('AND', 'Catalan', 'T', '32.3');
INSERT INTO `countrylanguage` VALUES ('ANT', 'Dutch', 'T', '0.0');
INSERT INTO `countrylanguage` VALUES ('ARE', 'Arabic', 'T', '42.0');
INSERT INTO `countrylanguage` VALUES ('ARG', 'Indian Languages', 'F', '0.3');
INSERT INTO `countrylanguage` VALUES ('ARM', 'Azerbaijani', 'F', '2.6');
像这样
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string CountryCode = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
cgv.DefaultView.RowFilter = "CountryCode='" + CountryCode.ToString() + "'";
gvOrders.DataSource = cgv;
gvOrders.DataBind();
}
}