Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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,093 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

730 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