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

Selecting first row in grid on load

1 Answer 1005 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 06 Jan 2012, 05:57 PM
I have a grid which I bind to a DataTable in page_load.  I want to programatically select the first row in the grid after I bind the data to the grid - the data binding is done server side by populating a DataTable.  My grid is defined as :-

<telerik:RadGrid ID="gridParts" runat="server" AllowSorting="True" CellSpacing="0" Height="690px" OnSelectedIndexChanged="gridParts_SelectedIndexChanged" AllowMultiRowSelection="False">
<ClientSettings>
<Selecting AllowRowSelect="True" />
</ClientSettings>
<MasterTableView AutoGenerateColumns="true" TableLayout="Fixed" DataKeyNames="PartNumber" AllowCustomSorting="false" AllowMultiColumnSorting="false" AllowNaturalSort="false" Alloo>
</MasterTableView>
<ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
<Resizing AllowColumnResize="true" ResizeGridOnColumnResize="false" AllowResizeToFit="true" ClipCellContentOnResize="true" EnableRealTimeResize="true" />
<Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True" />
</ClientSettings>
</telerik:RadGrid>

Note that I have enabled selection but disabled multi-row selection.  In my C# I do the following

// Data source for parts grid
gridParts.DataSource = partsTable;

// set initial sorting on the Part Number
GridSortExpression sort = new GridSortExpression();
sort.SortOrder = GridSortOrder.Ascending;
sort.FieldName = "PartNumber";
gridParts.MasterTableView.SortExpressions.AddSortExpression(sort);

// Select the first part (if any)
if (gridParts.Items.Count != 0)
gridParts.Items[0].Selected = true;

What happens now is odd - I don't see any visible sign that the first part has been selected however the grid is in an odd state.  In my gridParts_SelectedIndexChanged handler I have the following code :-

protected void gridParts_SelectedIndexChanged(object sender, EventArgs e)
{
if (gridParts.SelectedItems != null && gridParts.SelectedItems.Count > 0)
{
GridDataItem dataItem = gridParts.SelectedItems[0] as GridDataItem;
string partNumber = (string)dataItem.GetDataKeyValue("PartNumber");
DWPart part = DWPart.FindByName(partNumber);
DisplayComponents(part.ID);
}
}

What is odd is that the partNumber ALWAYS is the first item in the grid, even when I have scrolled down and selected another item.  Simple removing the line which sets the initial selection fixes the problem and the grid selection works correctly - buit I want to always have an item selected as the selected item causes a second grid to be populated.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Jan 2012, 07:00 AM
Hello,

Try the following code snippet.
CS:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       RadGrid1.MasterTableView.Items[0].Selected = true;
   }

Thanks,
Princy.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or