Hi Sandeep, The following error is been displayed when im running the program.. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)gridonlinestud.Rows[i].FindControl("imgOnline");
clearly you did not read my comment properly. Now, based on the error you share, it is clear that the dataset row count based on which you are looping is more than the number of rows you have in your grid.
Use two for loops one in other, find the row of dataset that presents data in grid and then use your logic. Currently, your mapping looks wrong and the dataset does not hold same data as grid has.
UPDATED:
my requirement is simple chat application.. when users logs in green image has to be displayed and when user logsout image has to disappear... so in my database, i have column online.. and when user logs in it is updated as true and when he logs out it is updated as false
When you fetch the data from database, you have something like:
Usernames IsOnline
Abc True
Xyz False
..
..
QWE False
Now, you can tie your image source to the boolean field like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imgOnline" ImageUrl='<%# ChooseImage(Eval("IsOnline")) %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
myGrid.DataSource = currentDataFromDataBaseInaDataTable;
myGrid.DataBind();
protected string ChooseImage(string isOnlineValue)
{
bool isOnline = Convert.ToBoolean(isOnlinevalue);
if (isOnline) {
return "~/images/Green.gif";
} else {
return "~/images/Red.gif";
}
}