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

Retrieving Data from selected row

3 Answers 57 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 29 Aug 2012, 01:32 PM
Hi Telerik,
I have a RadGrid, that I have set up to allow me to select rows by clicking on them
<telerik:RadGrid ID="RadGrid1" runat="server"
            AutoGenerateSelectColumn="True"
            CellSpacing="0" GridLines="Horizontal" AutoGenerateColumns="False" OnClientClick="RadGrid1_SelectedIndexChanged">
            <ClientSettings EnablePostBackOnRowClick="true">
            <Selecting AllowRowSelect="true" />
            </ClientSettings>
<MasterTableView>
 
<Columns>
                    <telerik:GridTemplateColumn AllowFiltering="false" UniqueName="TemplateEditColumn">
                        <ItemTemplate>
                            <asp:HyperLink ID="EditLink" runat="server" Text="Edit"></asp:HyperLink>
                        </ItemTemplate>
                        <FooterStyle Width="32px" />
                        <HeaderStyle Width="32px" />
                        <ItemStyle Width="32px" />
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="UniqueID" HeaderText="Unique ID" UniqueName="UniqueID"  FooterStyle-Width="1px" HeaderStyle-Width="1px" ItemStyle-Width="1px" Visible="false"/>
                    <telerik:GridBoundColumn DataField="Telephone" HeaderText="Telephone" UniqueName="Telephone"  FooterStyle-Width="1px" HeaderStyle-Width="1px" ItemStyle-Width="1px" Visible="false"/>
                    <telerik:GridBoundColumn DataField="StatusID" HeaderText="StatusID" UniqueName="StatusID"  FooterStyle-Width="1px" HeaderStyle-Width="1px" ItemStyle-Width="1px" Visible="false"/>
                    <telerik:GridBoundColumn DataField="Customer" HeaderText="Customer" UniqueName="Customer"  FooterStyle-Width="1px" HeaderStyle-Width="1px" ItemStyle-Width="1px" />
                    <telerik:GridBoundColumn DataField="From" HeaderText="From" UniqueName="From"  FooterStyle-Width="100px" HeaderStyle-Width="100px" ItemStyle-Width="100px" Visible="false"/>
                    <telerik:GridBoundColumn DataField="Subject" HeaderText="Subject" UniqueName="Subject"  FooterStyle-Width="100px" HeaderStyle-Width="100px" ItemStyle-Width="100px" />
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Taken By" UniqueName="Name"  FooterStyle-Width="100px" HeaderStyle-Width="100px" ItemStyle-Width="100px" />
                    <telerik:GridBoundColumn DataField="TakenOn" HeaderText="Taken On" UniqueName="TakenOn"  FooterStyle-Width="100px" HeaderStyle-Width="100px" ItemStyle-Width="100px" />
                    <telerik:GridBoundColumn DataField="Urgent" HeaderText="Urgent" UniqueName="Urgent"  FooterStyle-Width="1px" HeaderStyle-Width="1px" ItemStyle-Width="1px" />
                    <telerik:GridBoundColumn DataField="StatusNew" HeaderText="Status" UniqueName="StatusNew"  FooterStyle-Width="100px" HeaderStyle-Width="100px" ItemStyle-Width="100px" />
</Columns>
 
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
 
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="5px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="5px"></HeaderStyle>
</ExpandCollapseColumn>
 
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
        </telerik:RadGrid>

From here I would like to retrieve the uniqueID by using the allowrowselect feature
How would i retrieve the data in the uniqueID column, on my selected row. I'm working in VB, however an answer in c# is fine.

Your help is greatly appreciated!
Ryan


Also when i use

Dim item As GridDataItem = TryCast(RadGrid1.SelectedItems(0), GridDataItem)

I get an argument out of range exception
Saying my index was out of range. must be non-negative and less than the size of the collection

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 29 Aug 2012, 06:32 PM
Hello,

Please try with below code snippet.

In below demo i have used ID in-place of UniqueID
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateSelectColumn="True" CellSpacing="0"
            GridLines="Horizontal" AutoGenerateColumns="False" OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged"
            OnNeedDataSource="RadGrid1_NeedDataSource">
            <ClientSettings EnablePostBackOnRowClick="true">
                <Selecting AllowRowSelect="true" />
            </ClientSettings>
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" FooterStyle-Width="1px"
                        HeaderStyle-Width="1px" ItemStyle-Width="1px" Visible="false">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                </Columns>
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="5px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="5px"></HeaderStyle>
                </ExpandCollapseColumn>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
        </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
       {
           dynamic data = new[] {
             new { ID = 1, Name ="aaa"},
             new { ID = 2, Name = "bbb"},
             new { ID = 3, Name = "ccc"},
             new { ID = 4, Name = "ddd"},
              new { ID = 5, Name ="eee"}
           };
           RadGrid1.DataSource = data;
       }
 
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
       {
           // access your selected item here
 
           if (RadGrid1.SelectedItems.Count > 0)
           {
               foreach (GridDataItem item in RadGrid1.SelectedItems)
               {
                   string strId = item["ID"].Text;
               }
           }
       }


Thanks,
Jayesh Goyani
0
Ryan
Top achievements
Rank 1
answered on 30 Aug 2012, 08:30 AM
Hi Jayesh
Thanks for your help, it appears I did something wrong, however I'm not sure what.
But i copied your code and changed it to suit my own and now it works, I think it was somethign to do with the way I was accessing the datasource
Thanks
Ryan
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Aug 2012, 12:32 PM
Hello,

Are you binding your grid with Advancedatabinding / NeedDataSource event?

NOte : one of the reson to get this issue, before reach to selctedindexchanged event your grid binding again


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Ryan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Ryan
Top achievements
Rank 1
Share this question
or