日期:2014-05-19  浏览次数:20794 次

datgrid模版列邦定问题 ,谢谢!
比如:
提出人     执行人
    1             2,3,4
    5             6
    7             3,4,5,8

用datagrid怎么实现执行人的模版列邦定,其中执行人列表是HyperLink
请给出详细代码,谢谢~~


------解决方案--------------------
把所有执行人都传过去,在另一个地方去判断
------解决方案--------------------
给你个Demo,用的pubs数据库

//aspx
<form id= "Form1 " method= "post " runat= "server ">
<BR>
<asp:DataGrid id= "DataGrid1 " runat= "server " AutoGenerateColumns= "False ">
<Columns>
<asp:BoundColumn DataField= "au_id " ReadOnly= "True " HeaderText= "au_id "> </asp:BoundColumn>
<asp:TemplateColumn HeaderText= "title_id ">
<ItemTemplate>
<asp:Repeater id= "Repeater1 " runat= "server " DataSource= ' <%#GetTitleID(DataBinder.Eval(Container.DataItem, "au_id ").ToString())%> '>
<ItemTemplate>
<a href= ' <%# "details.aspx?title_id= " + DataBinder.Eval(Container.DataItem, "title_id ").ToString() %> '> <%#DataBinder.Eval(Container.DataItem, "title_id ")%> </a>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

//aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class DataGridDemo : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

public DataView GetTitleID(string au_id)
{
SqlConnection cn = new SqlConnection(@ "server=.;uid=sa;pwd=sa;database=pubs ");
SqlDataAdapter da = new SqlDataAdapter( "select title_id from titleauthor where au_id = @au_id ", cn);
da.SelectCommand.Parameters.Add( "@au_id ", SqlDbType.VarChar, 11).Value = au_id;
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
return ds.Tables[0].DefaultView;
}

private void BindGrid()
{
SqlConnection cn = new SqlConnection( "server=.;uid=sa;pwd=sa;database=pubs ");
SqlDataAdapter da = new SqlDataAdapter( "select au_id from authors ", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();

DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataKeyField = "au_id ";
DataGrid1.DataBind();
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}