You must Sign In to post a response.
  • Category: ASP.NET

    Adding a new row with a button in footer in Gridview

    I am trying to add new rows to Grid view using a button in footer, So that I have to see newly added data in grid view and must be saved in database. When I am executing the following code , no errors are coming but nothing is happening. After clicking the button to add with data in text boxes , it is loading and giving me a page with only master page contents, I can't see any grid view over there. If I find the solution for this I can use it for update button also, which there in each row.
    Following is my code :

    Aspx:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="922px"
    CellPadding="4" Height="231px" ShowFooter="True" EnableViewState="False" BackColor="White"
    BorderColor="#3366CC" BorderStyle="Double" BorderWidth="1px" OnRowDataBound="OnRowDataBound"
    OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowCommand="GridView1_RowCommand">
    <Columns>
    <asp:BoundField HeaderText="Id" DataField="ID" />
    <asp:TemplateField HeaderText="Department">
    <ItemTemplate>
    <asp:TextBox ID="TextBox1" Text='<%# Eval("DeptID") %>' runat="server"></asp:TextBox>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txt_dept" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="vdept" runat="server" ControlToValidate="txt_dept"
    Text="?" ValidationGroup="validaiton" />
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Location">
    <ItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Location") %>'></asp:TextBox>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txt_loc" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="vloc" runat="server" ControlToValidate="txt_loc"
    Text="?" ValidationGroup="validaiton" />
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Activity">
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Inactive") %>' Visible="false" />
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    </ItemTemplate>
    <FooterTemplate>
    <asp:DropDownList ID="ddl_inactive" runat="server">
    <asp:ListItem>--Select--</asp:ListItem>
    <asp:ListItem>Active</asp:ListItem>
    <asp:ListItem>Inactive</asp:ListItem>
    </asp:DropDownList>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Created Date" DataField="CreatedDate" />
    <asp:BoundField HeaderText="Created By" DataField="CreatedById" />
    <asp:BoundField HeaderText="Updated Date" DataField="UpdatedDate" />
    <asp:BoundField HeaderText="Updated By" DataField="UpdatedByID" />
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Button ID="ButtonEdit" runat="server" CommandName="EditRow" Text="Save" CommandArgument='<%# Eval("DeptID") %>' />
    </ItemTemplate>
    <FooterTemplate>
    <asp:Button ID="ButtonAdd" runat="server" CommandName="AddNew" Text="Add New Record">
    </asp:Button>
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>

    Code behind:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName.Equals("AddNew"))
    {
    TextBox txt_dept = (TextBox)GridView1.FooterRow.FindControl("txt_dept");
    TextBox txt_loc = (TextBox)GridView1.FooterRow.FindControl("txt_loc");
    //DropDownList ddl_inactive = (DropDownList)GridView1.FooterRow.FindControl("ddl_inactive");

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;

    con.Open();
    cmd.CommandText = "Insert Into dbo.tblTBT_Depts(DeptID,Location) Values(@DeptID,@Location),con";

    cmd.Parameters.Add("@DeptID", SqlDbType.VarChar, 15);
    cmd.Parameters["@DeptID"].Value = txt_dept.Text;

    //cmd.Parameters.Add("@Inactive", SqlDbType.VarChar, 50);
    //cmd.Parameters["@Inactive"].Value = ddl_inactive.SelectedValue;

    cmd.Parameters.Add("@Location", SqlDbType.VarChar, 50);
    cmd.Parameters["@Location"].Value = txt_loc.Text;

    int result = cmd.ExecuteNonQuery();
    con.Close();
    if (result == 1)
    {
    FillGrid();
    lblmsg.Text = txt_dept.Text + " Added successfully...... ";
    }
    else
    {

    lblmsg.Text = txt_dept.Text + " Error while adding row.....";
    }
    }
  • #766005
    It is bit simple, use 'RowDataBound' event and check if the row type is footer row and then add it
    see below snippet

    protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Footer)
    {
    TextBox txt = new TextBox();
    // set properties of text box
    e.Row.Cells[0].Controls.Add(txt);
    }
    }

    OR
    GridView gv = new GridView();
    gv.RowCreated += delegate(object dsender, GridViewRowEventArgs ge)
    {
    if (ge.Row.RowType == DataControlRowType.Footer)
    ge.Row.Cells[0].Text = "Something";
    };
    gv.AutoGenerateColumns = false;
    gv.ShowFooter = true;
    BoundField bf = new BoundField();
    bf.HeaderText = "col 1";
    bf.DataField = "Length";
    gv.Columns.Add(bf);
    gv.DataSource = new string[] { "One", "Two", "Three" };
    gv.DataBind();
    Form.Controls.Add(gv);

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766011
    Hi,

    Replace your rowcommand event with the following code


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName.Equals("AddNew"))
    {
    TextBox txt_dept = (TextBox)GridView1.FooterRow.FindControl("txt_dept");
    TextBox txt_loc = (TextBox)GridView1.FooterRow.FindControl("txt_loc");
    //DropDownList ddl_inactive = (DropDownList)GridView1.FooterRow.FindControl("ddl_inactive");

    SqlCommand cmd = new SqlCommand("Insert Into dbo.tblTBT_Depts(DeptID,Location) Values(@DeptID,@Location)",con);

    con.Open();

    cmd.Parameters.Add("@DeptID", SqlDbType.VarChar, 15);
    cmd.Parameters["@DeptID"].Value = txt_dept.Text;

    //cmd.Parameters.Add("@Inactive", SqlDbType.VarChar, 50);
    //cmd.Parameters["@Inactive"].Value = ddl_inactive.SelectedValue;

    cmd.Parameters.Add("@Location", SqlDbType.VarChar, 50);
    cmd.Parameters["@Location"].Value = txt_loc.Text;

    int result = cmd.ExecuteNonQuery();
    con.Close();
    if (result == 1)
    {
    FillGrid();
    lblmsg.Text = txt_dept.Text + " Added successfully...... ";
    }
    else
    {

    lblmsg.Text = txt_dept.Text + " Error while adding row.....";
    }
    }


    I guess the problem with SqlCommand, please replace with above code and debug the code and check once again.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766077
    Please check the following

    1. Once you click the butting are you able to debug the "GridView1_RowCommand" event?
    2. If you are getting the debugger in the event there is no issues in the binding.
    3. In the debugger, check the Database insert statement.
    4. Are you able to insert the data in the database. If the data is not inserted check your DB and DB insert code.
    5. If the Data is inserted successfully., there is an issue for getting the data and binding in the page load.
    6. Just check and make sure are you binding the data in the page load.
    7. And also make sure in the page load, are you binding the data in side the IsPostback() check.

    I hope the above points will help you to identity your issue.

    By Nathan
    Direction is important than speed


  • Sign In to post your comments