0
Answer

Dynamically GridView code behind

mary jean ligas

mary jean ligas

12y
1.3k
1
Hello Everyone,

Please help me how to create a gridview dynamically..I have ab HTML TABLE code here:
how can i revert this in a gridview

 protected void DisplayUser()
        {
            try
            {
                CLMSDataContext context = new CLMSDataContext();

                string query = "select User_Idx, LastName, FirstName, MiddleInitial, NickName, PosLoc_Abbreviation, DateTimeRegistered from Dim_User " + Environment.NewLine +
                                "where PosLoc_Abbreviation is not null" + Environment.NewLine +            
                                "order by DateTimeRegistered desc";
                             
               

                List<tmp_Users> TmpUser = context.ExecuteQuery<tmp_Users>(query).ToList();

                foreach (tmp_Users u in TmpUser)
                {
                   
                        var created = context.DateInterval(u.DateTimeRegistered);
                        foreach (var di in created)
                        {
                            var post = (from p in context.Fact_Contract_Threads
                                        where p.User_PIdx == u.User_Idx
                                        select new { p.Message }).Count();
                           
                            string s = di.Assigned.ToString();
                            tbl_DisplayUser("Content", u.LastName + ", &nbsp" + u.FirstName + " " + u.MiddleInitial + ". ", "tblDisplayUser", "TimeAssigned", "also known as ",
                                        "tblDisplayUser", "Content", u.NickName + ", " + u.PosLoc_Abbreviation, "tblDisplayUser",
                                        "View", "<a href=EditUser.aspx?id=" + u.User_Idx + " + class=View>&nbsp&nbsp&nbsp[edit]</a> [deactivate]" + "<br />", "tblDisplayUser",
                                        "TimeAssigned", "Created " + di.Assigned + " ago | " + "Active |" + " Posts"+ "("+post +")" , "tblDisplayUser");
                       
                            //string role = "SELECT usr.SMTPAddress, roles.Plant_Name, roles.Dept_Name, roles.Role_Name" + Environment.NewLine +
                            //           " FROM Dim_User usr " + Environment.NewLine +
                            //           " join ( " + Environment.NewLine +
                            //           " SELECT distinct User_PIdx, Role_Name, dept.Dept_Name, loc.Plant_Name, rol.Role_Idx FROM Dim_User_Role usrol " + Environment.NewLine +
                            //           " join  Dim_Role rol on (rol.Role_Idx = usrol.Role_PIdx) " + Environment.NewLine +
                            //           " join Dim_Department dept on (dept.Dept_Idx = usrol.Dept_PIdx) " + Environment.NewLine +
                            //           " join Dim_Location_Plant loc on  (loc.Plant_Idx = usrol.Plant_PIdx) " + Environment.NewLine +
                            //           " ) [roles] " + Environment.NewLine +
                            //           " on roles.User_PIdx = usr.User_Idx  " + Environment.NewLine +
                            //           " where usr.User_Idx='"+ u.User_Idx +"' ";


                            //List<tmp_Roles> TmpRole = context.ExecuteQuery<tmp_Roles>(role).ToList();

                            //foreach (tmp_Roles r in TmpRole)
                            //{
                            //    tbl_DisplayRole("SubFont", r.Role_Name + " |" + r.Plant_Name + " |" + r.Dept_Name + " |" + r.SMTPAddress + " |" , "tblDisplayUser");
                            //}                  
                        }
                  }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void tbl_DisplayUser(string clmsClass, string clmsValue, string tableName,
                                       string clmsClass2, string clmsValue2, string tableName2,
                                       string clmsClass3, string clmsValue3, string tableName3,
                                       string clmsClass4, string clmsValue4, string tableName4,
                                       string clmsClass5, string clmsValue5, string tableName5)
                                    
        {
            try
            {

                HtmlTableRow htRow = new HtmlTableRow();
                HtmlTableCell htCell = new HtmlTableCell();
               
                Label lbContent = new Label();
                lbContent.CssClass = clmsClass;
                lbContent.Text = clmsValue;
                htCell.Controls.Add(lbContent);
                htRow.Cells.Add(htCell);

                Label lbContent2 = new Label();
                lbContent2.CssClass = clmsClass2;
                lbContent2.Text = clmsValue2;
                htCell.Controls.Add(lbContent2);
                htRow.Cells.Add(htCell);

                Label lbContent3 = new Label();
                lbContent3.CssClass = clmsClass3;
                lbContent3.Text = clmsValue3;
                htCell.Controls.Add(lbContent3);
                htRow.Cells.Add(htCell);

                Label lbContent4 = new Label();
                lbContent4.CssClass = clmsClass4;
                lbContent4.Text = clmsValue4;
                htCell.Controls.Add(lbContent4);
                htRow.Cells.Add(htCell);

                Label lbContent5 = new Label();
                lbContent5.CssClass = clmsClass5;
                lbContent5.Text = clmsValue5;
                htCell.Controls.Add(lbContent5);
                htRow.Cells.Add(htCell);
               

                if (tableName == "tblDisplayUser")
                {
                    tblDisplayUser.Rows.Add(htRow);
                }
                else
                {
                    tblDisplayUser.Rows.Add(htRow);
                }

            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }
        }

        protected void tbl_DisplayRole(string clmsClass, string clmsValue, string tableName)
                                      
        {
            try
            {

                HtmlTableRow htRow = new HtmlTableRow();
                HtmlTableCell htCell = new HtmlTableCell();

                Label lbContent = new Label();
                lbContent.CssClass = clmsClass;
                lbContent.Text = clmsValue;
                htCell.Controls.Add(lbContent);
                htCell.Style.Add("padding-left", "10px");
                htRow.Cells.Add(htCell);

                if (tableName == "tblDisplayUser")
                {
                    tblDisplayUser.Rows.Add(htRow);
                }
                else
                {
                    tblDisplayUser.Rows.Add(htRow);
                }


            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }
        }