string percentage = e.Row.Cells[7].Text;
I am trying to do some dynamic stuff with my GridView, so I have wired up some code to the RowDataBound ev
use RowDataBound
function to bind data with a perticular cell, and to get control use
(ASP Control Name like DropDownList) GridView.FindControl("Name of Control")
why not pull the data directly out of the data source.
DataBinder.Eval(e.Row.DataItem, "ColumnName")
The above are good suggestions, but you can get at the text value of a cell in a grid view without wrapping it in a literal or label control. You just have to know what event to wire up. In this case, use the DataBound event instead, like so:
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.Contains("sometext"))
{
e.Row.Cells[0].Font.Bold = true;
}
}
}
When running a debugger, you will see the text appear in this method.
Regarding the index selection style of the columns i suggest you do the following. i ran into this problem but i was going to set values dynamically using an API
so what i did was this :
keep in mind .CastTo<T>
is simply ((T)e.Row.DataItem)
and you have to call DataBind()
in order to see the changes in the grid. this way you wont run into issues if you decide to add a column to the grid.
protected void gvdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var number = e.Row.DataItem.CastTo<DataRowView>().Row["number"];
e.Row.DataItem.CastTo<DataRowView>().Row["ActivationDate"] = DateTime.Parse(userData.basic_info.creation_date).ToShortDateString();
e.Row.DataItem.CastTo<DataRowView>().Row["ExpirationDate"] = DateTime.Parse(userData.basic_info.nearest_exp_date).ToShortDateString();
e.Row.DataItem.CastTo<DataRowView>().Row["Remainder"] = Convert.ToDecimal(userData.basic_info.credit).ToStringWithSeparator();
e.Row.DataBind();
}
}
Just use a loop to check your cell in a gridview for example:
for (int i = 0; i < GridView2.Rows.Count; i++)
{
string vr;
vr = GridView2.Rows[i].Cells[4].Text; // here you go vr = the value of the cel
if (vr == "0") // you can check for anything
{
GridView2.Rows[i].Cells[4].Text = "Done";
// you can format this cell
}
}
<asp:TemplateField HeaderText="# Percentage click throughs">
<ItemTemplate>
<%# AddPercentClickThroughs(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "EmailSummary.pLinksClicked")), Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "NumberOfSends")))%>
</ItemTemplate>
</asp:TemplateField>
public string AddPercentClickThroughs(decimal NumberOfSends, decimal EmailSummary.pLinksClicked)
{
decimal OccupancyPercentage = 0;
if (TotalNoOfRooms != 0 && RoomsOccupied != 0)
{
OccupancyPercentage = (Convert.ToDecimal(NumberOfSends) / Convert.ToDecimal(EmailSummary.pLinksClicked) * 100);
}
return OccupancyPercentage.ToString("F");
}