Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
315
Need help selecting/activating first record in grid
posted
Hi, I'm currently having problems trying to select the first record in the grid and set it as the active record. Please help.

// Set the data source
xdgField.DataSource = MyDataSource;

// Select the first record
if (xdgField.Records.Count>0)
{
    DataRecord firstRecord = (DataRecord)xdgField.Records[0];
    firstRecord.IsSelected = true;
    xdgField.ActiveRecord = firstRecord;
}

As shown in the code, the first time this is executed, the records count is always 0. I've tried other things such as setting the DataSource to null before setting it to the new datasource (and even to set the same datasource twice), but the count is still 0 on its first run. Please help. Thanks!

[edit] I've also tried other methods:
firstRecord = xdgField.RecordManager.Sorted[0];
firstRecord = (DataRecord)xdgField.ViewableRecords[0];
but none of them work because the first time they're called, the records count = 0
  • 8576
    Offline posted
    Hi -
     
    The reason this does not work is because the Records are created 'lazily' when they are asked for by the UI.
     
    The best way to select the first record is to listen to the InitializeRecord event and select the record there.  Something like this:
     

    public Window1()

    {

    InitializeComponent();

    this.xamDataGrid1.InitializeRecord += new EventHandler<Infragistics.Windows.DataPresenter..Events.InitializeRecordEventArgs>(xamDataGrid1_InitializeRecord);

    }

    void xamDataGrid1_InitializeRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e)

    {

    // Check member variable to see if we have selected the first record yet.

    if (this._firstRecordSelected == false)

    {

    e.Record.IsSelected = true;

    this._firstRecordSelected = true;

    }

    }

     
    Joe
     
    "jsjslim" wrote in message news:23254@forums.infragistics.com...
    Hi, I'm currently having problems trying to select the first record in the grid and set it as the active record. Please help.
    
    // Set the data source
    xdgField.DataSource = MyDataSource;
    
    // Select the first record
    if (xdgField.Records.Count>0)
    {
        DataRecord firstRecord = (DataRecord)xdgField.Records[0];
        firstRecord.IsSelected = true;
        xdgField.ActiveRecord = firstRecord;
    }
    
    
    As shown in the code, the first time this is executed, the records count is always 0. I've tried other things such as setting the DataSource to null before setting it to the new datasource (and even to set the same datasource twice), but the count is still 0 on its first run. Please help. Thanks!

    [edit] I've also tried other methods:
    firstRecord = xdgField.RecordManager.Sorted[0];
    firstRecord = (DataRecord)xdgField.ViewableRecords[0];
    
    but none of them work because the first time they're called, the records count = 0

    http://forums.infragistics.com/forums/p/5099/23254.aspx#23254