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

Update Cell Value

9 Answers 1005 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mohsen
Top achievements
Rank 1
Mohsen asked on 10 Aug 2016, 09:08 AM

The following code doesn't work:

1.string newDate = "...";
2.grid.CurrentRow.Cells["StartDate"].Value = newDate;

9 Answers, 1 is accepted

Sort by
0
Joe
Top achievements
Rank 2
answered on 10 Aug 2016, 02:26 PM

Mohsen --

 

What does not work?

Is grid your grid name?  Try something like the below (adding this. to qualify it)

What is the error?

this.report_gridview.CurrentRow.Cells["deliveryID"].Value ="test value"

0
Mohsen
Top achievements
Rank 1
answered on 11 Aug 2016, 05:17 AM

I'm going to update a cell value with the following code:

1.string newDate = "...";
2.this.radGridView.CurrentRow.Cells["StartDate"].Value = newDate;

But it doesn't work and no exception occurs. In fact, I think the code works but the grid need to be refreshed. Any idea?

0
Joe
Top achievements
Rank 2
answered on 11 Aug 2016, 06:42 AM

Is the grid databound?

If the cell is databound then doing an updated like that won't work, you would have to change the underlying datasource and refresh the grid.

How are you populating the grid with data?

Perhaps post the code that is populating the grid.

There is a Application.DoEvents() but I don't think that is the problem here.

 

0
Mohsen
Top achievements
Rank 1
answered on 11 Aug 2016, 07:08 AM

Here is the code to fill the gridview:

01.DateTime today = db.GetServerDate();
02.            var query = from p in _dbRahat.Projects
03.            where p.ProjectType_Id == Convert.ToInt32(cmbProjectTypes.SelectedValue)
04.            orderby p.Code
05.            select new
06.            {
07.               p.Id,
08.               p.Title,
09.               p.Code,
10.               StartDate = GetPastDays(p.StartDate, today),
11.            };
12. 
13.            grid.DataSource = query;
14. 
15.            if (grid.Rows.Any())
16.            {
17.                grid.Select();
18.                grid.Rows.Last().IsCurrent = true;
19.                grid.TableElement.ScrollToRow(grid.Rows.First());
20.            }

How can I solve the issue?

0
Joe
Top achievements
Rank 2
answered on 11 Aug 2016, 07:39 AM

I am sure somebody else can have a more elegant solution, but what I have done in the past is to either:

1. update the underlying database.  If you do that then you refresh the grid after the data is updated and it should reflect the new data.

2. add a column to the grid that is a unbound column, then your code from before should update that column.  This is what I have done normally, sometimes even hiding the databound column and only showing the new column I created.

If you create the grid ahead of time and use field names then you should be able to modify your select new part to use those names and it will populate it.

IE: make sure your grids fields are Id, Title, Code, StartDate and whatever unbound ones you want.

Joe

 

0
Joe
Top achievements
Rank 2
answered on 11 Aug 2016, 07:43 AM

A correction (I HATE that you can't modify posts here!)

When I said: "...should be able to modify your select new part to use those names and it will populate it" -- ignore the modify part, your select statement is fine I think.

Just go into your grid properties and make sure that you add the columns ahead of time vs letting it automaticly create them and set each ones field property to the field name.

0
Mohsen
Top achievements
Rank 1
answered on 11 Aug 2016, 08:23 AM
I don't agree with you. Your solution is not logical and is so complex. I want admins of this forum to help me.
0
Joe
Top achievements
Rank 2
answered on 11 Aug 2016, 01:01 PM

Like I said, I am sure somebody else will have a more elegant solution.

My solution is simply what I have used and have working on multiple projects.

Good luck, maybe you should consider paying for support and opening a trouble ticket - they are very good at responding.

I do wonder however, what is so complex or 'not logical' about "update your underlying data source then refresh the grid"?

 

0
Hristo
Telerik team
answered on 15 Aug 2016, 06:50 AM
Hi guys,

Thank you for writing.

Mohsen, the code snippet showing how you are binding the grid indicates that you are actually binding it to a collection of anonymous types. In this scenario, the grid binds successfully, however, it cannot be edited. Exactly the same behavior is observed with the standard MS grid. You can check the following thread discussing the same question: http://www.telerik.com/forums/problem-while-updating-the-cell-from-item-template-of-herarchical-grid.

As a solution, I can suggest creating a simple class having all the properties you want and use it in your query: http://stackoverflow.com/questions/534690/return-anonymous-type-results.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
Tags
GridView
Asked by
Mohsen
Top achievements
Rank 1
Answers by
Joe
Top achievements
Rank 2
Mohsen
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or