views:

508

answers:

1

Hello,

am having a grid view and i have a template checkbox... Now i have a edit, delete button.

Once i click on edit button in gridview.... This template checkbox has to be automatically have checked == true... that is automatically checked has to be selected on click of edit buton in gride view..can anyone tell that code...Thank you

A: 

Here is a sample code for a grid view that populates set of Person objects.

This is the markup code of the GridView

<asp:GridView runat="server" ID="grdView" AutoGenerateColumns="False" 
        onrowcancelingedit="grdView_RowCancelingEdit" 
        onrowdatabound="grdView_RowDataBound" onrowediting="grdView_RowEditing">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:TemplateField HeaderText="IsActive Template">
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblIsActive"></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="chkIsActive" runat="server" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>

and this is the code behind that handles these events

    public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }

    private void BindGrid()
    {
        List<Person> persons = new List<Person>
        {
            new Person{ID = 1, IsActive = true, Name = "Test 1"},
            new Person{ID = 2, IsActive = true, Name = "Test 2"},
            new Person{ID = 3, IsActive = true, Name = "Test 3"}
        };

        grdView.DataSource = persons;
        grdView.DataBind();
    }

    protected void grdView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdView.EditIndex = e.NewEditIndex;

        grdView.DataBind();
    }

    protected void grdView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdView.EditIndex = -1;
        grdView.DataBind();
    }

    protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Person p = e.Row.DataItem as Person;
        if (p == null)
            return;
        var lbl = e.Row.Cells[2].FindControl("lblIsActive") as Label;
        if (lbl != null)
        {
            lbl.Text = p.IsActive ? "Yes" : "No";
        }
        else
        {
            var chkIsActive = e.Row.Cells[2].FindControl("chkIsActive") as CheckBox;
            if (chkIsActive != null)
            {
                if (p != null)
                    chkIsActive.Checked = p.IsActive;
            }
        }
    }


}

class Person
{
    public int ID { get; set; }

    public string Name { get; set; }

    public bool IsActive { get; set; }
}

So As you see the RowDataBound event is the one responsible for writing the right value in the template field.

bashmohandes