adding rows in gridview

asp.net , db Tirupati, India
  • 13 years ago

    Hi...

         Iam using GridView in asp.net(2.0) .My requirement is user can add,update,delete 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" AutoGenerateColumns="False" CellPadding="3" Height="237px" Style="z-index: 100; left: 155px;
                position: absolute; top: 132px" Width="688px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                <FooterStyle BackColor="White" ForeColor="#000066" />
                <RowStyle ForeColor="#000066" />
                <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                <Columns>
               <asp:TemplateField>
               <HeaderTemplate>
               <asp:CheckBox ID="chk" runat="server" />
               </HeaderTemplate>
               <ItemTemplate>
               <asp:CheckBox ID="chk1"  runat="server" />
               </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Emp ID">
              <ItemTemplate>
              <asp:Label ID="lbl1" runat="server" Text='<%#Eval("EID") %>' ></asp:Label>
              </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Emp Name">
              <ItemTemplate>
               <asp:Label ID="lbl2" runat="server" Text='<%#Eval("EName") %>' ></asp:Label>
              </ItemTemplate>
              <EditItemTemplate>
              <asp:TextBox ID="tb1" Text='<%#Eval("EName") %>' runat="server"></asp:TextBox>
              </EditItemTemplate>
               </asp:TemplateField>
              <asp:TemplateField HeaderText="Emp Dept">
              <ItemTemplate>
              <asp:Label ID="lbl3" runat="server" Text='<%#Eval("EDept") %>' ></asp:Label>
              </ItemTemplate>
              <EditItemTemplate>
               <asp:TextBox ID="tb2" Text='<%#Eval("EDept") %>' runat="server"></asp:TextBox>
              </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="City">
              <ItemTemplate>
            <asp:Label ID="lbl4" runat="server" Text='<%#Eval("City") %>' ></asp:Label>
              </ItemTemplate>
              <EditItemTemplate>
               <asp:TextBox ID="tb3" Text='<%#Eval("City") %>' runat="server"></asp:TextBox>
              </EditItemTemplate>
               </asp:TemplateField>
               <asp:CommandField  ShowEditButton=True ShowDeleteButton=True  CancelText="Quit" />
                </Columns>
            </asp:GridView>


    CodeWindow:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    public partial class grid : System.Web.UI.Page
    {
        SqlConnection cn;
        DataSet ds;
        SqlDataAdapter da;
        SqlCommandBuilder bldr;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            getdata();
        }
        public void getdata()
        {
            cn = new SqlConnection("user id=sa;password=connex;database=santhosh;data source=BLRWS06CONNEXIN\\SQLEXPRESS");
            cn.Open();
            ds = new DataSet();
            da = new SqlDataAdapter("select * from empdetails", cn);
            da.Fill(ds, "details");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            getdata();

        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            getdata();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label l1 = (Label)GridView1.Rows[e.RowIndex].FindControl("lbl1");
            TextBox t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb1");
            TextBox t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb2");
            TextBox t3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb3");


            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=BLRWS06CONNEXIN\\SQLEXPRESS");
            cn.Open();
            SqlCommand cmd = new SqlCommand(str, cn);
            cmd.ExecuteNonQuery();
            GridView1.EditIndex = -1;
            getdata();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label l1 = (Label)GridView1.Rows[e.RowIndex].FindControl("lbl1");
            TextBox t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb1");
            TextBox t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb2");
            TextBox t3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb3");


            string str = "delete from empdetails where EID=" + l1.Text;

            cn = new SqlConnection("user id=sa;password=connex;database=santhosh;data source=BLRWS06CONNEXIN\\SQLEXPRESS");
            cn.Open();
            SqlCommand cmd = new SqlCommand(str, cn);
            cmd.ExecuteNonQuery();
            GridView1.EditIndex = -1;
            getdata();
        }
    }

  • 13 years ago

    Surely its just a case of writing an Insert statement and adding it to the DataAdapter.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski