错误:诸如Eval(),XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用.

问题描述:

我需要将数据库中的值绑定到网格视图.为此,我首先将数据库的值绑定到列表中.然后我需要将其绑定到网格视图...以将值获取到列表中,我使用了填充代码

i need to bind values from database to a grid view.for that first i got the databse values to a list.then i need to bind it to a gridview...to get the values to the list i used the fillowing code

public List<gridviewdisplay> Getgridview()
        {
            SqlCommand cmdSql = new SqlCommand();
            DataSet dstResult = new DataSet();
            List<gridviewdisplay> gridviewMdlLst = new List<gridviewdisplay>();
            gridviewdisplay gridviewMdl = null;

          
            cmdSql.CommandType = CommandType.Text;
            cmdSql.CommandText = "SELECT firstname,lastname,siteid,topfunctionid,subfunctionid,kmlevel,lmsroleid,channelid FROM tbluserprofile";

            dstResult = EOSSQLExecutor.query(cmdSql, strSqlConnString);

            if (dstResult != null)
            {
                if (dstResult.Tables.Count > 0)
                {
                    for (int i = 0; i < dstResult.Tables[0].Rows.Count; i++)
                    {
                        gridviewMdl = new gridviewdisplay();
                        gridviewMdl.firstname = EOSSQLExecutor.ToString(dstResult.Tables[0].Rows[i][0]).Trim();
                        gridviewMdl.lastname = EOSSQLExecutor.ToString(dstResult.Tables[0].Rows[i][1]).Trim();
                        gridviewMdl.site.siteid = EOSSQLExecutor.ToInt32(dstResult.Tables[0].Rows[i][2]);
                        gridviewMdl.topfunction.topfunctionid = EOSSQLExecutor.ToInt32 (dstResult.Tables[0].Rows[i][3]);
                        gridviewMdl.subfunction.subfunctionid = EOSSQLExecutor.ToInt32 (dstResult.Tables[0].Rows[i][4]);
                        gridviewMdl.role.lmsroleid = EOSSQLExecutor.ToInt32 (dstResult.Tables[0].Rows[i][5]);
                        gridviewMdl.channel.channelid = EOSSQLExecutor.ToInt32 (dstResult.Tables[0].Rows[i][6]);
                        gridviewMdl.kmlevel = EOSSQLExecutor.ToInt32 (dstResult.Tables[0].Rows[i][7]);
                        gridviewMdlLst.Add(gridviewMdl);
                    }
                }
            }
            return gridviewMdlLst;
        }


该函数很好地返回了值....在前端,我首先创建了此类的对象以获取值...像这样...


This function is well returning the values....in the front end i jst created an object of this class to get the values...like this...

 public void getgridview()
    {
        List<gridviewdisplay> obj1 = new List<gridviewdisplay>();
        obj1 = bl.Getgridview();
        GridView1.DataSource = obj1;
        GridView1.DataBind();

//in the source file i edited my gridview1 as,


 <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" 
                        BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" 
                        CellSpacing="2" ForeColor="Black">
                        <rowstyle backcolor="White" />
                        <footerstyle backcolor="#CCCCCC" />
                        <pagerstyle backcolor="#CCCCCC" forecolor="Black" horizontalalign="Left" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    First Name
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("FirstName"); %>
                                </itemtemplate>
                            
                        </columns>
                         <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Last Name
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("Lastname"); %>
                                </itemtemplate>
                            
                        </columns>
                         <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Site 
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("site.siteid"); %>
                                </itemtemplate>
                            
                        </columns>
                         <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Topfunction
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("Topfunction.topfunctionid"); %>
                                </itemtemplate>
                            
                        </columns>
                         <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    subfunction
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("Subfunction.subfunctionid"); %>
                                </itemtemplate>
                            
                        </columns>
                         <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    role
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("role.lmsroleid"); %>
                                </itemtemplate>
                            
                        </columns>
                         <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    channel
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("channel.channelid"); %>
                                </itemtemplate>
                            
                        </columns>
                         <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    kmlevel
                                </HeaderTemplate>
                                <itemtemplate>
                                    <%Eval("kmlevel"); %>
                                </itemtemplate>
                            
                        </columns>


它在运行程序时显示错误,因为诸如Eval(),XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用.任何人都可以帮助我...


its showing error while running the program as Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.can anyone plss help me...

U做了一些小小的尝试,例如使用
U have done some small mistack like using
<columns>

对于每个列,请参见此内容.

希望对您有帮助:

将数据绑定到自定义对象 [在GridView Web服务器控件中创建自定义列 [

for each Column Refer this.

I hope this will help U:

Data Binding to Custom Objects[^]

Creating a Custom Column in a GridView Web Server Control[^]