I have a web app and use gridview to show some SQL data. In the GV I have a checkbox in the first column. As suggested in the title, I want to check the checkbox when user clicks anywhere in that line. How can I accomplish that. Thanks.
This is my GV;
<asp:GridView ID="myGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
OnRowDataBound="myGV_OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="myCB" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
I use this code to highlight the row when mouse pointer is over it;
protected void myGV_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#D9ECFB'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
}
}
EDIT:
I added “OnSelectedIndexChanging” and “OnSelectedIndexChanged” to the GridView and then tried this (as suggested by Ravi), but couldn’t get it to work.
protected void myGV_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = myGV.Rows[e.NewSelectedIndex];
CheckBox chk = (CheckBox)myGV.FindControl("cbIzpis");
if (chk.Checked == true)
{
chk.Checked = false;
}
else if (chk.Checked == false)
{
chk.Checked = true;
}
}
protected void myGV_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow row = nyGV.SelectedRow;
}
you can use GridView.SelectedRow property, with SelectedIndexChanged Event, Gridview Selected Row