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.


Thanks&Regrads
Suresh.K
muthuraman alexander replied to suresh kotte on 05-Jan-12 01:42 AM
hi

have a text box inside the item template filed in the grid view control
then
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

regards
 muthu
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">
  <ItemTemplate>
    <asp:Label ID="lblEmpName" runat="server"
        Text='<%# Eval("EmpName")%>'></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
    <asp:TextBox ID="txtEmpName" runat="server"
      Text='<%# Eval("EmpName")%>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
    <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
  </FooterTemplate>
</asp:TemplateField>

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);
    con.Open();
    SqlCommand cmd = new SqlCommand();
 
    cmd.CommandText = "Insert into Emp(EmpId,EmpName,Salary)" +
               "Values(@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;
 
    cmd.ExecuteNonQuery();
    con.Close();
    BindData();
 
  }


Regards
dipa ahuja replied to suresh kotte on 05-Jan-12 02:53 AM
Step 1 : Add new template Field With the Required Controls:
 
<asp:TemplateField>
<FooterTemplate>
  Name:<asp:TextBox ID="txtname" runat="server" /><br />
  Gender :
  <asp:DropDownList ID="ddGender" runat="server">
  <asp:ListItem>Male</asp:ListItem>
  <asp:ListItem>Female</asp:ListItem>
  </asp:DropDownList>
      
  <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
</FooterTemplate>
</asp:TemplateField>
   
► 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);
  
  conn.Open();
  
  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);
  comm.ExecuteNonQuery();
  conn.Close();
 
  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-
http://www.dotnetfunda.com/articles/article180.aspx
kalpana aparnathi replied to suresh kotte on 05-Jan-12 04:56 AM
hi,
Try this code:


<div>
    <asp:GridView ID="gv" DataKeyNames="id" runat="server" SkinID="gvSkinSmall"
      AutoGenerateColumns="false" AllowPaging="false" AllowSorting="false">
      <Columns>
        <asp:BoundField DataField="id" HeaderText="InOutSet_Definition_Id" Visible="true" />
        <asp:TemplateField HeaderText="Name">
          <ItemTemplate>
            <asp:Label ID="txtName" runat="server" MaxLength="40" Text='<%# eval("Name") %>'></asp:Label>
             
          </ItemTemplate>
        </asp:TemplateField>
         
        <asp:TemplateField HeaderText="IsDelete">
          <ItemTemplate>         
              <asp:TextBox ID="txt1"  MaxLength="40"
              runat="server"></asp:TextBox>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
      <SelectedRowStyle BackColor="Silver" />
    </asp:GridView>
  </div>
     <div>
  <asp:LinkButton CssClass="button" ID="btnSave" runat="server">
          <span runat="server" id="spBtnSave" style="width: 60px; text-align: center">Save
          </span>
        </asp:LinkButton>
         <asp:LinkButton ID="btnCancel" runat="server" CssClass="button">
          <span runat="server" id="spBtnCancel" style="width: 60px; text-align: center">Cancel
          </span>
        </asp:LinkButton>
         
  </div>
 
 
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")
  {
   dt.Rows[i].Delete();
  }
}
 
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
    }
  }