Click here to Skip to main content
11,720,785 members (84,311 online)
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 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 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 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 Sergey Alexandrovich Kryukov 2,179
1 OriginalGriff 924
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100