SPGridView SelectedRow.Cells[] returns empty value
-
29-09-2020 - |
Question
I have created an SPGridView control in my visual web part. Inside SPGridView, there is an SPHyperLink control that stores some URL. When accessing this value using SPGridView SelectedRow.Cells[3].Text, i am getting an empty value.
SPGridView Control
<SharePoint:SPGridView ID="ShowNode" runat="server" AutoGenerateColumns="false"
AllowSorting="true" OnRowDataBound="ShowNode_RowDataBound" OnSelectedIndexChanged="ShowNode_RowIndexChanged" OnSorting="ShowNode_Sort" OnRowCommand="ShowNode_RowClick">
<Columns>
<asp:ImageField HeaderText="Icon" DataImageUrlField="Img" HeaderStyle-HorizontalAlign="Left"></asp:ImageField>
<SharePoint:SPBoundField HeaderText="Name" DataField="Title" HeaderStyle-HorizontalAlign="Left" SortExpression="Name"/>
<SharePoint:SPBoundField HeaderText="Type" DataField="Type" HeaderStyle-HorizontalAlign="Left" SortExpression="Type" />
<asp:HyperLinkField DataTextField="Link" HeaderText="Link" Visible="false" HeaderStyle-HorizontalAlign="Left"/>
</Columns>
</SharePoint:SPGridView>
Row Click event
protected void ShowNode_RowIndexChanged(object sender, EventArgs e)
{
int ind = ShowNode.SelectedRow.RowIndex;
string link = ShowNode.SelectedRow.Cells[3].Text;
}
Solution 2
I was able to get the values using SPGridView DataKey attribute.
GridView Control
<SharePoint:SPGridView ID="ShowNode" runat="server" AutoGenerateColumns="false" AllowSorting="true" ShowFooter="true" ShowHeader="true" DataSourceID="" DataKeyNames="Link,Type" OnRowDataBound="ShowNode_RowDataBound" OnSelectedIndexChanged="ShowNode_RowIndexChanged" OnSorting="ShowNode_Sort" EnableViewState="true" >
<Columns>
<asp:ImageField HeaderText="Icon" DataImageUrlField="Img" HeaderStyle-HorizontalAlign="Left">
</asp:ImageField><SharePoint:SPBoundField HeaderText="Name" DataField="Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name"/>
<SharePoint:SPBoundField HeaderText="Type" DataField="Type" HeaderStyle-HorizontalAlign="Left" />
<asp:HyperLinkField DataTextField="Link" HeaderText="Link" Visible="false" HeaderStyle-HorizontalAlign="Left"/>
</Columns>
</SharePoint:SPGridView>
Code behind
string u= ShowNode.DataKeys[ShowNode.SelectedRow.RowIndex]["Link"].ToString();
string t = ShowNode.DataKeys[ShowNode.SelectedRow.RowIndex]["Type"].ToString();
OTHER TIPS
Try to remove "Visible=false" property from hyperlink field and then check, it should not return empty value then.
I am getting value on my side. Try to debug it and see in window "Shift+F9" Navigate there as this will help you to find value of a particular control.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange