GirdView显示“有”“无” 状态

本文介绍如何在ASP.NET应用程序中使用GridView控件进行数据绑定,并实现对关联数据的查询与结果显示。通过示例代码展示了如何设置行的样式、转换日期格式以及根据查询结果动态显示“有”或“无”。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从数据库中获取数据,同时查询,再返回,如果有数据,显示“有”,如果没有,则显示“无”

  两张表
 Id是从绑定数据的时候的来的
 //注意引索值的获取
    protected void GVmasterList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时,行背景色变
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            //鼠标移出时,行背景色变
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }
 //注意
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            //在gridview数据绑定时将第4列的文本数据转换成短日期格式
            e.Row.Cells[3].Text = Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString();
            Label Label1seat = (Label)e.Row.FindControl("lblTAppendix");
            int nid = int.Parse(GVTopicList.DataKeys[e.Row.RowIndex].Value.ToString());
            DataSet dst = new DataSet();
            SqlDataAdapter dap = ttopicmanager.PTopicListBytopicIdGet(nid);
            dap.Fill(dst);
            DataTable dt = dst.Tables[0];
            if (dt.Rows.Count > 0)
            {
                Label1seat.Text = "有";
            }
            else
            {
                Label1seat.Text = "无";
            }
        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值