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
// 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 :)
if
(e.Item is GridDataItem)
{
GridDataItem dataItem = (GridDataItem)e.Item;
var column = this.gvLUs.MasterTableView.AutoGeneratedColumns[0];
}
I still need help accessing the autogenerated columns in the edit table.
Thanks!
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.
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?
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 :)