Use this code in .aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" Width="189px"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Width="179px"></asp:TextBox>
< asp:TextBox ID="TextBox3" runat="server" Height="22px" Width="222px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand"
onrowdeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btndelete" runat="server" Text="delete" CommandName="delete"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
CodeBehind:
static DataTable dtValues = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateDataTable();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataRow drValues = dtValues.NewRow();
drValues[0] = TextBox1.Text;
drValues[1] = TextBox2.Text;
drValues[2] = TextBox3.Text;
drValues[3] = TextBox4.Text;
dtValues.Rows.Add(drValues);
fillgrid();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
public void fillgrid()
{
GridView1.DataSource = dtValues;
GridView1.DataBind();
}
protected void CreateDataTable()
{
dtValues.Columns.Add("eno");
dtValues.Columns.Add("name");
dtValues.Columns.Add("sal");
dtValues.Columns.Add("phoneno");
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
GridViewRow d = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int index = d.RowIndex;
dtValues.Rows[index].Delete();
fillgrid();
}
}
No comments:
Post a Comment