Reputation: 2583
I am trying to prepend some text to what's already in a GridView
cell in the RowDataBound
event:
protected void gvPatientSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
if (Convert.ToBoolean(dr["Confidential"]))
{
String name = String.Copy(e.Row.Cells[1].Text);
//the problem is the following line
e.Row.Cells[1].Text = "<b><font color='red'>!</font></b>" + name;
}
}
}
For some reason it seems that no matter what I do the original text is deleted. When I remove the line below the comment the name displays normally without the exclamation point; when I add the append code, for some reason the original text becomes a blank string and I only get the exclamation point. I have tried this with and without the String.Copy
- I didn't think this would be needed but figured I would try it just in case. No luck.
I don't think this is relevant, but the first column in the row is a hidden column that contains a 1 or a 0. If the value is 1, I want to add the exclamation point. The data is there and this part is being called correctly, I just can't get the name to stay put.
Any ideas why this would be happening?
EDIT: Front end code below
<asp:GridView ID="gvPatientSearch" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True"
PageSize="25" OnPageIndexChanging="gvPatientSearch_PageIndexChanging" OnSorting="gvPatientSearch_Sorting" OnRowDataBound="gvPatientSearch_RowDataBound"
AutoGenerateColumns="false" PagerSettings-Position="TopAndBottom" PagerStyle-HorizontalAlign="Right" PagerSettings-Mode="NumericFirstLast" PagerSettings-PageButtonCount="5"
PagerSettings-FirstPageText=" First " PagerSettings-LastPageText=" Last " PagerSettings-PreviousPageText=" Previous " PagerSettings-NextPageText=" Next ">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:BoundField HeaderText="Confidential" DataField="Confidential" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol"></asp:BoundField>
<asp:HyperLinkField DataTextField="PatientName" DataNavigateUrlFields="PatientIdInternal" DataNavigateUrlFormatString="PatientProfile.aspx?InternalID={0}" HeaderText="Patient Name"
SortExpression="PatientName" />
<asp:BoundField DataField="PatientID" HeaderText="Patient ID" SortExpression="PatientID" />
<asp:BoundField DataField="AccountNumber" HeaderText="Account Number" SortExpression="AccountNumber" />
<asp:BoundField DataField="DateOfBirth" HeaderText="Date Of Birth" SortExpression="DateOfBirth" DataFormatString="{0:d}" />
<asp:BoundField DataField="PracticeName" HeaderText="Practice Name" SortExpression="PracticeName" />
<asp:BoundField DataField="PatientIdInternal" HeaderText="Internal ID" SortExpression="PatientIdInternal" Visible="false" />
</Columns>
</asp:GridView>
I can see the values of all the other fields showing up correctly in Visual Studio at this part of the code, only the name field that I am trying to modify shows as an empty string.
Upvotes: 0
Views: 48
Reputation: 21672
A HyperLinkField
is rendered as a Hyperlink control, and for this reason you cannot retrieve the cell's text directly. If you want the text, you must retrieve it from the control, not the cell:
HyperLink myLink = e.Row.Cells[1].Controls[0] as HyperLink;
String name = myLink.text;
An alternative would be to grab the name directly from the datasource by doing DataBinder.Eval(e.Row.DataItem, "name")
in your code-behind.
Upvotes: 1
Reputation: 2583
I went ahead and did it by changing the offending line to this:
e.Row.Cells[1].Text = "<b>font color='red'>!</font></b> " + Convert.ToString(dr["PatientName"]);
Still don't know why the other way doesn't work, if anyone else does and answers I will mark it correct.
Upvotes: 0
Reputation: 1542
I don't know why that's happening, but I think you can work around it by applying you logic with a function like this:
public string Exclaim(bool confidential, string originalText)
{
if (confidential) {
return "<b><font color='red'>!</font></b>" + originalText;
} else {
return originalText;
}
}
In your gridview, invoke the function using:
<asp:TemplateField>
<ItemTemplate>
<%# Exclaim(Eval("Confidential"), Eval("Name"))%>
</ItemTemplate>
</asp:TemplateField>
I think that would work.
Upvotes: 0