Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Friday, August 10, 2012

Insertion into SharePoint List Using GridView

Ascx
<asp:GridView ID="dgvInsertRecord" runat="server" AutoGenerateColumns="False"
    CellPadding="4" ForeColor="#333333" GridLines="None"  ShowFooter="True"
    onrowcommand="dgvInsertRecord_RowCommand">
        <Columns>
            <asp:TemplateField>
            <HeaderTemplate>
            <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td width="25%">
            Fname
            </td>
             <td width="25%">
            City
            </td>
             <td width="50%">
           Telno
            </td>
            </tr>
            </table>
            </HeaderTemplate>
            <ItemTemplate >
            <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td align="center" width="25%">
            <%#Eval("Fname") %>
            </td>
              <td align="center" width="25%">
             <%#Eval("City") %>
            </td>
             <td align="center" width="50%">
            <%#Eval("Telno") %>
            </td>
            </tr>
            </table>
            </ItemTemplate>
            <FooterTemplate>
            <table  cellpadding="0" cellspacing="0">
            <tr>
            <td align="center" width="25%">
            <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
            </td>
            <td align="center" width="25%">
            <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
            </td>
            <td align="center" width="50%">
            <asp:TextBox ID="txtTelno" runat="server"></asp:TextBox>&nbsp;<asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Insert" />
            </td>
            </tr>
            </table>
            </FooterTemplate>
            </asp:TemplateField>
        </Columns>
            <RowStyle BackColor="#E3EAEB" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

Ascx.Cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace GridInsert.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Contacts"];
            SPListItemCollection myColl = lst.Items;
            dgvInsertRecord.DataSource = myColl.GetDataTable();
            dgvInsertRecord.DataBind();
        }

        protected void dgvInsertRecord_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Contacts"];
                SPListItemCollection myColl = lst.Items;
                TextBox txtFname1 = (TextBox)dgvInsertRecord.FooterRow.FindControl("txtFname");
                TextBox txtCity1 = (TextBox)dgvInsertRecord.FooterRow.FindControl("txtCity");
                TextBox txtTelno1 = (TextBox)dgvInsertRecord.FooterRow.FindControl("txtTelno");
                SPListItem item = myColl.Add();
                item["Fname"] = txtFname1.Text;
                item["City"] = txtCity1.Text;
                item["Telno"] = txtTelno1.Text;
                item.Update();
                txtFname1.Text = "";
                txtCity1.Text = "";
                txtTelno1.Text = "";
                BindData();
            }
        }
    }
}



No comments:

Post a Comment