Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to insert a new row to gridview by adding footer row columns in the code behind.

Posted on 2013-01-04
3
Medium Priority
?
2,153 Views
Last Modified: 2013-01-05
Hi,
I am adding the columns to GridView in code behind and binding it to Data Table.
private void BindGridView(DataTable SQLDt)
        {

            grd1.Columns.Clear();
            grd1.DataSource = SQLDt;

            BoundField bnd = new BoundField();
            bnd.DataField = "Number";
            bnd.SortExpression = "Number";
            bnd.ShowHeader = true;
            bnd.HeaderText = "Number";
            grd1.Columns.Add(bnd);
           
            ButtonField btn = new ButtonField();
            btn.ButtonType = ButtonType.Link;
            btn.DataTextField = "AMOUNT";
            btn.SortExpression = "AMOUNT";
            btn.CommandName = "Select";
            btn.ShowHeader = true;
            btn.HeaderText = "Amount";
            grd1.Columns.Add(btn);

            bnd = new BoundField();
            bnd.DataField = "DESCRIPTION";
            bnd.SortExpression = "DESCRIPTION";
            bnd.ShowHeader = true;
            bnd.HeaderText = "Type";
            grd1.Columns.Add(bnd);
         

            bnd = new BoundField();
            bnd.DataField = "Date";
            bnd.SortExpression = "Date";
            bnd.ShowHeader = true;
            bnd.HeaderText = "Date";
            grd1.Columns.Add(bnd);

            bnd = new BoundField();
            bnd.DataField = "TIME";
            bnd.SortExpression = "TIME";
            bnd.ShowHeader = true;
            bnd.HeaderText = "Time";
            grd1.Columns.Add(bnd);

           
           
            grd1.DataBind();
            grd1.Columns[0].Visible = false;
        }

There is a Add button in the page. When Add button is clicked I want to show the footer row and add the columns to the footer row in the code behind.
Basically I want to insert a new row to gridview by editing footer row columns.  I want to add the footer row  it in the code behind.
 
 protected void AddButton_Click(object sender, EventArgs e)
        {
            try
            {
                grd1.FooterRow.Visible = true;
            }
            catch
            {

            }

        }
   adding columns to footer row in code behind is possible.

Please give me suggestions.
Thanks
0
Comment
Question by:KavyaVS
3 Comments
 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 1000 total points
ID: 38744077
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 1000 total points
ID: 38746097
Example :
http://www.dotnetfunda.com/articles/article180.aspx
How it Works .
Gridview Design Part :
 1.Enable footer on gridview showfooter="true".
 2.Add footer template to each column on your gridview .
 3.Create event for the add new button in the footer template
<asp:GridView ID="GridView1" runat="Server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
            GridLines="Vertical" Width="100%" OnRowCommand="GridView1_OnRowCommand1" ShowFooter="true"
            DataKeyNames="AutoID">
            <Columns>
                <asp:BoundField DataField="AutoID" HeaderText="AutoID" ReadOnly="True" />
                <asp:TemplateField HeaderText="UserName">
                    <ItemTemplate>
                        <%# Eval("UserName") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtUserName" runat="Server" Text='<%# Eval("UserName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtUserName" runat="Server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Password">
                    <ItemTemplate>
                        <%# Eval("Password") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtPassword" runat="Server" Text='<%# Eval("Password") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtPassword" runat="Server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Active">
                    <ItemTemplate>
                        <%# Eval("Active") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="dropActive" runat="server" SelectedValue='<%# Eval("Active").ToString().ToLower().Equals("true") ? "True" : "False" %>'>
                            <asp:ListItem Text="Yes" Value="True"></asp:ListItem>
                            <asp:ListItem Text="No" Value="False"></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="dropActive" runat="server">
                            <asp:ListItem Text="Yes" Value="True"></asp:ListItem>
                            <asp:ListItem Text="No" Value="False"></asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Open in new window

Codebehind :Event of the add new button
1.Find control of footer template.
2.Pass it to your insert procedure.
3.Bind Gridview Again.

  protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("Insert"))
        {

            TextBox u = GridView1.FooterRow.FindControl("txtUserName") as TextBox;

            TextBox p = GridView1.FooterRow.FindControl("txtPassword") as TextBox;

            new AdminBAL().Insert(u.Text, p.Text, sp.Text, dT.SelectedValue, ad.Text, bool.Parse(dA.SelectedValue));

            lblError.Text = "<br />Record inserted successfully<br />";

            BindCurrentUsers(); // rebind the data

        }

    }

Open in new window

Meeran03
0
 

Author Closing Comment

by:KavyaVS
ID: 38746839
Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question