Solved

Edit the Gridview control in asp.net

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
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 use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now