Pages

Men

rh

11/13/2012

How to Edit the Row in Gridview using C#

Example :

<table width="85%">
        <tr>
            <td>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Pid" AllowPaging="true" AllowSorting="True" BorderColor="Black" BorderStyle="Solid"  GridLines="None" ShowFooter="True" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" CaptionAlign="Top" Width="118%" >
 

<AlternatingRowStyle BackColor="#E5E5E5" Height="29PX" />
                  

  <Columns>
   <asp:BoundField HeaderStyle-Height="29px" HeaderText="PID" DataField="PID"
                            Visible="false">
          <HeaderStyle Height="29px"></HeaderStyle>
                        </asp:BoundField>
                        <asp:BoundField HeaderText="Postcode" DataField="Postcode" />


                        <asp:BoundField HeaderText="Latitude" DataField="Latitude" />


                        <asp:BoundField HeaderText="Longitude" DataField="Longitude" />


                        <asp:BoundField HeaderText="Easting" DataField="Easting" />


                         <asp:TemplateField>


                            <ItemTemplate>
                                <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" CommandArgument='<%# Eval("PID") %>'
                                    Text="Edit" Font-Underline="false" ForeColor="#386991"></asp:LinkButton>
                                |
                               
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#E5E5E5" />
                    <HeaderStyle BackColor="#C1D1DA" Font-Bold="False" Font-Names="verdana" Font-Size="X-Small"
                        ForeColor="#386991" HorizontalAlign="Left" Height="29px" Wrap="True" />
                    <RowStyle Font-Size="Small" Height="29px" ForeColor="#386991" />
                    <SelectedRowStyle BackColor="#E5E5E5" />
                </asp:GridView>
            </td>
        </tr>
    </table>



Please find the .CS file Code below.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }


protected void BindData()
{
         
 DataSet dsImage = new DataSet();


SqlConnection con = new SqlConnection("Data Source=Chandu/SQLExpress;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=sa;Connect Timeout=800");
   

con.Open();
            

SqlCommand cmd = new SqlCommand();
           

 cmd.Connection = con;
           

 cmd.CommandType = CommandType.Text;
            

cmd.CommandText = "Select top 1000 [PID],[Postcode],[Latitude],[Longitude],[Easting] FROM [Test].[dbo].[PostalCodes]";
            

SqlDataAdapter da = new SqlDataAdapter(cmd);
          
            da.Fill(dsImage);
            try
            {

                if (dsImage.Tables.Count > 0)
                {
                    if (dsImage.Tables[0].Rows.Count > 0)
                    {
                        GridView1.DataSource = dsImage.Tables[0];
                        GridView1.DataBind();
                        con.Close();
                    }
                }
            }
            catch (Exception ex)
            {

            }
  }

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{        

             Int32 intPid;
             if (e.CommandName == "Edit")
             {
                 intPid = Convert.ToInt32(e.CommandArgument.ToString());
             }
             else if (e.CommandName == "Delete")
             {

                 intPid = Convert.ToInt32(e.CommandArgument.ToString());
             }

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 {
             GridView1.EditIndex = -1;
             BindData();
 }