gridview中的EDMX根据数据动态创建列

时间:2013-12-05 18:11:08

标签: c# asp.net gridview entity-framework-4

我一直在努力解决这个问题。 我想使用实体框架动态创建数据列。

var query2 = (from c in dbc.FridayTimeSlots
              where c.RoundType == "Back 9"
              select c);
grvF2.Columns.Clear();
grvF2.DataSource = query2.ToList();
grvF2.Columns.Add(new BoundField { DataField = "TeeTime", HeaderText = "Tee-Off Time" });
grvF2.Columns.Add(new CommandField { HeaderText = "Select", SelectText = "Select Time", ButtonType = ButtonType.Link, ShowSelectButton = true });
grvF2.DataKeyNames = new string[] { "TimeID" };
grvF2.DataBind();

我的问题,我想只显示数据字段TeeTime =“something”时的命令字段,当TeeTime =“somethinig else”时我不想显示命令字段。

如果尝试过几乎所有事情,但我无法得到解决方案。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

在GridView RowDataBound中,您可以找到LinkButton并制作Visible=false

protected void grvF2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow gvr = e.Row as GridViewRow;
    if (gvr != null && gvr.RowType == DataControlRowType.DataRow)
    {
        LinkButton lb = gvr.Cells[1].Controls[0] as LinkButton;
        if (lb != null && gvr.Cells[0].Text.ToLower() != "something")
        {
            lb.Visible = false;
        }
    }

}

假设您为GridView

添加了此标记
<asp:GridView ID="grvF2" runat="server" 
    AutoGenerateColumns="false" OnDataBound="grvF2_DataBound"     
    OnRowDataBound="grvF2_RowDataBound" >
</asp:GridView>

输出可能如下:

enter image description here