Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

datagrid - Using C# How can I get the Value from a Telerik GridCheckBoxColumn

I have a Telerik RADGrid with a GridCheckBoxColumn I want to entire row background color to be green if the checkbox value is true. as my first step I was trying to get the value of the checkbox column using the code below.

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

    }

But this code does not find any of the rows, the value for CheckBox is always null. How can I get those values

I have modified my code and I am now able to get the value of the check box. I can still not figure out how to set the back color of the cell or row. here is the modified code.

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

    }
question from:https://stackoverflow.com/questions/65832430/using-c-sharp-how-can-i-get-the-value-from-a-telerik-gridcheckboxcolumn

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Good that you figured out how to access the value of the checkbox. To set the color of the entire row I would recommend creating a CSS class that defines the color (and any other styles you want to set). Then apply that class to your rows as needed.

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

Then the CSS class might be defined as:

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

Here is a link to a Telerik doc that also describes this and gives an example of how to modify individual columns as well.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...