将数据库上下文绑定到 网格视图并显示数据

本文关键字:视图 显示 数据 网格 数据库 上下文 绑定 | 更新日期: 2023-09-27 18:35:55

我有一个网格视图:

  <asp:GridView ID="ParentSelect" runat="server" AutoGenerateColumns="false" OnRowCommand="ParentSelect_RowCommand" OnRowCreated="ParentSelect_RowCreated" emptydatatext="Please Submit A Clip. C'mon dude." ShowHeaderWhenEmpty="true" HorizontalAlign="Center" Width="600" CssClass= "table table-striped table-bordered table-condensed">
    <HeaderStyle BorderColor="Black"   />
    <Columns>
        <asp:BoundField DataField ="dbContext.Mains.VideoUrl" HeaderText="Title" Visible="false" />
    </Columns>
</asp:GridView>

并且我已经绑定了我的数据库上下文:

       protected void LoadGrid()
    {
        ParentSelect.DataSource = dbContext.Mains.ToList(); 
        ParentSelect.DataBind();
    }

如何显示没有来自数据库的列名的列。

例如,当我将自动生成列设置为"true"时,它将显示数据库中的列名,但相反,我想在具有不同名称的列中显示相同的数据。

所以我想以某种方式掩盖它而不是 VideoUrl(db 列的名称),以便数据来自 videourl,但它有另一个名称,如"URL"。

感谢您的帮助!

将数据库上下文绑定到 网格视图并显示数据

您可以在 RowDataBound 事件上处理它

protected void ParentSelect_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
        e.Row.Cells[2].Text = "URL"; 
    } 
}