使用 C# 如何从 Telerik GridCheckBoxColumn 获取值

问题描述

我有一个带有 GridCheckBoxColumn 的 Telerik RADGrid,如果复选框值为 true,我希望整行背景颜色为绿色。作为我的第一步,我尝试使用下面的代码获取复选框列的值。

protected void RadGrid1_ItemDataBound(object sender,Telerik.Web.UI.GridItemEventArgs e)
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            CheckBox chkbox = item.FindControl("chkPriority") as CheckBox;
            if (chkbox != null && chkbox.Checked)
            {
                string id_ = item["p_id"].Text;
            }
        }

    }

但是这段代码没有找到任何行,CheckBox 的值始终为空。我怎样才能获得这些值

我修改了代码,现在可以获取复选框的值。我仍然不知道如何设置单元格或行的背景颜色。这是修改后的代码。

protected void RadGrid1_ItemDataBound(object sender,Telerik.Web.UI.GridItemEventArgs e)
    {

        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            CheckBox chkbox = item["chkPriority"].Controls[0] as CheckBox;
            if (chkbox.Checked == true)
            {
                
                GridDataItem dataItem = e.Item as GridDataItem;
               
                
            }
        }

    }

解决方法

很高兴您知道如何访问复选框的值。 要设置整行的颜色,我建议创建一个定义颜色(以及您想要设置的任何其他样式)的 CSS 类。然后根据需要将该类应用于您的行。

protected void RadGrid1_ItemDataBound(object sender,GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        CheckBox chkbox = item["chkPriority"].Controls[0] as CheckBox;
        if (chkbox.Checked == true)
        {
             item.CssClass += "MySpecialRow";
            
        }
    }
}

那么CSS类可能定义为:

.MySpecialRow 
{ 
  background-color: green; 
  color: white; 
  font-weight: bold;
}

这是一个指向 Telerik 文档的链接,该文档也描述了这一点,并给出了如何修改单个列的示例。

https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/appearance-and-styling/conditional-formatting

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...