insert data into sharepoint list using gridview

Advertisement
I will expalin how to insert data into sharepoint list using gridview.
Now i am taking a Visual webpart,in this .ascx i am designing the screen with Employee Information like Name,Location,Phoneno,Designation.
<asp:GridView ID="gridinsertdata" runat="server" AutoGenerateColumns="False"
    CellPadding="4"   ShowFooter="True"
    onrowcommand="gridinsertdata_RowCommand" Width="700px">
        <Columns>
            <asp:TemplateField>
            <HeaderTemplate>
            <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td width="20%">
            Name
            </td>
             <td width="20%">
            Location
            </td>
             <td width="40%">
           Phoneno
            </td>
                <td width ="40%" >

                    Designation
                </td>
            </tr>
            </table>
            </HeaderTemplate>
            <ItemTemplate >
            <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td align="center" width="20%">
            <%#Eval("Name") %>
            </td>
              <td align="center" width="20%">
             <%#Eval("Location") %>
            </td>
             <td align="center" width="40%">
            <%#Eval("Phoneno") %>
            </td>
                   <td align="center" width="40%">
            <%#Eval("Designation") %>
            </td>
            </tr>
            </table>
            </ItemTemplate>
            <FooterTemplate>
            <table  cellpadding="0" cellspacing="0">
            <tr>
            <td align="center" width="20%">
            <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
            <td align="center" width="40%">
            <asp:TextBox ID="txtlocation" runat="server"></asp:TextBox>
            </td>
            <td align="center" width="40%">
            <asp:TextBox ID="txtphoneno" runat="server"></asp:TextBox>
                </td>
                   <td align="center" width="40%">
            <asp:TextBox ID="txtdesignation" runat="server"></asp:TextBox>
                       </td>
                <td>
                       <asp:Button ID ="btnSave" runat ="server" Text ="Save" CommandName ="Insert" />
                </td>
                       </tr>
            </table>
            </FooterTemplate>
            </asp:TemplateField>
        </Columns>
     
        </asp:GridView>

Now in .cs  writing the logic as shown below.

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                bindGrid();
            }
        }

        public void bindGrid()
        {

            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["EmployeeInfo"];
            SPListItemCollection listcoll = list.Items;
            gridinsertdata.DataSource = listcoll.GetDataTable();
            gridinsertdata.DataBind();
        }
        protected void gridinsertdata_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if(e.CommandName == "Insert")
            {

                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists["EmployeeInfo"];
                SPListItemCollection myColl = list.Items;
                TextBox txtname = (TextBox)gridinsertdata.FooterRow.FindControl("txtname");
                TextBox txtlocation = (TextBox)gridinsertdata.FooterRow.FindControl("txtlocation");
                TextBox txtphoneno = (TextBox)gridinsertdata.FooterRow.FindControl("txtphoneno");
                TextBox txtdesignation = (TextBox)gridinsertdata.FooterRow.FindControl("txtdesignation");
                SPListItem item = myColl.Add();
                item["Name"] = txtname.Text;
                item["Location"] = txtlocation.Text;
                item["Phoneno"] = txtphoneno.Text;
                item["Designation"] = txtdesignation.Text;
                item.Update();

                bindGrid();

            }

        }

Output:


SharePoint Online Real Time Training Contact: JLAKSHMITULASI@GMAIL.COM
Advertisements
SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

5 comments:

  1. I'm trying to use the code above, but I get the error: "the name 'gridinsertdata' does not exist in the current context".

    What have I missed?

    ReplyDelete
  2. Can You Please Check Your GridView ID.

    ReplyDelete
  3. I every time spent my half an hour to read this wweb site's content daily along with a cup of coffee.


    Feel free to visit my web site; desert safari dubai

    ReplyDelete
  4. Heya i am for the first time here. I found this board and I find It really useful & it
    helped me out a lot. I hope to give something back and
    help others like you helped me.

    Also visit my blog post :: cialis

    ReplyDelete
  5. I have created one sharepoint list and one visual web part by using above code but it is not working at all. list names are same as above i haven't modified the code it doesn't giving error but it display nothing

    ReplyDelete