2010-08-24 64 views
0

发现控制我的GridView其对数据绑定我需要HID Image控件,所以我做了这个代码,但错误apear(rowIndex位置的网格视图dosenot containe认定中),当我试图找到控制错误在GridView控件

C#代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     Img = GridView1.Rows[e.RowIndex].FindControl("Logo") as Image ; 

     using (SqlConnection con = Connection.GetConnection()) 
     { 
      string Sql = "Select Logo From Model where [email protected]"; 
      SqlCommand com = new SqlCommand(Sql, con); 
      com.Parameters.Add(Parameter.NewInt("@Id", DDLModel.SelectedValue)); 
      com.CommandType = CommandType.Text; 
      SqlDataReader dr = com.ExecuteReader(); 
      if (dr.Read()) 
      { 
       string Img2 = dr["Logo"].ToString(); 
       if (Img2 == System.DBNull.Value.ToString()) 
       { 
        Img.Visible = false; 
       } 
      } 
     } 
    } 

回答

0

GridViewRowEventArgs包含行,所以你可以尝试:

image = e.Row.FindControl("Logo") as Image;