2010-12-10 28 views
2

我有一个10行的gridview我在每一页显示6行我有一个文本框和图像按钮在每一行当我单击图像按钮时,所有的功能都是工作,但当我点击页面索引它显示行命令中的错误我怎么能检查行类型是否是数据行或没有在gridview行命令事件。我正在使用的代码如下检查当前行是否是一个数据行

protected void gvgridview1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer); 
     TextBox txtgvGroupName = (TextBox)gvRow.FindControl("txtgvGroupName"); 
     ImageButton imgbtn = (ImageButton)gvRow.FindControl("imgbtn"); 
     if (e.CommandName == "Edit") 
      { 
       imgbtn.Visible = false; 
      } 
    } 
+0

你应该张贴你得到了什么错误。我猜你因为(ImageButton)e.CommandSource你的索引链接不是ImageButton而出现错误。 – Mihailo 2010-12-10 11:17:36

回答

0

您是否尝试过检查GridViewRow实例的RowType属性?

+0

在行命令我无法找到rowtype属性 – 2010-12-10 10:10:38

+0

嗯,这很奇怪。根据这个:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowtype.aspx GridViewRow具有该属性。 – willvv 2010-12-10 10:17:16

+0

我告诉你,在Gridview_RowCommand中没有像e.rowtype – 2010-12-10 11:35:38

0

检查e.Row.RowType,它允许您将RowType与DataControlRowType enum进行比较。

[编辑]我不知道这与你如何掌握这一行有关。我只是尝试的代码是:

GridViewRow row = Gridview.Rows[int.Parse(e.CommandArgument.ToString())]; 

之后,我可以使用row.RowType相当happliy。可能值得一试。

DataControlRowType

+0

在行命令我无法找到rowtype属性 – 2010-12-10 10:12:31

+0

@Mathew刚更新我的答案,你可能想尝试的东西 – PhilPursglove 2010-12-10 10:31:42

+0

如果你使用排序失败,因为Sort的commandArgument是SortExpression。 – dividius 2012-08-22 07:10:00

0

试试这个假设你reffering的System.Data.DataRow

GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer); 
DataRow drow = gvRow.DataItem as DataRow 

if(drow!=null) 
{ 
    // row is DataRow 
} 
+0

我的代码工作正常当我点击GridView页面索引来查看下一页其他时候我的页面正在工作正常 – 2010-12-10 10:20:25

3

如果(e.Row.RowType == DataControlRowType.DataRow)

然后写你的条件。

+0

GridViewCommandEventArgs没有行属性 – dividius 2012-08-22 07:10:26

1

一点跟这里的数据行,而不是你需要的第一线检查if (e.CommandSource is ImageButton)gvgridview1_RowCommand

+0

是的,这很好,谢谢你的帮助follwing代码工作正常字符串strTemp = string.Empty; strTemp = e.CommandSource.ToString(); if(strTemp ==“System.Web.UI.WebControls.ImageButton”) { – 2010-12-10 11:36:30

+1

我以为你可以使用'if(e.CommandSource是ImageButton)'? – Bolu 2010-12-10 11:43:44

+0

很高兴听到你的消息是,这是工作,谢谢我第一次使用..再次感谢 – 2010-12-13 06:00:49

相关问题