C# Gridview-对新列的引用
我有一个gridview,它从3个表中获取数据。 这个gridview还有一个名为“Role”的附加列,它不包括在数据库中。 目前,没有添加任何逻辑,只需在标签“Role”上使用findcontrol,我就可以显示“ML” 但是,当我加入逻辑时,它根本没有出现 在任何情况下,是否有人知道如何将“ML”插入数据库中找不到的“Role”列中,但该列是数据库中另一列的引用 这是用于在角色列中显示“ML”的代码。C# Gridview-对新列的引用,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,它从3个表中获取数据。 这个gridview还有一个名为“Role”的附加列,它不包括在数据库中。 目前,没有添加任何逻辑,只需在标签“Role”上使用findcontrol,我就可以显示“ML” 但是,当我加入逻辑时,它根本没有出现 在任何情况下,是否有人知道如何将“ML”插入数据库中找不到的“Role”列中,但该列是数据库中另一列的引用 这是用于在角色列中显示“ML”的代码。 protected void gridAMDisplay_RowDataBound(obj
protected void gridAMDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the Label control using FindControl and set its Text on some column value
DataRowView row = (DataRowView)e.Row.DataItem;
if (!DBNull.Value.Equals(row["ModuleLeader"]))
{
if (row["ModuleLeader"].ToString() == "ModuleStr")
{
Label lbl = e.Row.FindControl("lblRole") as Label;
if (lbl != null)
{
lbl.Text = "ML";
}
}
}
}
}
if (row["ModuleLeader"].ToString() == "ModuleStr")
{
这部分代码在注释关闭时,ML可以显示在“角色”列中,否则不显示任何内容。
因此,我觉得findcontrol部分可以工作。但是,引用不起作用。
protected void gridAMDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the Label control using FindControl and set its Text on some column value
DataRowView row = (DataRowView)e.Row.DataItem;
if (!DBNull.Value.Equals(row["ModuleLeader"]))
{
if (row["ModuleLeader"].ToString() == "ModuleStr")
{
Label lbl = e.Row.FindControl("lblRole") as Label;
if (lbl != null)
{
lbl.Text = "ML";
}
}
}
}
}
if (row["ModuleLeader"].ToString() == "ModuleStr")
{
正如我提到的,数据库中的任何表中都不包括role列。
因此,我添加了此代码。
<asp:TemplateField HeaderText="Role">
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text="" />
</ItemTemplate>
</asp:TemplateField>
但是,我现在的问题是,角色列没有引用它应该引用的列,即“模块领导者”您引用的数据项不能用于数据绑定,您应该使用OnDataBinding来实现这一点。我认为最好的做法是创建自己的模板 以下是一个关于您的案例:
public class NotEmptyValueFromDataTemplate : System.Web.UI.Page, IBindableTemplate
{
protected string p_DataFieldToLookAt;
protected string p_ValueToPlaceInIfNotNull;
protected Literal p_litControl;
public RoleTemplate(string DataFieldToLookAt, string ValueToPlaceInIfNotNull)
{
this.p_DataFieldToLookAt = DataFieldToLookAt;
this.p_ValueToPlaceInIfNotNull = ValueToPlaceInIfNotNull;
}
public virtual void OnInit(object sender, EventArgs e)
{ }
#region IBindableTemplate Members
public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)
{
throw new NotImplementedException();
}
#endregion
#region ITemplate Members
public void InstantiateIn(Control container)
{
p_litControl = new Literal();
p_litControl.ID = p_DataFieldToLookAt; /* we don't really care */
p_litControl.Init += new EventHandler(OnInit);
p_litControl.DataBinding += new EventHandler(p_litControl_DataBinding);
container.Controls.Add(p_litControl);
}
void p_litControl_DataBinding(object sender, EventArgs e)
{
var Data = ((GridViewRow)(p_litControl.NamingContainer)).DataItem;
string ValueFromData = Convert.ToString(DataBinder.Eval(Data, p_DataFieldToLookAt));
if(!String.IsNullOrEmpty(ValueFromData))
p_litControl.Text = p_ValueToPlaceInIfNotNull;
}
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
}
#endregion
}
然后必须实例化每个模板行:
protected void GridView3_Init(object sender, EventArgs e)
{
((TemplateField)(GridView3.Columns[/* Column Index Here*/])).ItemTemplate = new NotEmptyValueFromDataTemplate("ModuleLeader", "ML");
}
最后,在gridview中创建模板项:
<asp:TemplateField HeaderText="Role"></asp:TemplateField>
PS:代码未经测试,是从解决方案中写入的似乎您只希望基于同一行的另一列中的数据将角色列更新为“ML”。为什么不直接把它放到XHTML中呢
<asp:TemplateField HeaderText="Role">
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text='<%# GetRoleString(Eval("ModuleLeader"))%>' />
</ItemTemplate>
</asp:TemplateField>
这将在数据绑定时发生,并且每行仅发生一次。我编辑了您的问题以正确设置代码示例的格式,以便人们可以看到它们。如何使用onDataBinding?你能给我举个例子吗。