CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Edit/Update in GridView in ASP.Net 2.0
Submitted By Satheesh Babu B
On 10/18/2008 1:25:02 AM
Tags: ADO.Net,Asp.Net,CodeDigest  

Read the previous code snippet to connect to SqlServer and binding a GridView here. I have disabled AutoGenerateColumns and declared the columns explicitly.

In the below code snippet, I have used Template column for binding a link or URL to a hyperlink control. All other columns are BoundField columns.

 

ASPX

<asp:GridView ID="gvModerateCodes" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#010101" BorderStyle="Groove" BorderWidth="1px" CellPadding="4" OnRowCancelingEdit="gvModerateCodes_RowCancelingEdit" OnRowEditing="gvModerateCodes_RowEditing" OnRowUpdating="gvModerateCodes_RowUpdating" >

                    <Columns>

                        <asp:BoundField DataField="CodeID" HeaderText="CodeID" ReadOnly="True" />

                        <asp:BoundField DataField="Title" HeaderText="Title" />

                        <asp:BoundField DataField="Description" HeaderText="Description" />                    

                        <asp:TemplateField HeaderText="URL">

                            <EditItemTemplate>

                                <asp:TextBox ID="txtURL" Width="100%" Text='<%# Bind("URL")%>' runat="server"></asp:TextBox>

                            </EditItemTemplate>

                            <ItemTemplate>

                                <asp:HyperLink ID="hplURL" Text='<%# Bind("URL") %>' NavigateUrl='<%# Bind("URL") %>' runat="server">[hplURL]</asp:HyperLink>

                            </ItemTemplate>

                        </asp:TemplateField> 

  <asp:CommandField ShowEditButton="True" />   

                    </Columns>

                </asp:GridView>

 

CodeBehind

protected void Page_Load(object sender, EventArgs e)

    {

 

        if (!IsPostBack)

        {          

            GetCode();

        }

    }

    public void GetCode()

    {

      

        gvModerateCodes.DataSource = daoCD.GetCodesForModeration(); //Get data from DB

        gvModerateCodes.DataBind();

    }

    protected void btnGetArticles_Click(object sender, EventArgs e)

    {

        GetCode();

    }

    protected void gvModerateCodes_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        string articleID = gvModerateCodes.Rows[e.RowIndex].Cells[0].Text;

        string title = (gvModerateCodes.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;

        string url = (gvModerateCodes.Rows[e.RowIndex].Cells[3].Controls[1] as TextBox).Text;

        string description = (gvModerateCodes.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;

        daoCD.UpdateCode(int.Parse(articleID), title, url, description);//update data to DB

        gvModerateCodes.EditIndex = -1;

        GetCode();

    }

    protected void gvModerateCodes_RowEditing(object sender, GridViewEditEventArgs e)

    {

        gvModerateCodes.EditIndex = e.NewEditIndex;

        GetCode();

    }

    protected void gvModerateCodes_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        gvModerateCodes.EditIndex = -1;

        GetCode();

    }

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..