Question

I have a GridView and I want to refresh a row (not the whole grid) when it gets selected. I tried to change Text property for each of GridView.SelectedRow.Cells when Grid selected index changes. It seems to work for DataField, but not for TemplateField. For TemplateField I got strange results - the value for selected row changes properly, but when I select another row, the value of TemplateField for previously selected row becomes blank. Brief illustration :

1. Nothing selected
--------------------------
id          template_field
--------------------------
 1          value_1
 2          value_2

2. First record selected
--------------------------
id          template_field
--------------------------
 1          updated_value_1
 2          value_2

3. Second record selected
--------------------------
id          template_field
--------------------------
 1          [blank!]
 2          updated_value_2

Eventually, I end up with blank template_field for each record except selected. What is the proper way to change the text of TemplateField ?

Was it helpful?

Solution

Try adding a TextBox control to your template, and change it instead of the cell contents. You should be able to get at it via something like this:

TextBox tb = GridView.SelectedRow.Cells[0].Controls[0] as TextBox;
if(tb != null)
   tb.Text = newValue;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top