Friday, 9 November 2012

to edit,update in gridview

 <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False"
            GridLines="None" OnRowDataBound="GridView1_RowDataBound" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowEditing="GridView1_RowEditing">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:TemplateField HeaderText="Stateid">
                    <ItemTemplate>
                        <asp:Label ID="lblstateid" runat="server" Text='<%#Bind("Stateid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:TextBox ID="txtState" runat="server" Text='<%#Bind("State") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Location">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlcity" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="edit" />
                        <asp:Button ID="btndelete" runat="server" Text="delete" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Button ID="btnUpdate" runat="server" Text="Update" OnCommand="Update" />
                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField></asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>

public void bindgridview()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select Stateid,State from tbl_State", con);
        SqlDataAdapter sdp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sdp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    }
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindgridview();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        bindgridview();   
    }
    protected void Update(object sender, CommandEventArgs e)
    {
        con.Open();
        Button edit = (Button)sender;
        TextBox txtpackage = (TextBox)edit.FindControl("txtState");
        Label id = (Label)edit.FindControl("lblstateid");
        string query = "Update tbl_State set State=@State where Stateid=@Stateid";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@State", txtpackage.Text);
        cmd.Parameters.AddWithValue("@Stateid", id.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bindgridview();  

    }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)// method2
    {
        con.Open();
        TextBox txtpackage = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtState");
        Label id = (Label)GridView1.Rows[e.RowIndex].FindControl("lblstateid");
        string query = "Update tbl_State set State=@State where Stateid=@Stateid";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@State", txtpackage.Text);
        cmd.Parameters.AddWithValue("@Stateid", id.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bindgridview();
     
     

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) method 3
    {
        con.Open();
        GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        TextBox txtpackage = (TextBox)GridView1.Rows[row.RowIndex].FindControl("txtState");
        Label id = (Label)GridView1.Rows[row.RowIndex].FindControl("lblstateid");
        string query = "Update tbl_State set State=@State where Stateid=@Stateid";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@State", txtpackage.Text);
        cmd.Parameters.AddWithValue("@Stateid", id.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bindgridview();


}

Find Control or Datakeys on RowCommand Event of GridView : 

To Find Control onRowCommand Event in GridView there is no Simple Way But we can use simple technique to find control from GridView.
This Simple Technique is given as follows:
1. Create a object of GridViewRow from refrence of command source row.
For Eg: 
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer; 

But keep in mind that which type of object control is you are using to call event in above code is LinkButton. But if You Calling Rowcommand event from Simple Button then You need to Write this:
GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;

2. After Creating Row Object You can simply find any control from Gridview using rowindex.
For Eg: 
Label LabelEmail = (Label)GridView1.Rows[row.RowIndex].FindControl("LabelEmail");

3. Finally You Can Access that Properties of that Control.
For Eg: 
LabelResult.Text = LabelEmail.Text; 

4. Same as Above You can also find Datakeys of gridview using GridViewRow rowindex.
For Eg: 
int code = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0].ToString());