C# .NET - Enter data in grid view problem. - Asked By suresh kotte on 05-Jan-12 01:37 AM

Hai Everyone,

           I enter the data in gridview rows and save it in database. I try but i dont know how to enter the data in Gridview. Please slove this Problem.

muthuraman alexander replied to suresh kotte on 05-Jan-12 01:42 AM

have a text box inside the item template filed in the grid view control
when u enter the data in the text field..

get the grid control in the button click event and convert the data in to datatable
now u can save the data in to database easily

hope this idea will help you

Riley K replied to suresh kotte on 05-Jan-12 01:54 AM

Place a textbox in the footertemplate and enter into database

<asp:TemplateField ItemStyle-Width = "100px"  HeaderText = "Name">
    <asp:Label ID="lblEmpName" runat="server"
        Text='<%# Eval("EmpName")%>'></asp:Label>
    <asp:TextBox ID="txtEmpName" runat="server"
      Text='<%# Eval("EmpName")%>'></asp:TextBox>
    <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>

Place a button and on the click event write this code

protected void AddNewCustomer(object sender, EventArgs e)
    int EmpId = Convert.ToInt32( ((TextBox)GridView1.FooterRow.FindControl("txtEmpID")).Text);
    string EmpName = ((TextBox)GridView1.FooterRow.FindControl("txtEmpName")).Text;
    int salary = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtSalary")).Text);
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "Insert into Emp(EmpId,EmpName,Salary)" +
    cmd.Connection = con;
    cmd.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId;
    cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = EmpName;
    cmd.Parameters.Add("@Salary", SqlDbType.Int).Value = salary;

dipa ahuja replied to suresh kotte on 05-Jan-12 02:53 AM
Step 1 : Add new template Field With the Required Controls:
  Name:<asp:TextBox ID="txtname" runat="server" /><br />
  Gender :
  <asp:DropDownList ID="ddGender" runat="server">
  <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
► Step 2 Implement the Button's Click event:
protected void btnInsert_Click(object sender, EventArgs e)
  string name = ((TextBox)GridView1.FooterRow.FindControl("txtname")).Text;
  string gender = ((DropDownList)GridView1.FooterRow.FindControl("ddGender")).SelectedValue;
  string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection conn = new SqlConnection(ConnString);
  string q = "insert into people (name,gender) values(@name,@gender)";
  SqlCommand comm = new SqlCommand(q,conn);
  comm.Parameters.AddWithValue("name", name);
  comm.Parameters.AddWithValue("gender", gender);
  BindGrid(); //Re-bind Gridview
Jitendra Faye replied to suresh kotte on 05-Jan-12 04:44 AM
GridView doesn't provide inbuilt functionality for inserting record, you have to implement this feature
by own.

GridView after clicking Add New Record button

Follow this link also-
kalpana aparnathi replied to suresh kotte on 05-Jan-12 04:56 AM
Try this code:

    <asp:GridView ID="gv" DataKeyNames="id" runat="server" SkinID="gvSkinSmall"
      AutoGenerateColumns="false" AllowPaging="false" AllowSorting="false">
        <asp:BoundField DataField="id" HeaderText="InOutSet_Definition_Id" Visible="true" />
        <asp:TemplateField HeaderText="Name">
            <asp:Label ID="txtName" runat="server" MaxLength="40" Text='<%# eval("Name") %>'></asp:Label>
        <asp:TemplateField HeaderText="IsDelete">
              <asp:TextBox ID="txt1"  MaxLength="40"
      <SelectedRowStyle BackColor="Silver" />
  <asp:LinkButton CssClass="button" ID="btnSave" runat="server">
          <span runat="server" id="spBtnSave" style="width: 60px; text-align: center">Save
         <asp:LinkButton ID="btnCancel" runat="server" CssClass="button">
          <span runat="server" id="spBtnCancel" style="width: 60px; text-align: center">Cancel
and before bind to grid delete rows where isdelete=false as:
for (int i=0; i<dt.Rows.Count; i++)
  if (dt.Rows[i]["isdelete"].ToString() == 'false")
and for saving data to database see:
protected void Button1_Click(object sender, EventArgs e)
    for (int count = 0; count < GridView1.Rows.Count; count++)
       //you can get the bound filed values like
      string strId = GridView1.Rows[count].Cell[0].Text;
      //to get the template field value
      TextBox tx1 = (TextBox)GridView1.Rows[count].FindControl("txt1");
      string str1 = tx1.Text;
      //you can insert the values here