I have the following:
<Columns>
...
<asp:BoundField DataField="datetime_added" HeaderText="When Added" ReadOnly="True" SortExpression="datetime_added" />
...
</Columns>
The DataField datetime_added comes from a SQL Data Source. I want to do some editing of every value for this column after the values are queried from the SQL Data Source on the C# side. Kind of like:
//short pseudo code example
protected void someEvent(object sender, EventArgs e)
{
sender.GetField["datetime_added"] = "test";
}
Is there a way to do this?
use a templatefield instead of a boundfield
- with a label
- the label text is fed by a function throught something like this:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text='<%= SomeFunction(Eval("db_id"),Eval("description")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And this in your code behind:
protected string SomeFunction(object id, object description)
{
//your code
return "";
}
When your function is called, the field from SQL is passed as an object
so that you can use it if you want or return something else based on whatever you want.
The function is called when the value is bound from the datasource in each row, you'll probably need to pass an id as well to identify the row.
Have you tried the "OnRowCreated" event? I'm assuming here that you're using a GridView, and if not, then there may be something similar in whatever component it is you're binding the data to.
For GridView, here is an example:
protected void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// do what you need in here
// e.Row.Cells[x] where x = the column number
}
}
Also, this method can be really slow depending on how many rows you're processing.
If you are using RowDataBound event to manipulate the values before it's being bound to the grid (getting called while binding every row to the gridview)
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string datetimeadded = DataBinder.Eval(e.Row.DataItem, "datetime_added").ToString();
// or you can use the cell index to reference the boundfield like below
string datetimeadded = e.Row.Cells[1].Text;
}
}
Related
I have been trying to figure this out for hours. So sad! The structure of my ascx is like
<asp:DetailsView ...>
<Fields>
.
.
<asp:CheckBoxField DataField="ThingEnabled" HeaderText="Thing Enabled"/>
.
.
.
</Fields>
</asp:DetailsView>
and the element I want is the ThingEnabled one.
Setup:
DetailsView dv = (DetailsView)sender;
CheckBoxField cbf = ????
Note that CheckBoxFields do not have ID properties, so I can't use FindControl.
Note that DetailsView uses cells on it instead of control IDs for each row it has, hence you can get CheckBoxField value using row, cell and control position like this:
// Page_Load is just an example event here, change to any event you need
protected void Page_Load(object sender, EventArgs e)
{
DetailsView dv = sender as DetailsView;
// the checkbox uses checked state as its value to be passed
// n = row/cell/control indexes where CheckBoxField has bound into, starting from 0
// e.g. dv.Rows[0].Cells[0].Controls[0] as CheckBox
bool checkboxvalue = (dv.Rows[n].Cells[n].Controls[n] as CheckBox).Checked;
}
If you still want using FindControl, use ItemTemplate wrapper element and create a CheckBox control on it (note that you may need to use a BoundField with DataField="ThingEnabled" on top of TemplateField for data binding):
<asp:DetailsView runat="server" ...>
<Fields>
...
<asp:TemplateField HeaderText="Thing Enabled">
<ItemTemplate>
<asp:CheckBox ID="ThingEnabled" runat="server" Checked="<%# Bind("ThingEnabled") %>">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Then you can access that checkbox control using FindControl:
DetailsView dv = sender as DetailsView;
// n = row/cell indexes, starting from 0
bool checkboxvalue = (dv.Rows[n].Cells[n].FindControl("ThingEnabled") as CheckBox).Checked;
References/similar issues:
How to get value from DetailsView Control in ASP.NET?
Get the value of a BoundField from a DetailsView
Accessing DetailsView check box value
Its also possible to use linq to get any field within a DetailsView in this way.
// Page_Load is just an example event here, change to any event you need
protected void Page_Load(object sender, EventArgs e)
{
DetailsView dv = sender as DetailsView;
//using linq to get the CheckBoxField control
var field = dv.Fields.Cast<DataControlField>().Single(x => x.HeaderText == "TitleOfCheckboxField") as CheckBoxField;
}
I have a gridview which pulls data from an xml file. One of the columns of the gridview is a hyperlinkfield. I want to bind a URL field contained in my XML file to this column. I think I have the right idea with the code below but cant figure out how to finish it. The URL is the datakey of the gridview by the way.
protected void grdContents_RowCreated(object sender, GridViewRowEventArgs e)
{
((HyperLinkField)grdContents.Columns[1]).NavigateUrl =
}
you can bind hyperlink on gridView_RowDataBound event like this
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
HyperLinkField lnkHyper = (HyperLinkField)e.Row.FindControl("HyperLinkField1");
lnkHyper.NavigateUrl="";
}
}
try this.
or you can also bind url using DataBinder.Eval at the time of binding source to grid like
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl="<%# DataBinder.Eval(Container.DataItem, "url") %>"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can use this as well, as you also providing data source to Grid view.
I have a column in my gridview which displays the total cost of certain items which is pulled from my database table. However, my users also have a currency information attached to their account. What I would like to do is convert this column based on the individual user's currency info. I have tried the following:
<asp:HiddenField id="currencyconvfactor" runat="server" />
<asp:TemplateField HeaderText="Total" SortExpression="Total">
<ItemTemplate>
<%# currencyconvfactor.Value %> //this is just a test. see below for the issue.
</ItemTemplate>
</asp:TemplateField>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
currencyconvfactor.Value = 12345
//i simplified retrieving the specific factor value here as I do some database commands to pull the specific value
}
However, I notice that the template field column is always empty. Does that mean that the gridviews are generated before the page load event? If so, how could I perform the column conversions on the first page load? More specifically, how can I access the conversion factor to perform the conversion in time?
You may want to look at using a RowDataBound event to access the column and do whatever computation you need:
.aspx:
<Columns>
<asp:TemplateField HeaderText="Template Field">
<ItemTemplate>
<asp:Label ID="lblConvValue" Runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
Code-behind:
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the current row's data
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Do your conversion
var conv = int.Parse(rowView["valueToConvert"].ToString());
var converted = conv * 12345; // Whatever conversion you want.
// Set the value of the control in the ItemTemplate
Label lblConvValue= (Label)e.Row.FindControl("lblConvValue");
lblConvValue.Text = converted.ToString();
}
}
If all rows use the same Currency conversion factor, you can do the following.
...
<ItemTemplate>
<%#GetCurrencyconvfactor()%>
</ItemTemplate>
...
In the code behind define a method to return the value
public string GetCurrencyconvfactor()
{
//you can also pass in custID as an argument if each row uses different factor
return "123"; // or get this from the user profile/settings
}
After defining the values, call GridView.DataBind()
I have a user control named ActivityGrid which takes a list of object as a parameter in it's constructor.
public ActivityGrid(List<clsActivityRow> ActivityData)
{
bindData(ActivityData);
}
I need to bind this user control in a gridView called parentGrid, so I used Templatefield.
<asp:GridView ID="GridViewParent" runat="server" AutoGenerateColumns ="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<uc1:ActivityGrid ID="ActivityGrid1" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How do I pass the list of object in this user control while it is
bound inside a gridview?
I have the list of object List<clsActivityRow> ActivityData ready in the code behind.
You need to find the ActivityGrid1 object in every row of GridViewParent in its RowDataBound event and assign datasource.
protected void GridViewParent_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ActivityGrid ActivityGrid1= (ActivityGrid )e.Row.FindControl("ActivityGrid1");
if (ActivityGrid1 != null)
{
ActivityGrid1.DataSource = SomeMethodToReturnDataSource();
ActivityGrid1.DataBind();
}
}
}
or if your control has grid view i.e ActivityGrid1 has gridView1 then you can find gridView1 in ActivityGrid1
protected void GridViewParent_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ActivityGrid ActivityGrid1= (ActivityGrid )e.Row.FindControl("ActivityGrid1");
if (ActivityGrid1 != null)
{
GridView gridView1 = (ActivityGrid )ActivityGrid1 .FindControl("gridView1");
gridView1.DataSource = SomeMethodToReturnDataSource();
gridView1.DataBind();
}
}
}
You can do this by creating a public Property in your UserControl and In this public property set block you can call bindData method.
Pass the object List ActiveData for this usercontrol, in GridView RowDataBound Event.
I have a gridview with the following columns:
NAME|AGE|Birthday|Code
Joh 21 12.12.2 Yes/No
Currently the column code is a textbox. How can I transform it into a dropdownlist with 2 values: Yes/No so if I press edit I can choose in that cell the Value Yes or No.
Also How can I check on edit event too see if value is yes?
You can create a template field like this:
<columns>
<asp:TemplateField HeaderText="code">
<ItemTemplate>
<asp:DropDownList ID ="ddlCode" runat="server" AppendDataBoundItems="true" CssClass="DropDn1" />
</ItemTemplate>
</asp:TemplateField>
</columns>
In your rowdatabound event of the grid you will bind it if you want to bind from the database.
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList code= (DropDownList)e.Row.FindControl("ddlCode") as DropDownList;
if (code!= null)
{
//Bind the dropdownlist
}
}
To retrieve the value from the dropdown in your edit event you will do this:
string code = (row.FindControl("ddlCode") as DropDownList).Text);