在asp.net的datagrid的itemtemplate中显示图像的问题 [英] problem in displaying image in itemtemplate of datagrid in asp.net..

查看:102
本文介绍了在asp.net的datagrid的itemtemplate中显示图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个必须根据数据库值显示图像的要求.我在数据库中有一个列,该列存储对或错以及从数据库检索数据并将其存储在数据集中,现在当数据库中的数据为true时我需要在datagrid列中显示图像,而当其为false时不应该显示图像... br/>
我写了一些无法正常工作的代码...我不知道哪里出错了...

Hi
I have a requirement where i have to display a image based on database value. i have a column in database which stores true or false and im retriving data from database and storing in a dataset , now i need to display image in datagrid column when data from database is true and shouldnot display the image when its false...

i have written some code which is not working... i dont where i went wrong...

for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
        {
            Boolean abc = Convert.ToBoolean(ds1.Tables[0].Rows[i][2]);
          if (abc == true)
            {
               System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)gridonlinestud.Rows[i].FindControl("imgOnline");
                img.ImageUrl = "../images/dot.jpg";
            }
            else if(abc == false)
           {
                System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)gridonlinestud.Rows[i].FindControl("imgOnline");
                img.Visible = false;
            }
        }


任何人都可以帮助解决这个问题...

谢谢
Vidya


please can anyone helpme with this issue...

Thanks
Vidya

推荐答案

我认为它的引用Grid标头的索引引起了问题.第0个索引始终引用标题数据.因此,它可能无法在该行中找到图像.尝试从网格的第一行开始循环.
I think its the index which refers to Grid header is causing the issue. 0th index always refers to Header data. Hence it might not be able to find the image in that row. Try looping from 1st row of the grid.


您好,Sandeep,在运行程序时显示以下错误.索引超出范围.必须为非负数并且小于集合的大小.参数名称:System.Web.UI.WebControls.Image处的索引img =(System.Web.UI.WebControls.Image)gridonlinestud.Rows [i] .FindControl("imgOnline");
显然您没有正确阅读我的评论.现在,基于您所共享的错误,很明显,基于循环的数据集行数大于网格中的行数.

互相使用两个for循环,找到在网格中呈现数据的数据集所在的行,然后使用您的逻辑.当前,您的映射看起来不正确,并且数据集不包含与网格相同的数据.

更新:
我的要求是一个简单的聊天应用程序..当用户登录时必须显示绿色图像,而当用户注销时图像必须消失...所以在我的数据库中,我有在线列..当用户登录时更新为true,并在他注销时更新为false
当您从数据库中获取数据时,您会看到类似以下内容的内容:
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();


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";
	}
}


这篇关于在asp.net的datagrid的itemtemplate中显示图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