Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>

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...
Posted
Updated 19-Feb-12 21:59pm
v2
Comments
Varun Sareen 20-Feb-12 4:00am    
edit for: applied pre tag

1 solution

U have done some small mistack like using
HTML
<columns>
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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900