在C#中将Nested-GridView与数据源DataTable绑定 [英] Binding Nested-GridView with DataSource DataTable in c#
本文介绍了在C#中将Nested-GridView与数据源DataTable绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在c#
例如,我需要此退货
我下面的代码尝试失败,因为这是返回
My code below tried without success because this is return
我的问题出在 RowDataBound事件
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();
的值,以便使用单个CountryCode填充子表?
How to do get the value of gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
for populate the child table with single CountryCode?
该如何解决?
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#中将Nested-GridView与数据源DataTable绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文