Friday, February 18, 2011

How To bind sharepoint List with Grid view in Webpart- Part 2 Bind using List and Query on the list

Question : How To bind sharepoint List with Grid view in Webpart ? Part 2 Bind using List and Query on the listSolution :  for this we have add reference system.data.dll .  Bind using List and Query on the list

private void BindGridWithList() {
SPGridView oGrid = new SPGridView();
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["Personnel"];
            SPQuery myquery = new SPQuery();
            myquery.Query = "<Where><Neq><FieldRef Name='EmployeeName' /><Value Type='Text'> </Value></Neq></Where><OrderBy><FieldRef Name='EmployeeName' Ascending='True' /></OrderBy>";
            SPListItemCollection myitemcol = list.GetItems(myquery);
            DataTable table = new DataTable();
            table.Columns.Add("EmployeeName");
            foreach (SPListItem item in myitemcol){
                DataRow row = table.NewRow();
                row["EmployeeName"] = item["EmployeeName"].ToString();
  //next row(s) and add these rows
                table.Rows.Add(row);
            }
            DataView mydataview = new DataView(table);
            table = mydataview.ToTable(true, "EmployeeName");
            oGrid.DataSource = table;
            oGrid.DataBind();

        }

No comments:

Post a Comment