This
blog we discussed how to insert data in gridview within grid using asp.net.
For do this using footer temple within
gridview.
Gridinsert.aspx
code:-
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="mylink" runat="server" CommandName="select">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="mylbl" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mytxtid" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="myname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="mycity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Gridinset.aspx.cs page:-
public partial class gridinsert : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sai"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
bindit();
}
}
protected void bindit()
{
DataTable dt = new DataTable();
SqlDataAdapter sdr = new SqlDataAdapter("select * from
myadd", con);
con.Open();
sdr.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
TextBox txtid1 = (TextBox)GridView1.FooterRow.FindControl("mytxtid");
TextBox txtname1 = (TextBox)GridView1.FooterRow.FindControl("txtname");
TextBox txtcity1 = (TextBox)GridView1.FooterRow.FindControl("txtcity");
//insert
data
SqlCommand cmd = new SqlCommand("Insert into
myadd(Id,Name,City)values('" +
txtid1.Text + "','" + txtname1.Text + "','" + txtcity1.Text + "')",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
bindit();
}
}
Hope it will help you for insert data with in gridview.
Plz give comment.
No comments:
Post a Comment