Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a edit button in my page.

on click of the edit button i want to make all the editable rows in my gridview visible(in edit mode)

My gridview is in a web user control and button on an aspx page.
ASP.NET
<asp:Gridview ID="GridView1" runat="server" AutoGenerateColumns="False" >
   <columns>
        <asp:TemplateField HeaderText="Id">            
            <itemtemplate>
                <asp:LinkButton ID="lnkId" runat="server" onclick="lnkId_Click" Text='<%# Eval("Id") %>' >                
            </itemtemplate>        
        <asp:TemplateField HeaderText="Name">
            <edititemtemplate>
                <asp:TextBox ID="txtEditName"  runat="server" Text='<%# Bind("Name") %>'>
            </edititemtemplate>
            <itemtemplate>
                <asp:Label ID="lblName"  runat="server" Text='<%# Bind("Name") %>'>
            </itemtemplate>        
    </columns>
<asp:Button id="Button1" runat="server"  text"edit" onclick="Button1_click"/>



Thanks and Regards

Sourav
Posted
Updated 3-Aug-13 0:22am
v3

You would need to loop for each row of your gridview.

Something like

VB
For i = 0 To gridview.rows.count - 1
gridview.row(i).cell(0).Text = "Text of row: " i.ToString() & " and cell 0."
Next


Hope this helped,
Bert
 
Share this answer
 
It is recommended to do this in javascript. Javascript is far more better performance as the task is carried out at client browser.

However, if you still want to do it at server side, below will give you an idea:
ASP.NET page code:
ASP.NET
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
    <columns>
        <asp:boundfield datafield="name" headertext="Name" />
        <asp:boundfield datafield="tel" headertext="Tel" />
    </columns>

</asp:gridview>

<br />

<asp:button id="Button_Edit" runat="server" onclick="Button_Edit_Click" xmlns:asp="#unknown">
    Text="Edit" /></asp:button>

C# Code behind:
C#
public partial class WebForm1 : System.Web.UI.Page
{
    System.Data.DataTable dt
    {
        get
        {
            return (System.Data.DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }

    bool isEditMode
    {
        get
        {
            if (ViewState["isEditMode"] == null)
                return false;
            return (bool)ViewState["isEditMode"];
        }
        set
        {
            ViewState["isEditMode"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new System.Data.DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("tel");
            dt.Rows.Add("Thomas", "1111");
            dt.Rows.Add("Jessie", "2222");
            dt.Rows.Add("Parker", "3333");
            BindData();
            isEditMode = false;
        }

        if (isEditMode)
            AddTextBox();
    }

    void BindData()
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    void AddTextBox()
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                GridView1.Rows[i].Cells[j].Controls.Add(new TextBox());
            }
        }
    }

    void LoadDataIntoTextbox()
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                ((TextBox)GridView1.Rows[i].Cells[j].Controls[0]).Text = dt.Rows[i][j] + "";
            }
        }
    }

    void SaveData()
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                dt.Rows[i][j] = ((TextBox)GridView1.Rows[i].Cells[j].Controls[0]).Text;
            }
        }

        BindData();
    }

    protected void Button_Edit_Click(object sender, EventArgs e)
    {
        isEditMode = !isEditMode;
        if (isEditMode)
        {
            Button_Edit.Text = "Save";
            AddTextBox();
            LoadDataIntoTextbox();
        }
        else
        {
            Button_Edit.Text = "Edit";
            SaveData();
        }
    }
}
 
Share this answer
 
v3
Comments
doanhthang 7-Aug-17 23:10pm    
Thanks for this post. It very helpful for me
thanks adriancs...

i understood the logic..i have used the isEditMode in my textbox and label to check and perform hide and show setting the visibility

C#
<asp:templatefield headertext="Name" xmlns:asp="#unknown">
          <itemtemplate>
               <asp:textbox id="txtEditName" visible="<%# IsInEditMode %>" runat="server" text="<%# Bind("Name") %>"></asp:textbox>


               <asp:label id="lblName" visible="<%# !(bool) IsInEditMode %>" runat="server" text="<%# Bind("Name") %>"></asp:label>
           </itemtemplate>
           </asp:templatefield>



and getting the updated value from my textbox like this on click of save button

C#
protected void btnEdit_Click(object sender, EventArgs e)
        {
            IsInEditMode = true;
            BindGrid();
           
        }
 protected void btnSave_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                string name = ((TextBox)row.FindControl("txtEditName")).Text;
            }
        }
 
Share this answer
 
v3
Comments
adriancs 4-Aug-13 9:55am    
Not bad. Looks good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900