我无法在gridview中显示值。 [英] I am unable to display value in gridview.

查看:67
本文介绍了我无法在gridview中显示值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我正在使用在线考勤管理系统,

但是我遇到了一个小问题。



我从中获取所有记录数据库并在gridview中显示,但问题是当我试图用gridview的每一行显示计数值时。



我尝试过:



Actually i am working with online attendance management system ,
but i am facing a little problem.

I fetch all record from database and display in gridview but the problem is when i am trying to display count value with each row of gridview.

What I have tried:

<pre>void gvattendcount()
    {

        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        foreach (GridViewRow row in GVattend.Rows)
        {
            string rollno = (((Label)row.FindControl("lblroll")).Text);
            string attendlecture = (((Label)row.FindControl("lblattend")).Text);

            string query = "SELECT COUNT(*) as attendlecture FROM class8_attend where  rollno ='" + rollno + "' AND present ='p'";

            DataTable dtAdmin = new DataTable();
            SqlDataAdapter da;
            da = new SqlDataAdapter(query, cnn);
            da.Fill(dtAdmin);

            if (dtAdmin.Rows.Count > 0)
            {
                using (SqlCommand cmd = new SqlCommand(query, cnn))
                {
                    cnn.Open();
                    Int32 count = (Int32)cmd.ExecuteScalar();
                    attendlecture = count.ToString();
                    cnn.Close();
                }

                
            }
        }
    }









CSS -





CSS-

<asp:TemplateField  HeaderText="ATTENDED LECTURES" >
           <ItemTemplate>
           <center>
            <asp:Label ID="lblattend" runat="server" Text=""></asp:Label>
           </center>
           </ItemTemplate>
           </asp:TemplateField>

推荐答案

您使用 Bind 错过了列的赋值。

You have missed assign value of the column using Bind.
<asp:TemplateFiel>
<ItemTemplate>
        <asp:Label ID="lblattend" runat="server" Text='<%# Bind("attendlecture") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFiel>


string attendlecture = (((Label)row.FindControl("lblattend")).Text);

// ....

attendlecture = count.ToString();





您需要了解值类型和引用类型之间的区别。当您将lblAttend的值加载到您正在进行的参与选择时,该变量不是对lblattent.Text属性的引用,它只是它的副本,因此当您更新它时,您只会覆盖该副本。你需要这样做





You need to appreciate the difference between "value" types and "reference" types. When you load the value of lblAttend into attendlecture that is all you are doing, that variable is not a reference to the lblattent.Text property, it is simply a copy of it, so when you update it you're only overwriting the copy. You need to do this instead

Label lblattend = (Label)row.FindControl("lblattend");

// ....

lblattend.Text = count.ToString();


这篇关于我无法在gridview中显示值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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