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

Access cell text value

6 Answers 113 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 1
Jim asked on 13 Apr 2011, 09:15 AM
I want to access the text value of all selected cells in the grid.

I try ,,
            foreach (GridViewCellInfo  cellInfo in this.radGridView1.SelectedCells)
            {
                textBox1.Text = cellInfo.Item.ToString();
            }
but don't get the value.

There is lot's of references to GridViewCellInfo   Value but my GridViewCellInfo   only offers Column,Equals,GetHashCode,GetType,Item,ToString

Help !
Thanks
Jim

6 Answers, 1 is accepted

Sort by
0
Jim
Top achievements
Rank 1
answered on 13 Apr 2011, 11:47 AM
My grids selectionmode is extended and selectionunit is cell
The cellInfo below seems come back as type Customer ( i.e. the whole row ).
And I can access the columns i.e cu.ContactTitle below.
But cu.ContactTitle wasn't the selected cell so how can I get/know the selected cell.


foreach (GridViewCellInfo  cellInfo in this.radGridView1.SelectedCells)
{
    object zz = cellInfo.Item;
    Customer cu = (Customer)zz;
    textBox1.Text = cu.ContactTitle;
}
0
Jim
Top achievements
Rank 1
answered on 13 Apr 2011, 12:53 PM
Well below sort of works
So if I could access the fields of Customer using the unique string i.e..
sting s = cu["City"]
then that would be a cleaner solution.
Any ideas ???



foreach (GridViewCellInfo  cellInfo in this.radGridView1.SelectedCells)
{
    GridViewColumn gvc = cellInfo.Column;
    string uniqueName = gvc.UniqueName;
    object zz = cellInfo.Item;
    Customer cu = (Customer)zz;
     
    if (uniqueName.Equals("City"))
    {
        textBox1.Text = cu.City;
    }
    else if (uniqueName.Equals("Address"))
    {
        textBox1.Text = cu.Address;
    }
    else
    {
        textBox1.Text = "To be written";
        // And the rest of the column names.
    }
     
}
0
Jim
Top achievements
Rank 1
answered on 14 Apr 2011, 10:22 AM

I also wanted to change the cell background colour of the single cell(s)

But can only manage 

cellInfo.Column.Background = new SolidColorBrush(Colors.Red);

which changes the whole column !
Any idea how to get access to the actual cell. The item property only seems to be the data object e.g. Customer.

Help Help ......
Thanks Jim

0
Maya
Telerik team
answered on 18 Apr 2011, 03:48 PM
Hello Jim,

Looking at the code-snippet you provided for running through the selected cells, I get the impression that you managed to resolve the issue. Am I right or am I missing something from your initial requirements ? 
As for setting the background of the cells, may you share a bit more details about the exact purpose of setting it ? What are the view and functionality that you want to accomplish ? Generally, you might find interesting the implementation of CellStyleSelector
 

Regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jim
Top achievements
Rank 1
answered on 18 Apr 2011, 04:21 PM
I resolved setting the single cell data but not the style.
Typically users select cells and click the button. We want the cells to be highlighted.
The user selects more cells and clicks the button.
Then the additional cells are highlighted in addition to the first ones.

The "CellStyleSelector." seems to be dependant on the data.

It seems that you can get the column from cellInfo but not the actual cell.

Thanks
Jim
0
Maya
Telerik team
answered on 19 Apr 2011, 02:08 PM
Hi Jim,

Getting the corresponding GridViewCell from the GridViewCellInfo is not quite easy since the virtualization of the grid is turned on by default. Consequently, only the visible cells can be accessed. Still, what you may do is to get the GridViewRow from the item you have already found and get its cells. It may be something similar to:

foreach (GridViewCellInfo cellInfo in this.clubsGrid.SelectedCells)
            {              
                GridViewColumn column = cellInfo.Column;
                 
                string uniqueName = column.UniqueName;
                object item = cellInfo.Item;
                Club club = (Club)item;
 
                var row = this.clubsGrid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item) as GridViewRow;
                foreach (GridViewCell cell in row.Cells)
                {
                    if (cell.Column == cellInfo.Column)
                    {
                        cell.Background = new SolidColorBrush(Colors.Cyan);
                    }
                }
            }

I am sending you a sample project so that you can test the functionality.
 


Kind regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Jim
Top achievements
Rank 1
Answers by
Jim
Top achievements
Rank 1
Maya
Telerik team
Share this question
or