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

How to get cell Information

21 Answers 504 Views
GridView
This is a migrated thread and some comments may be shown as answers.
NickZ
Top achievements
Rank 1
NickZ asked on 15 Jun 2009, 05:01 AM
Hello,  I have a RadGridView.  How can I get a cell information when clicking on it?  I would like to get Cell's row number, column number and cell value.  Thanks.

21 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 15 Jun 2009, 06:52 AM
Hi NickZ,

Here are three methods that you can use:

public object GetCellValue(GridViewDataControl gridView, int rowIndex, int cellIndex)  
{  
    var row = gridView.ItemsControl.ItemsGenerator.GenerateItemAtIndex(rowIndex) as GridViewRow;  
    object cellValue = null;  
 
    if (row != null && row.Cells.Count >= cellIndex)  
        cellValue = row.Cells[cellIndex].Content;  
 
    return cellValue;  
}  
 
public int GetCellRowIndex(GridViewDataControl gridView, GridViewCell cell)  
{  
    var record = cell.ParentRow.Record as DataRecord;  
    return record.DataSourceIndex;  
}  
 
public int GetCellColumnIndex(GridViewDataControl gridView, GridViewCell cell)  
{  
    return gridView.Columns.IndexOf(cell.Column);  

Hope this helps.

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
NickZ
Top achievements
Rank 1
answered on 16 Jun 2009, 01:13 AM
Hi Milan,  thanks for your help.  I copied your codes to my project and got two errors:

1. An error said that Telerik.Windows.Controls.GridView.GridViewRow does not contain a definition of Cells ...  Am I missing something here?
2. Another one is GridViewRowItem GridViewCellBase.ParentRow Error: Telerik.Windows.Controls.Gridview.GridViewCellBase.ParentRow' is inaccessible due to its protection level.

Thanks again.
0
Milan
Telerik team
answered on 16 Jun 2009, 08:14 AM
Hello NickZ,

Could you confirm that you are actually using the 526 version of our controls?
Is very strange that the compiler is complaining about the Cells property because it is present in 526.

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
NickZ
Top achievements
Rank 1
answered on 17 Jun 2009, 04:22 AM
Hi Milan, you were right.  After downloading the 526 version, all the missing parts are shown up. 

In my case, I have a RadGridView and I would like to display cell's information (value, row index, column index) which clicking on a cell.  Here is part of my codes:

    public partial class Page : UserControl  
    {  
        public Page()  
        {  
            InitializeComponent();  
            Loaded += new RoutedEventHandler(Page_Loaded);  
        }  
 
        void Page_Loaded(object sender, RoutedEventArgs e)  
        {  
            mygrid.ItemsSource = "H e l l o W o r l d !".Split();  
            mygrid.RowLoaded += new EventHandler<RowLoadedEventArgs>(mygrid_RowLoaded);  
        }  
 
        void mygrid_RowLoaded(object sender, RowLoadedEventArgs e)  
        {  
                foreach (GridViewCell cell in e.Row.Cells)  
                {  
                    //int rownumber = GetCellRowIndex(mygrid, cell);  
                    //int columnnumber = GetCellColumnIndex(mygrid, cell);  
                    //object cellvalue = GetCellValue(mygrid, rownumber, columnnumber);  
 
                    cell.MouseLeftButtonDown += new MouseButtonEventHandler(cell_MouseLeftButtonDown);  
                }  
        }  
 
        void cell_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)  
        {  
            MessageBox.Show("Got you!");  
        // will display cell information here!  
        }  
 
.......  
 
My question is how I can pass those parameters to cell_MouseLeftButtonDown event.  Please help me out.  Thanks.

0
Milan
Telerik team
answered on 17 Jun 2009, 09:33 AM
Hello NickZ,

Here is the code that you should modify or add to get the desired cell info:

void mygrid_RowLoaded(object sender, RowLoadedEventArgs e)  
{  
    foreach (var cell in e.Row.Cells)  
    {  
        if (cell is GridViewHeaderCell)  
            continue;  
 
        cell.MouseLeftButtonDown += new MouseButtonEventHandler(cell_MouseLeftButtonDown);  
    }  
}  
 
void cell_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)  
{  
    GridViewCell cell = sender as GridViewCell;  
 
    if (cell == null)  
        return;  
 
    var columnIndex = GetCellColumnIndex(this.mygrid, cell);  
    var rowIndex = GetCellRowIndex(this.mygrid, cell);  
 
    MessageBox.Show("Value: " + cell.Value.ToString() + " Row: " + rowIndex.ToString() + " Column: " + columnIndex);  
}  
 
public static int GetCellRowIndex(GridViewDataControl gridView, GridViewCell cell)  
{  
    var record = cell.ParentRow.Record as DataRecord;  
    return record.DataSourceIndex;  
}  
 
