Solved

Edit the Gridview control in asp.net

Posted on 2011-03-21
2
308 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

863 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

27 Experts available now in Live!

Get 1:1 Help Now