一个页面两个GridView实现主细信息的显示

点击GridView某一行的按钮,在另一个GridView中显示该条信息的详细信息

两种方法:

1.前台不采用模板列:

GridView1中

<asp:ButtonField  Text="查看" ButtonType="Button"  commandName="look"  HeaderStyle-HorizontalAlign ="Center" Itemstyle-HorizontalAlign="Center" />

对应后台代码:差别就在前两行

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //显示订单从表信息
            if (e.CommandName.ToString() == "look")
            {
               
               int index = Convert.ToInt32(e.CommandArgument.ToString());//得到行号

string sql2 = "select * from tb_orderDetails where orderID='" + GridView1.DataKeys[index].Value.ToString() + "'";


                SqlCommand cmd = new SqlCommand(sql2, con.conn);
                SqlDataAdapter da = new SqlDataAdapter();
                con.OpenConn();
                da.SelectCommand = cmd;
                con.CloseConn();
                DataSet ds = new DataSet();
                da.Fill(ds, "details");
                GridView2.DataSource = ds;
                GridView2.DataBind();


            }
        }


string strID = e.CommandArgument.ToString();
                string sql2 = "select * from tb_orderDetails where orderID='" + strID + "'";
                SqlCommand cmd = new SqlCommand(sql2, con.conn);
                SqlDataAdapter da = new SqlDataAdapter();
                con.OpenConn();
                da.SelectCommand = cmd;
                con.CloseConn();
                DataSet ds = new DataSet();
                da.Fill(ds, "details");
                GridView2.DataSource = ds;

                GridView2.DataBind();


2.前台采用模板列

GridView1中

 <asp:TemplateField ShowHeader="False">
<ItemTemplate>
       <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="look" Text="查看"  CommandArgument='<%#Eval("orderID") %>'></asp:LinkButton>
 </ItemTemplate>
                                                                        <HeaderStyle HorizontalAlign="Center" />
                                                                        <ItemStyle HorizontalAlign="Center" />

 </asp:TemplateField>

对应后台代码:差别就在前两行

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //显示订单从表信息
            if (e.CommandName.ToString() == "look")
            {
              
                string strID = e.CommandArgument.ToString();
                string sql2 = "select * from tb_orderDetails where orderID='" + strID + "'";

                SqlCommand cmd = new SqlCommand(sql2, con.conn);
                SqlDataAdapter da = new SqlDataAdapter();
                con.OpenConn();
                da.SelectCommand = cmd;
                con.CloseConn();
                DataSet ds = new DataSet();
                da.Fill(ds, "details");
                GridView2.DataSource = ds;
                GridView2.DataBind();


            }
        }

为什么写法会这样?自己调试的时候发现,两种形式下,e.CommandArgument里的存储的值不同

第一个,存的是当前行索引值

第二个,存的是当前行主键的值

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值