Click here to Skip to main content
15,078,241 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test_Page.aspx.cs" Inherits="Grid_Data.Test_Page" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 113px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <table style="width: 61%;">
            <tr>
                <td class="style1">
                    <asp:Label ID="DeptLabel" runat="server" Text="Employee Name :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="nameTextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1" >
                    <asp:Label ID="EMPLabel" runat="server" Text="Emp_ID"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :</td>
                <td>
                    <asp:TextBox ID="empidTextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1" >
                    <asp:Label ID="DeptmntLabel" runat="server" Text="Department"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :</td>
                <td>
                    <asp:DropDownList ID="Deptdrop" runat="server">
                        <asp:ListItem>HR</asp:ListItem>
                        <asp:ListItem>ATM</asp:ListItem>
                        <asp:ListItem>R&amp;D</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style1" >
                    <asp:Label ID="DesigLabel" runat="server"
                        Text="Designation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="Desigdrop" runat="server">
                        <asp:ListItem>AVP</asp:ListItem>
                        <asp:ListItem>CTO</asp:ListItem>
                        <asp:ListItem>DCTO</asp:ListItem>
                        <asp:ListItem>First Executive Officer</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>

        </table>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <br />
        <asp:Label ID="dept" runat="server" Text="[dept]"></asp:Label>
        <asp:Label ID="desLabel" runat="server" Text="[des]"></asp:Label>
        <br />
        <div>
           <asp:GridView ID="showinfogrid" runat="server" AutoGenerateColumns="False"
    Width="100%">
           <Columns>
            <asp:BoundField DataField ="Name" HeaderText ="Employee_Name" ReadOnly="true" />
            <asp:BoundField DataField ="Employee_ID" HeaderText ="Emp_ID" ReadOnly="true"/>
            <asp:BoundField DataField ="Department" HeaderText ="Department" ReadOnly="true"/>
            <asp:BoundField DataField ="Designation" HeaderText ="Designation" ReadOnly="true"/>
           </Columns>
       </asp:GridView>
        </div>


    </div>
    </form>
</body>
</html>


----------------------------------
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            dept.Text=Deptdrop.SelectedItem.Text;
            desLabel.Text = Desigdrop.SelectedItem.Text;
            DataTable dt = new DataTable();
            this.showinfogrid.Visible = true;
            dt.Columns.Add("Name");
            dt.Columns.Add("Employee_ID");
            dt.Columns.Add("Department");
            dt.Columns.Add("Designation");
            dt.Rows.Add(nameTextBox1.Text.Trim(), empidTextBox2.Text.Trim(), Deptdrop.SelectedItem.Text, Desigdrop.SelectedItem.Text);
            showinfogrid.DataSource = dt;
            showinfogrid.DataBind();

        }
    }
}
Posted
Comments
Karthik_Mahalingam 5-Dec-13 3:11am
   
pls check this

http://www.codeproject.com/Questions/692479/HOW-Add-row-in-grid-view

Please dont make multiple question, try to followup with the same question.. and if u need add some question.. u can do it by improve question widget..

Put your gridview inside a update panel and set the panel trigger to your button. Here is an article on MSDN[^] to help you out.

Good luck,
OI
   
Anyone tell me how i add row each time in the grid view when i click button without refreshing the page but the new row add after the previous row
   
Comments
Karthik_Mahalingam 5-Dec-13 3:09am
   
Hi Kawshik pls add your comments in "Have a Question or Comment?" section..

this block is only for solutions...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900