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

[Solved] Editing GridViewDataColumn cell

5 Answers 376 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Amit Patel
Top achievements
Rank 1
Amit Patel asked on 30 Oct 2009, 07:01 PM

 

Hello,
I am adding GridViewDataColumn to my grid dynamically as showen below; However, if i type something to cell once the column has been added, the text doesn't show up in cell. By default grid has IsReadOnly set to false.  Can you tell me what property i need to set in order to make the cell editable? Thanks,Amit

GridViewDataColumn
column = new GridViewDataColumn() { Header = "Test" };

 

 

_grid.Columns.Add(column);

5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 03 Nov 2009, 06:36 AM
Hi,

First of all you may need to bind this column (set DataMemberBinding).

All the best,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Amit Patel
Top achievements
Rank 1
answered on 03 Nov 2009, 02:08 PM


I can't set the dataMemeberBinding at the column level. I need to create dynamic column and provide the dataContext at the cell level. Each cell will have different business object attached to it. Below I have pasted part of the code that I am currently using  to create dynamic column. My issue is that the changes in the cell are not propagated back to the business object. If I can get hold of the edit Template during the row loaded event(the way I am ding for cell template) then I can set the binding on the TextBox with binding mode set to Two-way, and I am sure that will do the trick.

 

 

var column = new ExtendedGridViewDataColumn()

 

{

IsFilterable =

false,

 

Header = columnName,

UniqueName = columnName,

Index = index,

DataType = dataType

};

column.CellTemplate = (

DataTemplate)Resources["_cellTemplate"];

 

column.CellEditTemplate = (

DataTemplate)Resources["_cellEditingTemplate"];

 

_gridViewResourceRequirement.Columns.Add(column);


 

private

 

void _gridViewResourceRequirement_RowLoaded(object sender, RowLoadedEventArgs e)

 

{

 

var requirement = e.Row.DataItem as ResourceRequirement;

 

 

if (requirement != null)

 

{

 

var attributeValue = requirement.AttributeValues;

 

 

foreach (var cell in e.Row.Cells)

 

{

 

if (cell.Column is ExtendedGridViewDataColumn)

 

{

 

var col = cell.Column as ExtendedGridViewDataColumn;

 

 

var value = attributeValue.Where(a => a.Index == col.Index && a.DataType == col.DataType).FirstOrDefault();

 

 

if (value == null)

 

{

value =

new AttributeValue()

 

{

Id =

Guid.NewGuid(),

 

Index = col.Index,

DataType = col.DataType

};

requirement.AttributeValues.Add(value);

}

 

cell.DataContext = value;

 

TextBlock textBlock = cell.ChildrenOfType<TextBlock>().FirstOrDefault();

 

textBlock.DataContext = value;

 

if (textBlock != null)

 

{

 

Binding binding = null;

 

 

switch ((AttributeDataType)value.DataType)

 

{

 

case AttributeDataType.Date:

 

binding =

new Binding("Date");

 

binding.Mode =

BindingMode.TwoWay;

 

textBlock.SetBinding(

TextBlock.TextProperty, binding);

 

 

break;

 

 

case AttributeDataType.Flag:

 

binding =

new Binding("Flag");

 

binding.Mode =

BindingMode.TwoWay;

 

textBlock.SetBinding(

TextBlock.TextProperty, binding);

 

 

break;

 

 

case AttributeDataType.Number:

 

binding =

new Binding("Number");

 

binding.Mode =

BindingMode.TwoWay;

 

textBlock.SetBinding(

TextBlock.TextProperty, binding);

 

 

break;

 

 

case AttributeDataType.Text:

 

binding =

new Binding("Text");

 

binding.Mode =

BindingMode.TwoWay;

 

textBlock.SetBinding(

TextBlock.TextProperty, binding);

 

 

break;

 

}

}

}

}

}

}

0
Accepted
Nedyalko Nikolov
Telerik team
answered on 05 Nov 2009, 11:55 AM
Hello Amit Patel,

As I understand you have some properties of your business object and you want a column which can display (edit) only one of them depending from some business logic. If I'm right I don't think that RowLoaded event is not the right place to create custom editor control.

I'm attaching a sample project which demonstrates how to extend editable columns. The project is compatible with 2009.Q2 (latest internal assemblies) and 2009.Q3 release (just uncomment all commented code). The version for 2009.Q3 is a better choice since the implementation has full integration with RadGridView's edit and validation mechanisms.

I'll try to clarify a little all overridden methods and their usage:

public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) :
This method is called to create cell element (presenter) for every cell.

public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem) :
This method is called when GridViewCell is about to enter into edit mode, the result of this is set as an editing element of the GridViewCell.

public override IList<ValidationError> UpdateSourceWithEditorValue(GridViewCell gridViewCell) :
This method is part of the validation mechanism. Its purpose is to update underlying data object and return all binding errors. This explicit update is needed because when you create an editing binding it is better to update source explicitly than default binding behavior.

public override object GetNewValueFromEditor(object editor) :
This method is part of the editing system and should be overridden if you have some custom editors to get proper new value (entered by the user) from it.

Of course last two methods are available only with the 2009.Q3 release.
Hope this helps. I'll be glad to assist if you have further questions.

Sincerely yours,
Nedyalko Nikolov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Amit Patel
Top achievements
Rank 1
answered on 05 Nov 2009, 02:31 PM
Thanks Nedyalko for spending time to create sample project! This is a marvelous solution for my need.  Now understand more about the grid and its methods.
0
Surender Reddy
Top achievements
Rank 1
answered on 07 Mar 2010, 12:20 AM
Hi,

Looks like there is a small bug in the example (in GridViewExtendedColumnExample.cs)

Instead of
List<ValidationError> errors = null;
probably it should be

List<ValidationError> errors = new List<ValidationError>();

Otherwise it gives an error "Object reference not set to an instance of an object".

Regards,
Surender.
Tags
GridView
Asked by
Amit Patel
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Amit Patel
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Surender Reddy
Top achievements
Rank 1
Share this question
or