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,965 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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: …

729 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