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

x
?
Solved

Edit GridView (ASP.NET 20 / C#)

Posted on 2006-11-29
4
Medium Priority
?
71,981 Views
Last Modified: 2011-08-18
I have a gridview with one column called "Advertiser". When the "Edit" button is clicked, the litteral-control shall be replaced with an textbox control, and the "Edit button shall be replaced with "Save" and "Cancel" buttons.

The datagrid is filled correctly with data, but I dont know how to write the "Update" or "Cancel" command.
Here is my code so far:


--------------
ASPX
--------------

<asp:GridView runat="server" id="grdAdvertisers" AutoGenerateColumns="false" ShowHeader="false" OnRowEditing="grdAdvertisers_RowEditing">
    <EditRowStyle BackColor="yellow" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton CommandName="Edit" Text="Edit" Width="45px" runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton CommandName="Update" Text="Update" Width="45px" runat="server" />
                <asp:LinkButton CommandName="Cancel" Text="Cancel" Width="45px" runat="server" />
            </EditItemTemplate>                  
        </asp:TemplateField>
        <asp:TemplateField>
             <ItemTemplate>
               <%# DataBinder.Eval(Container.DataItem,"AdvertiserName") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtAdvertiser" Text='<%# DataBinder.Eval(Container.DataItem,"AdvertiserName") %>' />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


------------------
ASPX.CS
------------------

    private void bindAdvertisers()
    {
        SqlConnection Conn = new SqlConnection(Variables.ConnString);
        SqlCommand cmd = new SqlCommand("usp_AdvertiserNames", Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        this.grdAdvertisers.DataSource = dr;
        this.grdAdvertisers.DataBind();
        cmd.Connection.Close();
        cmd.Dispose();
        Conn.Dispose();
    }


    protected void grdAdvertisers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Response.Write(e.NewEditIndex);
    }
0
Comment
Question by:webressurs
[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
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18037294
In order to get the buttons that you need, you should add CommandField columns through the designer:

                    <asp:CommandField ShowDeleteButton="True" />

Bob
0
 
LVL 1

Author Comment

by:webressurs
ID: 18038820
Ok, but how do I change the button-name from "edit" to ex "edit this"...? How do I write the codebehind to actually execute a Stored Procedure with a parameter (ID)?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 18039304
If you need to stick to that kind of layout, then I would suggest something like this example:

      <asp:GridView ID="GridView2" runat="server" Style="z-index: 103; left: 144px; position: absolute;
        top: 212px" AutoGenerateColumns="False" OnRowCancelingEdit="GridView2_RowCancelingEdit"
        OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating">
        <Columns>
          <asp:BoundField DataField="name" HeaderText="Name" />
          <asp:BoundField DataField="number" HeaderText="Number" />
          <asp:TemplateField>
            <ItemTemplate>
              <asp:LinkButton ID="linkEdit" CommandName="Edit" Text="Edit" Width="45px" runat="server"
                OnClick="EditClick" />
            </ItemTemplate>
            <EditItemTemplate>
              <asp:LinkButton ID="linkUpdate" CommandName="Update" Text="Update" Width="45px" OnClick="UpdateClick"
                runat="server" />
              <asp:LinkButton ID="linkCancel" CommandName="Cancel" Text="Cancel" Width="45px" OnClick="CancelClick"
                runat="server" />
            </EditItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>

    protected void EditClick(object sender, EventArgs e)
    {
        LinkButton button = (LinkButton)sender;
        GridView grid = (GridView)this.FindParent(button, typeof(GridView));

        int index = GetItemIndex(button.UniqueID);
        grid.EditIndex = index;
        grid.DataBind();
    }

    protected void UpdateClick(object sender, EventArgs e)
    {
        LinkButton button = (LinkButton)sender;
    }

    protected void CancelClick(object sender, EventArgs e)
    {
        LinkButton button = (LinkButton)sender;
    }

    private int GetItemIndex(string input)
    {
        string pattern = @"ctl(?<index>\d+)";
        Match match = Regex.Match(input, pattern);

        if (match.Success)
            return int.Parse(match.Groups["index"].Value) - 2;
        return -1;
    }

    private Control FindParent(Control ctrl, Type type)
    {
        while (ctrl.GetType() != type)
            ctrl = ctrl.Parent;

        return ctrl;
    }

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView grid = (GridView)sender;
        grid.EditIndex = -1;
        grid.DataBind();
    }
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // Execute update SQL.
    }

Bob
0
 
LVL 1

Author Comment

by:webressurs
ID: 18044490
Thank you :)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

722 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