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 2:09am
Edited 16-May-12 3: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">
 
<pre>
        &lt;ItemTemplate&gt;
          &lt;asp:Label runat=&quot;server&quot; ID=&quot;category&quot; Text='&lt;%#Eval(&quot;field1&quot;) %&gt;' /&gt;
        &lt;/ItemTemplate&gt;
 
        &lt;EditItemTemplate&gt;
        &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;txt1&quot; Text='&lt;%#Eval(&quot;filed1&quot;) %&gt;' ValidationGroup=&quot;v1&quot; /&gt;
        &lt;asp:RequiredFieldValidator runat=&quot;server&quot; ID=&quot;req1&quot; ControlToValidate=&quot;txt1&quot; ErrorMessage=&quot;*&quot; ValidationGroup=&quot;v1&quot; /&gt;
        &lt;/EditItemTemplate&gt;
 
     &lt;/asp:TemplateField&gt;
 
      &lt;asp:TemplateField HeaderText=&quot;Action&quot;&gt;
 
  &lt;ItemTemplate&gt;
  &lt;asp:LinkButton ID=&quot;btnEdit&quot; Text=&quot;Edit&quot; runat=&quot;server&quot; CommandName=&quot;Edit&quot; /&gt;&amp;nbsp;&amp;nbsp;
  &lt;asp:LinkButton ID=&quot;btnDelete&quot;  Text=&quot;Delete&quot; runat=&quot;server&quot; CommandName=&quot;Delete&quot; /&gt;
  &lt;/ItemTemplate&gt;
 
  &lt;EditItemTemplate&gt;
  &lt;asp:LinkButton ID=&quot;btnUpdate&quot; Text=&quot;Update&quot; runat=&quot;server&quot; CommandName=&quot;Update&quot; ValidationGroup=&quot;v1&quot; /&gt;
  &lt;asp:LinkButton ID=&quot;btnCancel&quot; Text=&quot;Cancel&quot; runat=&quot;server&quot; CommandName=&quot;Cancel&quot; /&gt;
  &lt;/EditItemTemplate&gt;
 
          &lt;ControlStyle ForeColor=&quot;#6600FF&quot; /&gt;
 
          &lt;ItemStyle Font-Size=&quot;Medium&quot; /&gt;
 
  &lt;/asp:TemplateField&gt;
 
   &lt;/Columns&gt;</pre>

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];
 
<pre>
    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();
        }
 
<pre>
}
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();
}</pre>
  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 OriginalGriff 475
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 290
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web01 | 2.8.150331.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