如何检查是否从GridView中选择了任何行?

我在aspx页面中有一个gridview:

                 
--- No Consumer Exists ---

rowDataBound方法是:

 protected void gdvMainList_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gdvMainList, "Select$" + e.Row.RowIndex); } } 

我有一个OK按钮,当它被点击时,我从页面收集数据。 我想查看OK按钮单击是否有任何从Gridview中选择的行。

我怎样才能做到这一点? 任何帮助,将不胜感激。

你可以检查…

 if (GridView1.SelectedValue != null) { //Row is Selected } 

你可以尝试这样的事情:

 If GridView1.SelectedRows.Count > 0 Then ' yourcode here - a row is selected Else ' yourcode here - NO row is selected End If 

更好的是:

 if(GridView1.SelectedIndex < 0) { its -1 and no row is selected.} else {its >= 0 and a row is selected} 

如果所选值为!= null则测试!= null将抛出exception。

你也可以这样检查

 if(GridView.SelectedIndex >= 0) { string result = "Selected"; } else { string result = "Not Selected"; }