当前位置: 代码迷 >> ASP.NET >> asp:GridView 中显示两个表中的数据,后台怎么绑定
  详细解决方案

asp:GridView 中显示两个表中的数据,后台怎么绑定

热度:4226   发布时间:2013-02-25 00:00:00.0
asp:GridView 中显示两个表中的数据,后台如何绑定?
DataTable dt = BaseZyslinfo_db.getByZyslId(basetypeId);
  _count = dt.Rows.Count.ToString();
  gvList.DataSource = dt;
  gvList.DataBind();

这是获取zysl表中的数据根据basetypeId,展示到前台的gridview中,

另一张表是与zysl表的id是关联的
如何也在这个gridview中显示该表的信息呢?

请大虾指导....



------解决方案--------------------------------------------------------
SQL查询多表数据 直接GridView.DataSource 就可以了啊
------解决方案--------------------------------------------------------
可以建个视图啊 两个表关联起来 再绑定视图
------解决方案--------------------------------------------------------
LZ你把问题想复杂了,首先你要搞清楚一点,你Gridview里面绑定的字段是你后台查询出来的字段。简单的给你描述一下你就明白了。
C# code
  select a.Name,a.Sex,b.Address,b.Mail from TableA a,TableB b where 两表的公共条件  最后执行的ds结果会是这样子  NAME  SEX   ADDRESS      MAIL  张三   男     北京      123@sina.com  然后在你的gridview前台绑定的时候就分别直接绑定NAME SEX  ADDRESS MAIL就可以了
------解决方案--------------------------------------------------------
C# code
  <asp:GridView ID="GridView1" runat="server">  <asp:TemplateField HeaderText="性别">  <ItemTemplate>  <asp:Label ID="lblSex" runat="server" Text='<%# Eval("sex") %>'></asp:Label>      <asp:RadioButtonList ID="rblSex" runat="server">  </asp:RadioButtonList>  <asp:HiddenField ID="id" runat="server" Value='<%# Eval("id") %>' />      </asp:TemplateField>  </asp:GridView>C# code   protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)        {            //判断是否是数据项            if (e.Row.RowType == DataControlRowType.DataRow)            {                //判断是否是编辑状态或交替行                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)                {                                       RadioButtonList rblSex = (RadioButtonList)e.Row.FindControl("rblSex");//找到RadioButtonList                    string id =( (HiddenField)e.Row.FindControl("rblSex")).Value;//找到关联键                    rblSex.DataSource = xxxManager.GetXXById(id);//绑定RadioButtonList                    rblSex.DataBind();                     }            }        }
  相关解决方案