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

CurrentCell in RadGridView errors out when not in visible view

1 Answer 173 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Xeeyee
Top achievements
Rank 1
Xeeyee asked on 09 Jun 2016, 08:14 PM
I have a radgridview in WinsForm. I created data entry number pad (buttons) for the user interface. When the table loads up, the current cell (focused cell) is set to the first cell in the upper left. When the user presses the number pad button, it enters the data to the currentcell's value. It works all fine and dandy until the table gets bigger and requires scrolling to see other cells. My problem is, when the user scrolls away from the currentcell and the currentcell is not in sight there will be an error if any button referencing the currentcell is pressed. It seems that when the currentcell is not in view, it cannot access the currentcell. Is there a way around this or resolve this?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Jun 2016, 09:17 AM
Hello ,

Thank you for writing. 

The RadGridView.CurrentCell property returns a GridDataCellElement. However, when the current cell is outside the visible area, this property will return null. Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. If you need to assign a new value to the current cell that is now visible, feel free to access the relevant GridViewCellInfo. Here is a sample code snippet:
public Form1()
{
    InitializeComponent();
 
    DataTable dt = new DataTable();
    for (int i = 0; i < 3; i++)
    {
        dt.Columns.Add("Col" + i);
    }
 
    for (int i = 0; i < 100; i++)
    {
        dt.Rows.Add("Data " + i + ".0", "Data " + i + ".1", "Data " + i + ".2");
    }
 
    this.radGridView1.DataSource = dt;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}
 
private void radButton1_Click(object sender, EventArgs e)
{
    if (this.radGridView1.CurrentRow != null && this.radGridView1.CurrentColumn != null)
    {
        this.radGridView1.CurrentRow.Cells[this.radGridView1.CurrentColumn.Name].Value = DateTime.Now.ToLongTimeString();
    }
}

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik
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
Xeeyee
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or