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>
Related
It seems like this would be simple, but for the life of me, I can't figure out how it would work.
I have a gridview.
I have a standard button.
How do I use the button click to display the gridview?
Any suggestions?
This is a brief example from the MSDN page: (https://msdn.microsoft.com/en-us/library/bb907626.aspx)
You might to add an asp:TemplateField inside your GridView and through the CommandArgument property in the Button, set the current row index.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
And in your code, in the RowCommand event:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "AddToCart") {
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
Hope this help you.
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 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);
I have three fields being displayed in the gridview. Depending on the value of first field, I have to either display or hide the second two fields.
The following code is what I've tried so far but I don't know how to get the full solution.
Could anybody have a look please?
The three fields are
1) activeStatus
2)DateMadeInactive
3)Comments
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool activeStatus=Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem,"Active"));
if(activeStatus)
{
// I need to display the activeStatus columns
}
else
{
// I need to hide activeStatus Column and Display the DatemadeInactive and Comments
}
}
}
You can put conditionals into template fields:
<asp:GridView ... runat="server">
<Columns>
... your other fields ...
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label Text='<%# (bool)Eval("Active")
? Eval("activeStatus")
: Eval("DateMadeInactive", "Inactive since {0}") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
What I gather from your question is you want to know how to hide or display any given column in a DataGridView. If so, you just need to add the column to the DataGridView, either with a DataSource or manually, and then hide any columns you don't want by doing the following:
dataGridView1.Columns["YourColumnName"].Visible = false;
I did something like this using, IIRC, the CellFormatting event. It gives you an opportunity to test the value of each cell and replace it or other cells in the row based on that. In my case I was replacing numeric values with looked-up strings and altering background color.
Ok... I have a database table called employees..
This has columns called ID, Name, datejoined and Cannotbedeleted (check boxes)...
Now i want to add a delete column which deletes the rows when clicked.
But there are some entries which cannot be deleted if the Cannotbedeleted field is true (checked)... so the delete button should be invisible for these rows.
Please tell me a way of how to do this...
<asp:CheckBoxField DataField="CannotBeDeleted" HeaderText="CannotBeDeleted"
SortExpression="CannotBeDeleted" />
<asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded"
SortExpression="TimeAdded" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I also tried using in delete template field...
' >
But i did not know what to do in code behind??
protected bool GetShowDeleteButton()
{
}
The solution is below.... but is there a way i can refresh the page once i click the delete button in the gridview....
Try looping through the rows in GridView_DataBound, and hide the button for each row that has the checkbox checked.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach(GridViewRow myRow in GridView1.Rows)
{
//Find the checkbox
CheckBox ckbox1 = (CheckBox)myRow.FindControl("nameOfCheckBox");
if(ckbox1.Checked)
{
//Find the Delete linkbutton and hide it
LinkButton deleteButton = (LinkButton)myRow.FindControl("nameOfDeleteLinkButton");
deleteButton.Visible = false;
}
}
}
Now, here's the difference you need:
Implement the CheckBox column as a TemplateField with a CheckBox in it. You can bind the CheckBox to the data from your datasource.
Do it like this:
if($row['cannotbedeleted'] == true) {
$buttonDisplay = 'none';
}
echo "<button onclick='delete();' style='display: $buttonDisplay;'>Delete</button>";
Well that would be the solution in PHP but if the style of the button has "display: none;" in it the button will be hidden. :) HTH