原文链接:http://www.cnblogs.com/Echo529/p/4521701.html
GridView中点击某行的任意位置就选中该行
分类:
第一步:添加选择列
点击GridView右边小尖头,双击CommandField,选中"选择",添加,将起设置为不可见;
第二步:处理"ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证",在源文件中添加"enableEventValidation="false"";
第三步:在GridView的RowDataBound事件中增加以下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
PostBackOptions myPostBackOptions = new PostBackOptions(this);
myPostBackOptions.AutoPostBack = false;
myPostBackOptions.RequiresJavaScriptProtocol = true;
myPostBackOptions.PerformValidation = false;
String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex.ToString());
e.Row.Attributes.Add("onclick", evt);
}
第四步:在GridView的SelectedIndexChanged事件中增加你选中了某行后想做的事:
protected void dataGridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//Functions you want to carry out;
}
OK!
补充记录:
清空选中项
GridView1.SelectedIndex = -1;