This is a migrated thread and some comments may be shown as answers.

How to set Cell to ReadOnly

6 Answers 585 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 21 Sep 2012, 04:15 PM
Hi,

I'm trying to set various properties of cells conditionally in the ItemDataBound event. So far I've been able to get values and adjust their styles, but have not found a ReadOnly property in the GridDataItem.

Here is my test code within RadGrid1_ItemDataBound...
if (e.Item is GridDataItem)
{
    foreach (DataRow row in Headers.Rows)
    {
        String columnName = row[0].ToString();
        String groupColumnHeader = row[1].ToString();
        String columnHeader = row[2].ToString();
        String attributeType = row[3].ToString();
        String targetValue = row[4].ToString();
 
        GridDataItem gridRow = (GridDataItem)e.Item;
        String ReportID = gridRow.GetDataKeyValue("report_id").ToString();
 
        if (ReportID.Equals(today) && attributeType.Equals("ManualEntry")) // report_id is a string in the format "yyyyMMdd"
        {
            gridRow[columnName].BorderColor = System.Drawing.Color.Green;
            gridRow[columnName].BorderWidth = new Unit("1px");
            columnValue = ((DataRowView)gridRow.DataItem)[columnName].ToString();
            Label1.Text += "GridDataItem::columnName=" + columnName + "::columnValue=" + columnValue + " <br>";
        }
        else if (attributeType.Equals("ManualEntry"))
        {
            gridRow[columnName].BackColor = System.Drawing.Color.LightGray;
            //gridRow[columnName].ReadOnly = false;
        }
    }
}



Thanks,
David

6 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 21 Sep 2012, 06:53 PM
Hi David,

Are you trying to set the ReadOnly property for the whole column, or for the cell in that particular row? 

If you want to set the ReadOnly property for the whole column, try the following code: 

((GridBoundColumn)RadGrid1.MasterTableView.GetColumnSafe("Column1")).ReadOnly = true;

If these are GridBoundColumns, then they won't be able to edit the values unless they put the row in Edit Mode. In the ItemDataBound event, you could set the Enabled/ReadOnly property of the textbox that corresponds to the column you want to be read only, for that record. 

Could you please elaborate on how the RadGrid is setup (types of columns, edit mode)? 

Casey
0
David
Top achievements
Rank 1
answered on 21 Sep 2012, 07:27 PM
Hi Casey,

This is all about targeting specific cells. The grid is built programmatically where the MasterTableView.EditMode is set to InPlace. This is also where all the non-manual entry typed columns are set to ReadOnly=true (!attributeType.Equals("ManualEntry")).

However, the ItemDataBound event is where I need to get in and do surgery on some of the manual entry cells setting them to ReadOnly. Now that I know about a textbox property, I'll begin looking for it!

Thanks,
David
0
Casey
Top achievements
Rank 1
answered on 21 Sep 2012, 07:39 PM
Hi David,

I hope you find the below code helpful!

Casey

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        if (e.Item.IsInEditMode)
        {
            GridDataItem dataItem = e.Item as GridDataItem;               
            TextBox tbRO = (TextBox)dataItem["Column1"].Controls[0];
            tbRO.ReadOnly = true;
        }
    }
}
0
David
Top achievements
Rank 1
answered on 21 Sep 2012, 08:06 PM
Hi Casey,

Getting closer! It compiled fine, but throws an out of range error.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Is index=0 defined as the TextBox portion of the item?

