Friday, February 18, 2011

How To bind sharepoint List with Grid view in Webpart?

Question : How To bind sharepoint List with Grid view in Webpart?

Solution 1: Simple bind

private void BindGridWithList() {
            SPGridView oGrid = new SPGridView();
            SPWeb CurrentSite = SPContext.Current.Web;
            SPList PersonnelList = CurrentSite.Lists["Personnel"];
           
            BoundField colEmployeeName = new BoundField();
            BoundField colEmployeeGroup = new BoundField();
            BoundField colEmployeeJobRole = new BoundField();
                                 
            colEmployeeName.DataField = "EmployeeName";           
            colEmployeeName.HeaderText = "EmployeeName";
            colEmployeeGroup.DataField = "EmployeeGroup";
            colEmployeeGroup.HeaderText = "EmployeeGroup";
            colEmployeeJobRole.DataField = "EmployeeJobRole";
            colEmployeeJobRole.HeaderText = "EmployeeJobRole";           
            oGrid.Columns.Add(colEmployeeName);
            oGrid.Columns.Add(colEmployeeGroup);
            oGrid.Columns.Add(colEmployeeJobRole);
            SPDataSource PersonnelDataSource = new SPDataSource();
           
            PersonnelDataSource.List = PersonnelList;
            oGrid.AutoGenerateColumns = false;
            oGrid.DataSource = PersonnelDataSource;
            oGrid.DataBind();
        }

1 comment: