Hi...
Iam using GridView in asp.net(2.0) .My requirement is user can add,update,dele te in gridview I written the code for add & update but can u help adding rows in grid view
My code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False" CellPadding="3" Height="237px" Style="z-index: 100; left: 155px;
position: absolute; top: 132px" Width="688px" BackColor="Whit e" BorderColor="#C CCCCC" BorderStyle="No ne" BorderWidth="1p x" OnRowCancelingE dit="GridView1_ RowCancelingEdi t" OnRowDeleting=" GridView1_RowDe leting" OnRowEditing="G ridView1_RowEdi ting" OnRowUpdating=" GridView1_RowUp dating">
<FooterStyle BackColor="Whit e" ForeColor="#000 066" />
<RowStyle ForeColor="#000 066" />
<SelectedRowSty le BackColor="#669 999" Font-Bold="True" ForeColor="Whit e" />
<PagerStyle BackColor="Whit e" ForeColor="#000 066" HorizontalAlign ="Left" />
<HeaderStyle BackColor="#006 699" Font-Bold="True" ForeColor="Whit e" />
<Columns>
<asp:TemplateFi eld>
<HeaderTemplate >
<asp:CheckBox ID="chk" runat="server" />
</HeaderTemplate>
<ItemTemplate >
<asp:CheckBox ID="chk1" runat="server" />
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Emp ID">
<ItemTemplate >
<asp:Label ID="lbl1" runat="server" Text='<%#Eval(" EID") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Emp Name">
<ItemTemplate >
<asp:Label ID="lbl2" runat="server" Text='<%#Eval(" EName") %>' ></asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="tb1" Text='<%#Eval(" EName") %>' runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Emp Dept">
<ItemTemplate >
<asp:Label ID="lbl3" runat="server" Text='<%#Eval(" EDept") %>' ></asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="tb2" Text='<%#Eval(" EDept") %>' runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cit y">
<ItemTemplate >
<asp:Label ID="lbl4" runat="server" Text='<%#Eval(" City") %>' ></asp:Label>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="tb3" Text='<%#Eval(" City") %>' runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateFie ld>
<asp:CommandFie ld ShowEditButton= True ShowDeleteButto n=True CancelText="Qui t" />
</Columns>
</asp:GridView>
CodeWindow:
using System;
using System.Data;
using System.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
public partial class grid : System.Web.UI.P age
{
SqlConnection cn;
DataSet ds;
SqlDataAdapter da;
SqlCommandBuild er bldr;
protected void Page_Load(objec t sender, EventArgs e)
{
}
protected void LinkButton1_Cli ck(object sender, EventArgs e)
{
getdata();
}
public void getdata()
{
cn = new SqlConnection(" user id=sa;password= connex;database =santhosh;data source=BLRWS06C ONNEXIN\\SQLEXP RESS");
cn.Open();
ds = new DataSet();
da = new SqlDataAdapter( "select * from empdetails", cn);
da.Fill(ds, "details");
GridView1.DataS ource = ds.Tables[0];
GridView1.DataB ind();
}
protected void GridView1_RowEd iting(object sender, GridViewEditEve ntArgs e)
{
GridView1.EditI ndex = e.NewEditIndex;
getdata();
}
protected void GridView1_RowCa ncelingEdit(obj ect sender, GridViewCancelE ditEventArgs e)
{
GridView1.EditI ndex = -1;
getdata();
}
protected void GridView1_RowUp dating(object sender, GridViewUpdateE ventArgs e)
{
Label l1 = (Label)GridView 1.Rows[e.RowIndex].FindControl("l bl1");
TextBox t1 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b1");
TextBox t2 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b2");
TextBox t3 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b3");
string str = "update empdetails set EName='" + t1.Text + "',EDept='" + t2.Text + "',City='" + t3.Text + "' where EID=" + l1.Text;
cn = new SqlConnection(" user id=sa;password= connex;database =santhosh;data source=BLRWS06C ONNEXIN\\SQLEXP RESS");
cn.Open();
SqlCommand cmd = new SqlCommand(str, cn);
cmd.ExecuteNonQ uery();
GridView1.EditI ndex = -1;
getdata();
}
protected void GridView1_RowDe leting(object sender, GridViewDeleteE ventArgs e)
{
Label l1 = (Label)GridView 1.Rows[e.RowIndex].FindControl("l bl1");
TextBox t1 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b1");
TextBox t2 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b2");
TextBox t3 = (TextBox)GridVi ew1.Rows[e.RowIndex].FindControl("t b3");
string str = "delete from empdetails where EID=" + l1.Text;
cn = new SqlConnection(" user id=sa;password= connex;database =santhosh;data source=BLRWS06C ONNEXIN\\SQLEXP RESS");
cn.Open();
SqlCommand cmd = new SqlCommand(str, cn);
cmd.ExecuteNonQ uery();
GridView1.EditI ndex = -1;
getdata();
}
}