public static int GetCellColumnIndex(GridViewDataControl gridView, GridViewCell cell  
{  
    return gridView.Columns.IndexOf(cell.Column);  
}   

Hope this works.

Sincerely yours,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
NickZ
Top achievements
Rank 1
answered on 17 Jun 2009, 01:44 PM
It works perfectly.  Thanks a lot, Milan.
0
NickZ
Top achievements
Rank 1
answered on 19 Jun 2009, 04:30 AM
Hi Milan,
I have two more questions about Rad Gridview.
1. How can I set Column header's text in my gridview?
2. When gridview itemsource is obtained, how to control the order of columns?  In my case, item source is List<type>, where <type> is a class name and is used in service to get data from database.  When my gridview displays, its column order is based on the names of property in class <type> alphabetically, which is not what I want.  I am looking for a way to set the column order programmically.
Thanks a lot.
0
NickZ
Top achievements
Rank 1
answered on 20 Jun 2009, 03:46 PM
I found solutions.  Thanks, Milan.
0
Milan
Telerik team
answered on 22 Jun 2009, 06:12 AM
Hello NickZ,

Do not hesitate to write if you have more questions.

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
NickZ
Top achievements
Rank 1
answered on 24 Jun 2009, 04:02 PM
Hi Milan.  I do have a question.  Here is my code:

<telerikDocking:RadPane x:Name="Pane01" Header="ISHTA Committed">  
    <telerikDocking:RadPane.Content> 
        <StackPanel x:Name="MainLayout">  
            <StackPanel x:Name="TopFilter" Orientation="Horizontal" Height="45" Background="Ivory" VerticalAlignment="Center">  
                <TextBlock x:Name="AreaLabel" Text="Area" Canvas.Top="20"/>  
                <telerikInput:RadComboBox x:Name="PeriodList" Width="75" Height="22" /> 
                <Button x:Name="FilterClick" Height="22" Width="75" Click="FilterClick_Click" Content="Filter" /> 
            </StackPanel> 
            <StackPanel x:Name="GridArea">  
                <data:RadGridView x:Name="ViewCommitted" AutoGenerateColumns="False" ShowGroupPanel="True" /> 
            </StackPanel> 
        </StackPanel> 
    </telerikDocking:RadPane.Content> 
</telerikDocking:RadPane> 
 

My problem is that scroll bars on both vertical and horizontal sides are disappeared in RadGridView.  I believe this is related to StackPanel.  If there is no stackpanel, both scrolls are shown correctly.   Thanks a lot.
0
Casey
Top achievements
Rank 1
answered on 24 Jun 2009, 06:04 PM
deleted and moved back to original trhead.
0
NickZ
Top achievements
Rank 1
answered on 28 Sep 2009, 08:16 PM
Hi Milan,

I just added reference from the new purchased software and I got the following warning:

'Telerik.Windows.Data.DataRecord' is obsolete: 'This class is obsolete and is not used anymore.

How can I modify the code to get rid of DataRecord in GetCellRowIndes()?

Thanks,
Nick

0
Milan
Telerik team
answered on 29 Sep 2009, 07:37 AM
Hi NickZ,

Here is the new code:

public static int GetCellRowIndex(GridViewDataControl gridView, GridViewCell cell)     
{     
    return gridView.Items.IndexOf(cell.ParentRow.DataItem);     
}    

The Items collection contains all items that the grid is bound to - it is similar to the Records collection but Items contains the actual data items instead.

Best wishes,
Milan
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
NickZ
Top achievements
Rank 1
answered on 08 Jan 2010, 04:43 AM
Hi Milan,

Today I downloaded RadControls for Silverlight Q3 2009 SP1 and I got a couple of errors in my codes.

For the method to get cell value:
public object GetCellValue(GridViewDataControl gridView, int rowIndex, int cellIndex)     
{     
    var row = gridView.ItemsControl.ItemsGenerator.GenerateItemAtIndex(rowIndex) as GridViewRow;     
    object cellValue = null;     
    
    if (row != null && row.Cells.Count >= cellIndex)     
        cellValue = row.Cells[cellIndex].Content;     
    
    return cellValue;     
}     
 
An error says that ItemsControl is not existing in GridViewDataControl.  What is the new one for getting cell value?

Also for getting cell row index:
public static int GetCellRowIndex(GridViewDataControl gridView, GridViewCell cell)        
{        
    return gridView.Items.IndexOf(cell.ParentRow.DataItem);        
}       
 
There is a warning about DataItem and it suggests to use Item instead.  I just want to make sure it is alright.

Please help me at your earliest convenience.  I really want to have it work before weekend.

Thanks a lot,
Nick
0
Milan
Telerik team
answered on 08 Jan 2010, 09:37 AM
Hello NickZ,

There are some minor API changes that need to be addressed. Here is the updated code: 

public object GetCellValue(GridViewDataControl gridView, int rowIndex, int cellIndex)
{
    var row = gridView.ItemContainerGenerator.ContainerFromIndex(rowIndex) as GridViewRow;
    object cellValue = null;
  
    if (row != null && row.Cells.Count >= cellIndex)
        cellValue = row.Cells[cellIndex].Content;
  
    return cellValue;
}

And

public static int GetCellRowIndex(GridViewDataControl gridView, GridViewCell cell)        
{        
    return gridView.Items.IndexOf(cell.ParentRow.Item);        
}

Kind regards,

Milan
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
NickZ
Top achievements
Rank 1
answered on 08 Jan 2010, 03:57 PM
Deleted. 
0
NickZ
Top achievements
Rank 1
answered on 11 Jan 2010, 10:31 PM
Hi Milan,

Thanks a lot for your help.  I have another issue in this new build.  I have a radgridview and data are added to each column as follows:
            GridViewDataColumn column = new GridViewDataColumn();  
            column.Header = "Facility";  
            column.UniqueName = "Facility";  
            column.HeaderTextAlignment = TextAlignment.Center;  
            column.DataMemberBinding = new Binding("Facility");  
            column.IsFilterable = false;  
            column.IsReadOnly = true;  
            column.Width = new GridViewLength(1, GridViewLengthUnitType.Star);  
            nzViewResults.Columns.Add(column);  // add to radgridview
 
            column = new GridViewDataColumn();  
            column.Header = "Section";  
            column.UniqueName = "Section";  
            column.HeaderTextAlignment = TextAlignment.Center;  
            column.DataMemberBinding = new Binding("Section");  
            column.IsFilterable = false;  
            column.IsReadOnly = true;  
            column.Width = new GridViewLength(1.5, GridViewLengthUnitType.Star);  
            nzViewResults.Columns.Add(column);  
 
            column = new GridViewDataColumn();  
            column.Header = "SectionID";  
            column.UniqueName = "SectionID";  
            column.DataMemberBinding = new Binding("SectionID");  
            column.IsFilterable = false;  
            column.IsReadOnly = true;  
            column.Width = new GridViewLength(1, GridViewLengthUnitType.Star);  
            nzViewResults.Columns.Add(column);  
 
            column = new GridViewDataColumn();  
            column.Header = "Location";  
            column.UniqueName = "Location";  
            column.DataMemberBinding = new Binding("Location");  
            column.IsFilterable = false;  
            column.IsReadOnly = true;  
            column.Width = new GridViewLength(1, GridViewLengthUnitType.Star);  
            nzViewResults.Columns.Add(column);  
 
            nzViewResults.Columns["SectionID"].IsVisible = false;  
 
            nzViewResults.ItemsSource = e.Result; 
You can see that visibility of one column (SectionID) is false.  So it does not show on the interface.  I added a mouse click event to each cell and when it is clicked, this hidden section id will be obtained. 
        public object GetCellValue(GridViewDataControl gridView, int rowIndex, int cellIndex)  
        {  
            //var row = gridView.ItemsControl.ItemsGenerator.GenerateItemAtIndex(rowIndex) as GridViewRow;  
            var row = gridView.ItemContainerGenerator.ContainerFromIndex(rowIndex) as GridViewRow;  
            object cellValue = null;  
            if (row != null && row.Cells.Count >= cellIndex)  
                cellValue = row.Cells[cellIndex].Content;  
 
            return cellValue;  
        }  
 
In this case, cellIndex has a value of 2, because it is the third column.  The problem is that in the current build,  it returns the value from Column #4 (Location), not SectionID column.  In the previous builds, it returns the correct value.  It seems that radgridView in the current build only includes whatever is visible. 

Is this a bug and any workarounds?

Thanks,
Nick
0
Milan
Telerik team
answered on 12 Jan 2010, 06:19 PM
Hi NickZ,

This is the expected behavior in our latest builds. Fortunately you can workaround the problem simply by  setting the Width of the column to 0 and leaving its IsVisible property to true.


Greetings,
Milan
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
NickZ
Top achievements
Rank 1
answered on 13 Jan 2010, 04:40 PM
Hi Milan,

Thanks for your reply.  I tried to set width to zero:

            column = new GridViewDataColumn();  
            column.Header = "SectionID";  
            column.UniqueName = "SectionID";  
            column.DataMemberBinding = new Binding("SectionID");  
            column.IsFilterable = false;  
            column.IsReadOnly = true;  
            column.Width = 0;  
            //column.Width = new GridViewLength(0, GridViewLengthUnitType.Star);  
            nzViewCommitted.Columns.Add(column);  
 

And I can still see that column (see attached image).  I tried both (with and without GridViewLength) and got samething.  Is this the way you mentioned?

 

Thanks,

Nick

0
Milan
Telerik team
answered on 15 Jan 2010, 11:35 AM
Hi NickZ,

Sorry I forgot something - you have to set MinWidth to 0 as well.

column = new GridViewDataColumn();  
column.Header = "SectionID";  
column.UniqueName = "SectionID";  
column.DataMemberBinding = new Binding("SectionID");  
column.IsFilterable = false;  
column.IsReadOnly = true;  
column.MinWidth = 0;  // must be set before Width
column.Width = 0;  
  
nzViewCommitted.Columns.Add(column);


Kind regards,
Milan
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
Alex
Top achievements
Rank 1
answered on 09 Oct 2012, 10:45 PM
how to get Cell value by cell's index in client side?
so far I just know getCellByColumnUniqueName

thanks,
Alex
Tags
GridView
Asked by
NickZ
Top achievements
Rank 1
Answers by
Milan
Telerik team
NickZ
Top achievements
Rank 1
Casey
Top achievements
Rank 1
Alex
Top achievements
Rank 1
Share this question
or