namespace Gridwebpart.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SPWeb site = SPContext.Current.Web;
SPList list = site.Lists["GridList"];
SPListItemCollection items = list.Items;
DataTable dt = items.GetDataTable();
Gridnames.DataSource = dt;
Gridnames.DataBind();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
SPWeb site = SPContext.Current.Web;
SPList list = site.Lists["GridList"];
SPListItemCollection items = list.Items;
//SPListItem item = list.GetItemById(1);
SPListItem item = items.Add();
item["Title"] = txtName.Text;
item["Description"] = txtDescription.Text;
item["IS Sharepoint 2010 Developer"] = chkbox.Checked;
item.Update();
site.AllowUnsafeUpdates = false;
txtName.Text = "";
txtDescription.Text = "";
chkbox.Checked=false;
items = list.Items;
DataTable dt = items.GetDataTable();
Gridnames.DataSource = dt;
Gridnames.DataBind();
}
}
}
Tell me more
×
SharePoint Stack Exchange is a question and answer site for
SharePoint enthusiasts. It's 100% free, no registration required.
|
|
||||
|
|
|
I am not sure whats best but you can use SPQuery (only for reading) as well to get Data out of sharepoint, however if you want someone to look at your code and improve it then I would suggest you try this question on http://codereview.stackexchange.com/ hope it helped ;) Just realized your not disposing your SharePoint objects in code, I would recommend you to look at this MSDN article |
|||||||||
|