C# – display sharepoint list value in visual web part

csharepoint-2010web-parts

I'd like to know how to get values from SharePoint list column in custom WebPart and I want to display it in the visual webPart.

 splist lists = web.list["list name"];
 spitem item = list.additem();
 item["Title"] = "doc";
 item["No"] = "1";'

I can use code something similar to the above to assign value in list column from visual WebPart. Is there a solution for the other way around — to get the value from SharePoint list and display it in visual WebPart?

Best Answer

there are lots of examples to populate a literal,label etc with the content of a list. you also might want to filter what you want to display. This is some code to get you started, if you require a more precise result just let us know what is the final result you wish to achieve:

 using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList lists = web.Lists["list name"];
                    foreach (SPListItem itemin lists.Items)
                    {
                       string test = Convert.ToString( item["test"]);
                        TextBox1.Text = test;

                    }
                }
             }
Related Topic