Solved

Edit the Gridview control in asp.net

Posted on 2011-03-21
2
316 Views
Last Modified: 2012-05-11
Iam new to asp.net.I placed the Gridview control and connected to database.
Now The data is displaying well from the table.
I want to edit the rows when I click on button under the grid view control  and delete the rwo when I click on Delete button.

These buttons are not part of the Gridview control
I added two new buttons.
0
Comment
Question by:petersti
2 Comments
 
LVL 4

Accepted Solution

by:
Tuyau2poil earned 500 total points
ID: 35185326
OK but how do you know which row is to edit or to delete ???

you MUST Put Buttons in each rows :

In the datasource used to connect gridview to DB, you can add insert, update and delete sub-query.
the gridview wizard allow you to directly attach edit/insert/delete Command buttons to this sub-querys.

for example, your DataSource can be :

 <asp:SqlDataSource ID="productDataSource" Runat="server"
            SelectCommand="SELECT [ProductName], [ProductID],
            [UnitPrice], [UnitsInStock] FROM [Products]"
            UpdateCommand="UPDATE [Products] SET [ProductName] =
              @ProductName, [UnitPrice] = @UnitPrice, [UnitsInStock] =
              @UnitsInStock WHERE [ProductID] = @original_ProductID"
            ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
            <UpdateParameters>
                <asp:Parameter Type="String"
                  Name="ProductName"></asp:Parameter>
                <asp:Parameter Type="Decimal"
                  Name="UnitPrice"></asp:Parameter>
                <asp:Parameter Type="Int16"
                  Name="UnitsInStock"></asp:Parameter>
                <asp:Parameter Type="Int32" Name="ProductID"></asp:Parameter>
            </UpdateParameters>
        </asp:SqlDataSource>

and your gridview look like this :

<asp:GridView ID="GridView1" Runat="server"
            DataSourceID="productDataSource" DataKeyNames="ProductID"            AutoGenerateColumns="False" AllowPaging="True"
            BorderWidth="1px" BackColor="White"
            CellPadding="4" BorderStyle="None" BorderColor="#3366CC">
            <FooterStyle ForeColor="#003399"
               BackColor="#99CCCC"></FooterStyle>
            <PagerStyle ForeColor="#003399" HorizontalAlign="Left"
               BackColor="#99CCCC"></PagerStyle>
            <HeaderStyle ForeColor="#CCCCFF" Font-Bold="True"
               BackColor="#003399"></HeaderStyle>
            <Columns>
                <asp:CommandField ShowEditButton="True"></asp:CommandField>                <asp:BoundField ReadOnly="True" HeaderText="ProductID"
                    InsertVisible="False" DataField="ProductID"
                    SortExpression="ProductID"></asp:BoundField>
                <asp:BoundField HeaderText="Product"
                    DataField="ProductName"
                    SortExpression="ProductName"></asp:BoundField>
                <asp:BoundField HeaderText="Unit Price"
                    DataField="UnitPrice" SortExpression="UnitPrice">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField HeaderText="Units In Stock"
                   DataField="UnitsInStock" SortExpression="UnitsInStock">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>
            </Columns>
            <SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True"
                BackColor="#009999"></SelectedRowStyle>
            <RowStyle ForeColor="#003399" BackColor="White"></RowStyle>
        </asp:GridView>

note that datasource update query use '@fields' ( ex:  [UnitPrice] = @UnitPrice) wich refer directly to datasource UpdateParameters. Gridview will use this query and column value to create per line update query (or delete ou insert query).
in all case, a line ID is required to know wich line to modify in db table : it's the "datakey" property of gridview.
you can set all this property by VS graphical UI.

have a look to this page : http://msdn.microsoft.com/en-us/library/ms972948.aspx

another approach is to use template fields and gridview datarows events.
looks at this article : http://aspalliance.com/1125_Dynamically_Templated_GridView_with_Edit_Delete_and_Insert_Options.3

hope will help : subject is very large and you will find tons of doc on web.

0
 

Author Closing Comment

by:petersti
ID: 35475214
good
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
This collection of functions covers all the normal rounding methods of just about any numeric value.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

831 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