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

x
?
Solved

Insert Row in Datagrid

Posted on 2009-03-30
14
Medium Priority
?
864 Views
Last Modified: 2012-05-06
i have the below code.

Now i want to add a row when Add button is clicked.

How can i achieve that
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ' Generate rows and cells.           
            Dim dt As New DataTable()
            dt.Rows.Add(dt.NewRow())
            dgTest.DataSource = dt
            dgTest.DataBind()
        End If
    End Sub
 
    Protected Sub dgTest_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemCreated
        If e.Item.ItemType = ListItemType.EditItem Then
            Dim e1 As TextBox = DirectCast(e.Item.FindControl("TextBox3"), TextBox)
            e1.Width = Unit.Parse("75px")
            e1 = DirectCast(e.Item.FindControl("TextBox2"), TextBox)
            e1.Width = Unit.Parse("75px")
            e1 = DirectCast(e.Item.FindControl("TextBox1"), TextBox)
            e1.Width = Unit.Parse("75px")
        End If
    End Sub
 Sub doEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        dgTest.ShowFooter = False
        dgTest.EditItemIndex = e.Item.ItemIndex
        BindData()
    End Sub
 
    Sub doCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        dgTest.ShowFooter = True
        dgTest.EditItemIndex = -1
        BindData()
    End Sub
 
    Sub doInsert(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'add a new row to datagrid
 
    End Sub
 
aspx
 
<asp:DataGrid ID="dgTest" runat="server" AutoGenerateColumns="false" BorderColor="White" BorderStyle="Solid" BorderWidth="2px" 
    CellPadding="3" CellSpacing="1" Font-Names="Tahoma" Font-Size="Small" 
    HorizontalAlign="Center"  ShowFooter="True"
    OnEditCommand ="doEdit" OnCancelCommand="doCancel" >
    <FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
        Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
    <Columns>
        <asp:TemplateColumn>
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Shop" 
                    DataTextField="ShopName" DataValueField="ShopCode">
                </asp:DropDownList>
                <asp:SqlDataSource ID="Shop" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:eTFSConnectionString %>" 
                    SelectCommand="SELECT * FROM [DEFShops]"></asp:SqlDataSource>
            </EditItemTemplate>
            <HeaderTemplate>
                Shop Name 
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </EditItemTemplate>
            <HeaderTemplate>
                Docket Number
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </EditItemTemplate>
            <HeaderTemplate>
                Docket Total
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </EditItemTemplate>
            <HeaderTemplate>
                Docket Date
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
         <asp:EditCommandColumn
             ButtonType="PushButton" UpdateText="Update" CancelText="Cancel"
             EditText="Edit" HeaderText="Edit">
      </asp:EditCommandColumn>
        <asp:TemplateColumn HeaderText="Delete">
        <FooterTemplate>
          <asp:Button CommandName="Insert" Text="Add" ID="btnAdd" Runat="server" OnClick="AddRow" />
        </FooterTemplate>
        <ItemTemplate>
          <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" />
        </ItemTemplate>
      </asp:TemplateColumn>
 
    </Columns>
    <HeaderStyle BackColor="#3366CC" Font-Bold="False" Font-Italic="False" 
                                                    Font-Names="Tahoma" Font-Overline="False" Font-Size="Small" 
                                                    Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" 
                                                    ForeColor="White" />
</asp:DataGrid>

Open in new window

0
Comment
Question by:AsishRaj
  • 7
  • 5
  • 2
14 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 24030204
Ashish,

Before we move on with this application, here are some of the things you need to know/answer:

1. When you say 'add a row to the datagrid', do you mean, just add it to the datagrid or do you want that data to be added to a database and then attach the data in the database to the grid?

2. If it is the latter, you need to come up with a database design (with table structures and views and stored procedures as needed). You also need to know what database you would want to use (SQL Server, MS Access, Oracle..)

3. If it is the former, you should know that this data only gets rendered on the browser and does not get rendered anywhere else.

Let me know which way you are going.
0
 
LVL 9

Author Comment

by:AsishRaj
ID: 24032837
its one...

I only want to add a new row to the datagrid. This to me sounds a bit challenging but not impossible.

>>3. If it is the former, you should know that this data only gets rendered on the browser and does not get rendered anywhere else.

Ok I couldnt get what you meant here
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24033776
Ashish,

All this means is that the data the user enters on the screen does not get saved anywhere. So the next time the user logs on to this site, it shows up only with the grid's headers and nothing else. This is because the application did not save the data ('persisted the data' to speak technical) so it could be pulled and displayed to the user at a later date.

I hope I've explained it so you can get a better picture of what path you can take.

Arun
0
Industry Leaders: 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!

 
LVL 9

Author Comment

by:AsishRaj
ID: 24033951
OK, i understood.

This is what i am trying to achieve.

I have some selection criteria when a user wants to register.

I have an empty datagrid with the headers.
when the user clicks add - names will be changed to meaning full names- a new row will be created in the datagrid, By default that will be in edit mode where they will select from the Dropdownlist, textbox etc, defined in the datagrid. when the click add again then new row will be added and again they will get to enter the details.

When they click Finish - all the raw data will get processed and then they will be redirected to the neccessary page.

Thing to note:
Raw data are meaningless therefore there is no need to keep them in the table - but once they get processed - they are updated in the tables. this is sort of the business rule created


This is the UI requirement - the user needs to see the previous details entered unitl he clicks finish, also give the ability to edit and delete those row data

This is the whole reason i am trying to insert a new row in the datagrid
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24033989
Try this line of code behind the add new button
dt.Rows.Add(dt.NewRow())
dgtest.databind()
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24034034
CodeCruiser bhai.. he's done that already.

Asish, please go through the tutorials for a gridview. I'm sure you'll get a better understanding of how to do things:
http://www.codersource.net/asp_net_grid_view_whidbey.aspx
http://www.devx.com/dotnet/Article/22141
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24034078
Yaar lambi lambi explanations likhi thain maine soocha seedha saadha jawab doon.
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24034100
LOL. Good one CodeCruiser.
0
 
LVL 9

Author Comment

by:AsishRaj
ID: 24034474
Yaar aapka seedha saadha jawab toh kaam nahi karega.  toh aap kyon nahi seedhe seedhe apne try karke tab batao.

I have already have a work around which i am using but since i got involved in this i think its good to solve it so that it can be usefull for some in future who is stuck with the same problem.


0
 
LVL 27

Expert Comment

by:nmarun
ID: 24034496
Ashish.. dude chill. Were you able to get your hands around the tutorial?
0
 
LVL 9

Author Comment

by:AsishRaj
ID: 24034561
Sorry CodeCruiser, ulta baat ke ulta response diya.

Yeap i have had a peek at those tutorial.

I think the bottom line is that the datagrid is empty, when we try to add a new row it adds it at row one.

So a workaround could be when add button is clicked, we retrieve the data store it in the dataset or datatable  and then we add a new row.

I havent tried that, but do think it will work.

So far i was trying to add to datagrid without retriveing the details. Silly of me
0
 
LVL 27

Accepted Solution

by:
nmarun earned 2000 total points
ID: 24034591
Ashish,

Here's an article that does what you're willing to do.
http://geekswithblogs.net/casualjim/articles/51360.aspx

The gridview defines an item template, an edit template and the footer has controls that gets used to add new records.
0
 
LVL 9

Author Comment

by:AsishRaj
ID: 24034871
Thanks for the help Arun.

this article explains it in dept. got the link from your above post.
http://geekswithblogs.net/casualjim/archive/2006/05/04/77151.aspx
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24036088
good that things worked out for you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

971 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