Facing error both DataSource and And DataSourceID are defined on GridView Entity Framework

点点圈 提交于 2019-12-23 23:50:56

问题


i am using entity frame work and want to bind data on Grid View but facing problem i have code that i am pasting as well as attaching screen shot i also saw answer regarding this problem but not beneficial for me so any one have experience with this error must be appreciated.

aspx.cs Code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
        lblMessage.Text = "";
    }

    void BindGrid()
    {
        using (GapEntities1 context = new GapEntities1())
        {
            if (context.Organizations.Count() > 0)
            {
                // GdvOrganization is a gridview ID name
                GdvOrganization.DataSource = context.Organizations;
                GdvOrganization.DataBind();
            }
        }
    }

回答1:


Seems like you are trying to bind the GridView from Markup side (.aspx ) as well as using code behind.( .aspx.cs )

Choose any one way only to bind the grid.

1.) If you bind gridview from code behind then remove the DataSourceId property from grid view from markup. Change below code:

<asp:gridview id="GdvOrganization" runat="server" autogeneratecolumns="False" 
DataSourceID="MyDataSource">

to

<asp:gridview id="GdvOrganization" runat="server" autogeneratecolumns="False">

2.) if you prefer to bind from markup side then you have to remove the c# code to bind the grid.

Still if above 2 steps doesn't interest you, try below trick ( Recommended ?? )

GdvOrganization.DataSource = ds;
GdvOrganization.DataSourceID = String.Empty;
GdvOrganization.DataBind();


来源:https://stackoverflow.com/questions/22248048/facing-error-both-datasource-and-and-datasourceid-are-defined-on-gridview-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!