Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 16-May-12 1:09am
Edited 16-May-12 2:26am
v2
Comments
Mohamed Mitwalli at 16-May-12 7:37am
   
This is for Comment if you want to add solution Go for Add a Solution button
Ajayreddy2611 at 16-May-12 8:05am
   
ok thanks
Sandeep Mewara at 16-May-12 8:15am
   
All the answers below are of ASP.NET. You have not tagged it as Web, you looking for Winforms?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Youu can use Rowedititng property..
<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.....
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..
  Permalink  
v3
Comments
AxeScript at 6-Nov-13 1:19am
   
Very useful as my opinion.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi
 
I hope this link will be help full for you.
 
[edit and update grid example]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

---source code---
<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-------
 
 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();
    }
  Permalink  
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

This will also help.
 
Gridview-row-in-edit-mode[^]
  Permalink  

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

  Print Answers RSS
0 Maciej Los 589
1 OriginalGriff 419
2 CPallini 205
3 RyanDev 175
4 Sergey Alexandrovich Kryukov 170
0 OriginalGriff 5,649
1 Sergey Alexandrovich Kryukov 4,783
2 Peter Leow 2,954
3 Maciej Los 2,689
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 5 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid