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,788 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
Comment Utility
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 250 total points
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

16 Experts available now in Live!

Get 1:1 Help Now