Solved

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

Posted on 2013-01-04
3
1,844 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 250 total points
ID: 38744077
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 250 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now