Solved

Insert Row in Datagrid

Posted on 2009-03-30
14
832 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 500 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

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

11 Experts available now in Live!

Get 1:1 Help Now