Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Edit the Gridview control in asp.net

Posted on 2011-03-21
2
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
Tuyau2poil earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
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 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…

715 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