在gridview中查找控件时出错

时间:2010-08-24 08:36:14

标签: c# datagridview

我有网格视图,在数据绑定我需要隐藏图像控件所以我做了这个代码但是错误apear(网格视图dosenot容纳RowIndex的定义)当我试图找到控件时

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 Id=@Id";
            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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

GridViewRowEventArgs包含该行,您可以尝试:

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