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

Accessing AutoGenerated Columns

6 Answers 105 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 14 Jul 2010, 02:12 PM
Hi,

I have a 2-part question. I have a radgrid populated using a datatable, with columns that are autogenerated. All is well, I can view the edit form and drop down into my events.

My first question is how are autogenerated controls named? I'd like to disable the first column so a user cannot edit it as it is an identity column but I'm having trouble finding the control. I use the below when I know the string Id of the control:

string

 

xx= (userControl.FindControl("xx") as TextBox).Text;

But obviously can't use this method without knowing the string id of the autogenerated edit controls.

Second question is what is the best method to get the changed values of autogenerated controls?
I have so far:

 

 

GridEditableItem editedItem = e.Item as GridEditableItem;

 

 

 

 

 

// Get the new values

 

 

 

 

 

Hashtable newValues = new Hashtable();

 

e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

But I may have a mix of different types in the newValues hashTable and thus can't to a CopyTo strongly typed array.

Need some help please, thanks!

6 Answers, 1 is accepted

Sort by
0
Brad
Top achievements
Rank 1
answered on 14 Jul 2010, 08:09 PM
Well, I figured out how to get my values and column names out:

// Extract out our column names and values

 

foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)

 

{

 

if (!(column is GridEditableColumn)) continue;

 

 

if (editedItem[column.UniqueName].Controls[0] is TextBox)

 

{

 

if (ic != e.Item.OwnerTableView.RenderColumns.Count())

 

strUpdate += column.UniqueName.ToString() +

" = " + (editedItem[column.UniqueName].Controls[0] as TextBox).Text + ",";

 

 

else

 

strUpdate += column.UniqueName.ToString() +

" = " + (editedItem[column.UniqueName].Controls[0] as TextBox).Text;

 

}

ic++;

}



I could still use some help on programmatically disabling the first column before the edit screen is shown if possible!

Thanks :)
0
Brad
Top achievements
Rank 1
answered on 14 Jul 2010, 08:35 PM
And the answer to the first piece is:

if

 

(e.Item is GridDataItem)

 

{

 

    GridDataItem dataItem = (GridDataItem)e.Item;

 

 

    var column = this.gvLUs.MasterTableView.AutoGeneratedColumns[0];
}

0
Brad
Top achievements
Rank 1
answered on 14 Jul 2010, 08:46 PM
Scratch that, the last post gives you access to the autogenerated columns in the master table.

I still need help accessing the autogenerated columns in the edit table.

Thanks!
0
Princy
Top achievements
Rank 2
answered on 15 Jul 2010, 11:30 AM
Hello Brad,

 I guess you want to hide the first column (AutoGeneratedColumn) when the grid is in edit mode. If that the case, then try the following code snippet.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (RadGrid1.EditItems.Count > 0)
        {
            RadGrid1.MasterTableView.AutoGeneratedColumns[0].Visible = false;
        }
        else
            RadGrid1.MasterTableView.AutoGeneratedColumns[0].Visible = true;
    }

Thanks,
Princy.


0
Brad
Top achievements
Rank 1
answered on 15 Jul 2010, 01:17 PM
Hey Princy,

Thanks for the reply. That doesn't solve it for me. It throws an out of index error if in PreRender so I tried it on ItemCommand, in the edit section. It hides the column on the master table, not in the edit window. I think I may need to resign myself to dynamically generate and remove the columns programmatically.

If I can get around that though I would be very happy. Something like making the first column read only. I read somewhere that it wouldn't be autopopulated into the edit form in that case.

I'm having some problems getting the dynamic columns sorted out just right. I can add them OK but am having trouble actually removing the column from the grid, so far I can only remove the data in that column. That and EditForm = "Autogenerated" is having trouble when being used with programmatic column generation.

I guess the initial question still is there then, is there anyway to directly access the columns in the Edit Form?
0
Brad
Top achievements
Rank 1
answered on 15 Jul 2010, 02:24 PM
Figured it out:

protected void gvLUs_ItemCommand(object source, GridCommandEventArgs e)
    {
        // Disable the ID column
        GridBoundColumn boundColumn;
        boundColumn = (GridBoundColumn)gvLUs.MasterTableView.AutoGeneratedColumns[0];
        boundColumn.ReadOnly = true;
     ...
}

Thanks for all the help :)
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Brad
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or