Insert TextBox Data Into Gridview in Asp.net by Using C# Coding


Add Multiple records to Gridview with textboxes

In this article i'm going to explain about how to add textboxes data into gridview instead of storing single record to database. Here i'm using gridview control to hold employees records one by one, let me show you how to achieve this programmatically.

HTML Markup : Design Page

drag and drop Textbox control's and Griview Control to design as shown in above image.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InsertGridviewRecordtoDataBase.aspx.cs" Inherits="InsertGridviewRecordtoDataBase" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center" style="margin-top:50px;">
     <table align="center" style="border-collapse:collapse; width:500px;">
      <tr>
       <th align="left" colspan="4" style="color:#86ef12; border:1px solid #86ef12; padding:5px 10px;">Add Employees Details</th>        
      </tr>
      <tr>
       <td style="border:1px solid #86ef12;padding:5px 10px;"> Employ ID :</td>
       <td style="border:1px solid #86ef12;"><asp:TextBox ID="txtempid" runat="server"></asp:TextBox></td>
      </tr>      
      <tr>
       <td style="border:1px solid #86ef12;padding:5px 10px;"> Employ Name :</td>
       <td style="border:1px solid #86ef12;"><asp:TextBox ID="txtempname" runat="server"></asp:TextBox></td>
      </tr>      
      <tr>
       <td style="border:1px solid #86ef12;padding:5px 10px;"> Employ Salary :</td>
       <td style="border:1px solid #86ef12;"><asp:TextBox ID="txtempsalary" runat="server"></asp:TextBox></td>
      </tr>
      <tr>
       <td colspan="4" style="padding:5px 10px; text-align:center;border:1px solid #86ef12;">
        <asp:Button ID="btn_add" runat="server" Text=" ADD " onclick="btn_add_Click" />
       </td>
      </tr>
     </table>
     <br />
     <asp:GridView ID="GridView1" runat="server" BackColor="White" 
            BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            ForeColor="Black" GridLines="Vertical">
         <AlternatingRowStyle BackColor="White" />
         <FooterStyle BackColor="#CCCC99" />
         <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
         <RowStyle BackColor="#F7F7DE" />
         <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
         <SortedAscendingCellStyle BackColor="#FBFBF2" />
         <SortedAscendingHeaderStyle BackColor="#848384" />
         <SortedDescendingCellStyle BackColor="#EAEAD3" />
         <SortedDescendingHeaderStyle BackColor="#575357" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 

C# Coding : Add Textbox Data into Gridview



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class InsertGridviewRecordtoDataBase : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("EmpID");
        dt.Columns.Add("EmpName");
        dt.Columns.Add("EmpSal");
        DataRow dr = null;
        if (ViewState["emp"] != null)
        {
            for (int i = 0; i < 1; i++)
            {
                dt = (DataTable)ViewState["emp"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr["EmpID"] = txtempid.Text;
                    dr["EmpName"] = txtempname.Text;
                    dr["EmpSal"] = txtempsalary.Text;
                    dt.Rows.Add(dr);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
        else
        {
            dr = dt.NewRow();
            dr["EmpID"] = txtempid.Text;
            dr["EmpName"] = txtempname.Text;
            dr["EmpSal"] = txtempsalary.Text;
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        ViewState["emp"] = dt;

    }
}

Video Tutorial :

0 Komentar untuk "Insert TextBox Data Into Gridview in Asp.net by Using C# Coding"

Back To Top