Solved

Edit the Gridview control in asp.net

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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 …
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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

617 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