Updated code snippet...
if (e.Item is GridDataItem)
{
    foreach (DataRow row in Headers.Rows)
    {
        String columnName = row[0].ToString();
        String groupColumnHeader = row[1].ToString();
        String columnHeader = row[2].ToString();
        String attributeType = row[3].ToString();
        String targetValue = row[4].ToString();
        TextBox cellTextBox;
 
        GridDataItem gridRow = (GridDataItem)e.Item;
        String ReportID = gridRow.GetDataKeyValue("report_id").ToString();
 
        if (ReportID.Equals(today) && attributeType.Equals("ManualEntry")) // report_id is a string in the format "yyyyMMdd"
        {
            gridRow[columnName].BorderColor = System.Drawing.Color.Green;
            gridRow[columnName].BorderWidth = new Unit("1px");
            columnValue = ((DataRowView)gridRow.DataItem)[columnName].ToString();
            Label1.Text += "GridDataItem::columnName=" + columnName + "::columnValue=" + columnValue + " <br>";
        }
        else if (attributeType.Equals("ManualEntry"))
        {
            columnValue = ((DataRowView)gridRow.DataItem)[columnName].ToString();
            gridRow[columnName].BackColor = System.Drawing.Color.LightGray;
            cellTextBox = (TextBox)gridRow[columnName].Controls[0];
            cellTextBox.ReadOnly = true;
 
            Label1.Text += "GridDataItem::columnName=" + columnName + "::columnValue=" + columnValue + " <br>";
        }
    }

Cheers,
David
0
David
Top achievements
Rank 1
answered on 23 Sep 2012, 09:23 PM
Hi Casey,

My requirement is that all columns designated "ManualEntry" in today's row be editable and that unpopulated cells in the past be editable. In addition, I've been given the style requirements that all populated cells in the past will be grayed out and all unpopulated (editable) ones will be white.

Here's the problem: I have not been able to observe cell values in order to set their properties. I've tried the basic Text property (and the TextBox control property), but they're always empty. I've also tried casting the data item to DataRowView for the ToString: ((DataRowView)gridRow.DataItem)[columnName].ToString(). This does work for getting cell value into my test output, but when the user clicks edit, the page throws an "Object reference not set to an instance of an object" error.

Why is the Text property empty? Could it be because the grid is generated programmatically? Everything except the RadDatePickers are in the code-behind.

Here's the current state of my scratch-work...
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    String today = "20110903";
    //String today = DateTime.Today.ToString("yyyyMMdd");
    String columnText = "";
    TextBox columnTextBox;
 
    DataTable Headers = WastewaterGrid.GetHeaders();
 
    if (e.Item is GridDataItem)
    {
        GridDataItem gridRow = (GridDataItem)e.Item;
        GridTableCell gridCell;
        String ReportID = gridRow.GetDataKeyValue("report_id").ToString();
 
        foreach (DataRow row in Headers.Rows)
        {
            String groupName = row[0].ToString();
            String columnName = row[1].ToString();
            String columnHeader = row[2].ToString();
            String attributeType = row[3].ToString();
            String targetValue = row[4].ToString();
 
            gridCell = (GridTableCell)gridRow[columnName];
 
            if (ReportID.Equals(today) && attributeType.Equals("ManualEntry"))
            {
                gridCell.BackColor = System.Drawing.Color.White;
                gridCell.BorderColor = System.Drawing.Color.Green;
                gridCell.BorderWidth = new Unit("1px");
                //columnText = gridCell.Text;
                columnText = ((DataRowView)gridRow.DataItem)[columnName].ToString();
                Label1.Text += "today::columnName=" + columnName + "::columnText=" + columnText + " <br>";
            }
            else if (e.Item.IsInEditMode)
            {
                columnTextBox = (TextBox)gridCell.Controls[0];
                columnTextBox.Enabled = false;
                columnTextBox.BorderStyle = BorderStyle.None;
                columnTextBox.BackColor = System.Drawing.Color.LightGray;
                columnTextBox.ReadOnly = true;
                columnText = ((DataRowView)gridRow.DataItem)[columnName].ToString();
                Label1.Text += "IsInEditMode::columnName=" + columnName + "::columnTextBox=" + columnTextBox.Text + " <br>";
 
            }
        }
    }
 
    if ((e.Item is GridDataInsertItem) && e.Item.IsInEditMode)
    {
        Label1.Text += "insert operation triggered<br>";
    }
    else if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        Label1.Text += "Edit operation triggered<br>";
    }
}

Thanks,
David
0
David
Top achievements
Rank 1
answered on 25 Sep 2012, 03:24 AM
I figured out it was a pebkac error. I was assigning the RadGrid1_ItemDataBound method to the ItemCreated event; the stage at which there are no data in the grid yet.
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or