Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
Hi
In my application i have displayed data in the form of grid view. How can i edit a row and update it in the grid view??
Posted
Updated 16-May-12 2:26am
v2
Comments
Mohamed Mitwalli 16-May-12 7:37am    
This is for Comment if you want to add solution Go for Add a Solution button
MAKReddy 16-May-12 8:05am    
ok thanks
Sandeep Mewara 16-May-12 8:15am    
All the answers below are of ASP.NET. You have not tagged it as Web, you looking for Winforms?

Youu can use Rowedititng property..
XML
<Columns>
       <asp:TemplateField HeaderText="Filed">

            <ItemTemplate>
              <asp:Label runat="server" ID="category" Text='<%#Eval("field1") %>' />
            </ItemTemplate>

            <EditItemTemplate>
            <asp:TextBox runat="server" ID="txt1" Text='<%#Eval("filed1") %>' ValidationGroup="v1" />
            <asp:RequiredFieldValidator runat="server" ID="req1" ControlToValidate="txt1" ErrorMessage="*" ValidationGroup="v1" />
            </EditItemTemplate>


         </asp:TemplateField>

          <asp:TemplateField HeaderText="Action">

      <ItemTemplate>
      <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />&nbsp;&nbsp;
      <asp:LinkButton ID="btnDelete"  Text="Delete" runat="server" CommandName="Delete" />
      </ItemTemplate>

      <EditItemTemplate>
      <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" ValidationGroup="v1" />
      <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
      </EditItemTemplate>

              <ControlStyle ForeColor="#6600FF" />

              <ItemStyle Font-Size="Medium" />

      </asp:TemplateField>


       </Columns>



than in row_editing property.....
C#
GridView1.EditIndex = e.NewEditIndex;
       BindGridData();


in BindGridData youu can bind your grid with selected field.

now,row_Updating Property

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        
        TextBox txt1 = (TextBox)row.FindControl("txt1");
        GridView1.EditIndex = -1;
        string s="update query";
        Sqlcommand cmd=new Sqlcommand(s,connection object);
 cmd.executenonquery();

BindGridData();
}


Hope You Got it and Solve Your Problem.
Thanks..
 
Share this answer
 
v3
Comments
expert-programmer 6-Nov-13 1:19am    
Very useful as my opinion.
hi

I hope this link will be help full for you.

[edit and update grid example]
 
Share this answer
 
In gridview there is a property for update or delete you just have to set it true and add a link for update by adding a seperate column.I Hope it will help you out
 
Share this answer
 
---source code---
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        AutoGenerateEditButton="True"
        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
        <Columns>
            <asp:BoundField DataField="TEAMID" HeaderText="ID" SortExpression="TEAMID" />
            <asp:BoundField DataField="TEAMFIRSTNAME" HeaderText="NAME"
                SortExpression="TEAMFIRSTNAME" />
        </Columns>
    </asp:GridView>



------cs coding-------
C#
SqlConnection conn;
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!(IsPostBack))
       {
           gridbind();
       }

   }
   public void gridbind()
   {
       conn = new SqlConnection("Data Source=KOMURAIAH\\SRIRAMA;Initial Catalog=IPL;user id=Ganesh_kuamrpally_hnk;password=Swathi_sbit_2009_civil_warangal");
       conn.Open();
       SqlDataAdapter da = new SqlDataAdapter("select * from team", conn);
       DataSet dset = new DataSet();
       da.Fill(dset, "team");
       GridView1.DataSource = dset.Tables[0];
       GridView1.DataBind();
   }

   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
   {
       GridView1.EditIndex = e.NewEditIndex;
       gridbind();
   }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {

       string teamname =string.empty;

        TextBox txtemp =(TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
           int teamid= Convert.ToInt32(txtemp.Text);

         TextBox txtemp =(TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
           int teamid= Convert.ToInt32(txtemp.Text);
      //---or---
      //foreach (Control ctrl in GridView1.Rows[e.RowIndex].Cells[2].Controls)
      //{
      //TextBox txt = ctrl as TextBox;
      //teamname = txt.Text;
      //}

       conn = new SqlConnection("Data Source=KOMURAIAH\\SRIRAMA;Initial Catalog=IPL;user id=Ganesh_kuamrpally_hnk;password=Swathi_sbit_2009_civil_warangal");
       conn.Open();
       SqlCommand cmd = new SqlCommand("update team set teamid=" + teamid+ ",teamfirstname=' " + teamname + "' where teamid=" + teamid+ "", conn);
       cmd.ExecuteNonQuery();
       gridbind();
   }
 
Share this answer
 
v6
This will also help.

Gridview-row-in-edit-mode[^]
 
Share this answer
 

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