error in finding control in gridview
I had gridview which on databound I need to hid Image co开发者_StackOverflow中文版ntrol so I did this code but error apear( grid view dosenot containe defination of RowIndex) when i tried to find control
C# Code:
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;
}
}
}
}
GridViewRowEventArgs contains the row so you could try:
image = e.Row.FindControl("Logo") as Image;
精彩评论