小弟在做一个订单的处理,一张订单表和一张订单明细表。同时显示订单表订单表和订单明细表的内容,让GridView显示订单明细表中对应订单表的相同订单ID内容。
前段代码:
<asp:ListView ID="lv" runat="server" DataKeyNames="OrderId">
<LayoutTemplate>
<table id="Table1" runat="server" border="0">
<tr runat="server" id="itemPlaceHolder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<table>
<tr><td>订单号:</td><td><asp:Label ID="lblId" runat="server" Text='<%Bind("OrderId") %>'></asp:Label></td></tr>
</table>
</td>
</tr>
<tr>
<td>
<table >
<tr>
<td>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" DataSource='<%# GVSource() %>'>
<Columns>
<asp:BoundField DataField="GoodsName" HeaderText="商品名" />
<asp:BoundField DataField="Price" HeaderText="价格" />
<asp:BoundField DataField="Quantity" HeaderText="数量" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</td>
</tr>
后台代码:
public object GVSource()
{
LQDataClassesDataContext lq = new LQDataClassesDataContext(ConfigurationManager.ConnectionStrings["db_B2CConnectionString"].ConnectionString);
var result = from u in lq.tb_OrderDetails
join gds in lq.tb_Goods on u.GoodsId equals gds.GoodsId
where u.OrderId == lv.DataKeys[CurrentIndex].Value.ToString()
select new
{
GoodsName = gds.GoodsName,
Price = u.Price,
Quantity = u.Quantity,
};
return result;
}
就是如何能获得当前行的索引,就是CurrentIndex的位置要用的
在GridView中设置DataKeys属性值,如DataKeys="订单ID"
后台则获取: DataKeys[0].Value.ToString()
找的,不知道是否可以帮你.
//ListView中数据绑定时 激发GridView绑定事件
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
((GridView)e.Item.FindControl("GridView1")).DataSource=getData((e.Item.FindControl("TextBox1") as TextBox).Text);
((GridView)e.Item.FindControl("GridView1")).DataBind();
//绑定数据方法
public DataTable getData(string rq)
{
Response.Write("<script>alert('" + rq + "') //string sql = "select 项目 from HHR_月考核表 where 创建时间='"+rq+ "'";
SqlDataAdapter da = new SqlDataAdapter("proc_任务提示框四", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter("@rq",DbType.DateTime);
p.Value = Convert.ToDateTime(rq);
da.SelectCommand.Parameters.Add(p);
DataSet ds = new DataSet();
da.Fill(ds,"tab");
return ds.Tables["tab"];
}