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;
}
Related
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;
}
}
I am using GridView in asp.net like this:
mygrid.DataSource = dTable;
mygrid.DataBind();
if (mygrid.Columns.Count > 1)
{
mygrid.Columns[2].Visible = false;
}
my grid view code is as follows
<asp:GridView ID="mygrid" runat="server" AllowPaging="True"
onpageindexchanging="mygrid_PageIndexChanging" PageSize="15"
PersistedSelection="true"
ondatabound="mygrid_DataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("Value", "~/myweppage.aspx?Id=M{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings PageButtonCount="4" />
</asp:GridView>
Here I am not able to set visible=false.
I tried with the following answer
How do I make several gridview columns invisible dynamically?
I am not finding datarow event in Visual Studio 2010. Can anyone help me to set the column visible property?
my Column structure of data table is
column[0] is Value column then 4 other columns are there.
my Column structure of Grid view is
column[0] is link field
column1 is Value field from Dtable
4 other columns
This is perfect solution for dynamically generated columns in gridview
Please try this :
int indexOfColumn = 1; //Note : Index will start with 0 so set this value accordingly
protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells.Count > indexOfColumn)
{
e.Row.Cells[indexOfColumn].Visible = false;
}
}
For .aspx page edit gridview tag as follow :
<asp:GridView ID="mygrid" runat="server" AllowPaging="True"
onpageindexchanging="mygrid_PageIndexChanging" PageSize="15"
PersistedSelection="true"
ondatabound="mygrid_DataBound"
OnRowDataBound="mygrid_RowDataBound">
Here is the simple answer. Create css as below
.classHide{ display:none }
then instead of column.visible = false, just assign classHide CSS class to the column.
e.g.
grdRole.Columns(0).ItemStyle.CssClass = "classHide"
grdRole.Columns(0).HeaderStyle.CssClass = "classHide"
*strong text*Try to make use of the event ItemDataBound event and try the following syntax to hide the column dynamically:
mygrid.Columns[1].Visible = false //(Example)
Column count for a datatable starts from 0 not from 1 . so if it is the second column , you want to hide, index should be 1.
Hope this helps..
right Click on gridview and select Properties then select Events you will find there RowDataBound Double Click on it and in Row data bound write this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
Try this:
for (int i = 0; i < YourGrid.Rows.Count; i++)
{
(YourGrid.Columns[2]).Visible = false;
}
I have a GridView that draws fields from a dataset:
protected void Page_Load(object sender, EventArgs e)
{
var dat = SecurityManager.GetAllGroups();
GridView1.DataSource = dat;
GridView1.DataBind();
}
One of these is the IsAdministrator property.
It should show up as a checkbox.
It does, but it is grayed out. I want it to be enabled so I can modify it.
From there, I will go through each row and update accordingly.
Is this possible to do? How can I ungray the checkbox?
Thanks
If you are using the CheckBoxField, you can only change the value when in edit mode. To workaround this, if you want a checkbox for all rows, use the TemplateField, and supply a checkbox in the item template like:
<asp:TemplateField ..>
<ItemTemplate>
<asp:CheckBox .. Checked='<%# Bind("IsAdministrator") %>' />
</ItemTemplate>
</asp:TemplateField>
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 two GridView in ASP.net 3.5 page. I have HyperLink field as one of the fields in the First GridView.
On click of this hyperlink I need to call display the 2nd grid by passing some values to a method showAllRecords(value from hyperlink)
How do I do this?
Thanks
You can try a TemplateField like this for GridView1 (primary GridView)
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="LinkButton1" CommandName="cmdName" CommandArgument='<%# Eval("IdColumn") %>' > LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and in GridView1's RowCommand, you can get the CommandArgument and setup the DataSource for GridView2 (child GridView).
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName = "cmdName")
{
var arg = e.CommandArgument;
// use arg to filter GridView2's DataSource
GridView2.DataSource = FilteredDataSource;
GridView2.DataBind();
// show GridView2 if it's hidden.
}
}
Maybe the following blog post could give you a hint;
http://www.tugberkugurlu.com/archive/parent-child-view-in-a-single-table-with-gridview-control-on-asp-net-web-forms
First you need to handle SelectedIndexChanged event on the first grid and then get the value from the hyperlink. Is the hyperlink a DataKey? If it is then you get it by GridOne.SelectedDataKey.Values["key"] otherwise get the actual cell by valuefromGridOne = GridOne.SelectedRow.Cells[num].Text where number is the cell number. Once you have it you can pass the value to your second grid by handling Selecting event of the objectDataSource (assuming you use it to bind data) and passing the value like this e.InputParameters["dataKey"] = valuefromGridOne;