Is it possible to have a Checkbox that only shows up when Editing the last row of a GridView?
I have tried something like this in the EditItemTemplate:
<asp:CheckBox ID="chkNextDay" runat="server"
ToolTip="Is it a next-day departure?"
Enabled="true"
Checked='<%# DateTime.Parse(Eval("OutHour","{0:d}")).Date >
DateTime.Parse(Eval("InHour","{0:d}")).Date %>'/>
Then on code-behind I tried hiding it for rows other than the last one like this:
protected void grvOutHour_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView grvOutHour = (GridView)this.grvReport.Rows[grvReport.EditIndex].FindControl("grvOutHour");
TextBox txtBox = (TextBox)grvOutHour.Rows[e.NewEditIndex].FindControl("txtEditOutHour");
CheckBox nextDay = (CheckBox)grvOutHour.Rows[e.NewEditIndex].FindControl("chkNextDay");
if (grvOutHour.Rows.Count-1 != e.NewEditIndex)
nextDay.Visible = false;
}
This ALMOST worked, but the checkbox kept showing for all fields, I think because the RowDataBound is called AFTER RowEditing again so it renders the whole thing again :(
Any suggestions?
Thanks,
EtonB.
Use RowDataBound instead...
protected void grvOutHour_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
GridView grid = (GridView)sender;
CheckBox nextDay = (CheckBox)e.Row.FindControl("chkNextDay");
nextDay.Visible = (e.Row.RowIndex == (grid.Rows.Count - 1));
}
}
You will need to handle hiding the checkbox in the RowDataBound event.
You'll need to determine what the last row is, and set the checkboxes visible property to true when that condition is true, obviously.
I guess it's more of a hack than an elegant solution, but I would probably just hide the other checkboxes via JavaScript if the condition is true.
Related
I've a GridView inside a Panel that I want to hide when the child is empty because at the moment remains a fieldset with the legend text and nothing inside.
I've already tried to put something like Panel.Visible = GridView.Rows.Count > 0 in the Page_Load event but it doesn't works well.
How can I obtain the result that I need?
Thank you
Some more details:
If on first load the database table is empty I don't see the fieldset, when I add a row the Panel with the GridView doesn't appears; if on first load I have a row I can see the Panel with the GridView, when I delete the unique row anything disapears but never come back even if I insert a new row. I think that the Page_Load is not the right event.
try this..
Panel.Visible = (GridView.Rows.Count > 0?false:true);
Please try following steps:
Check for Panel.Visible = GridView.Rows.Count > 0 in a late page event such as Page_PreRender
Add OnRowDeleted event in GridView and check for Panel.Visible = GridView.Rows.Count > 0 if the row deleting is a last row. Also rebind the GridView as GridView.DataBind()
Add OnRowCreated event in GridView and repeat the same process as you did in case of row deletion.
Code:
protected void Page_PreRender(object sender, EventArgs e)
{
Panel.Visible = GridView.Rows.Count > 0;
}
protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
{
GridView.DataBind();
Panel.Visible = GridView.Rows.Count > 0;
}
protected void GridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
GridView.DataBind();
Panel.Visible = GridView.Rows.Count > 0;
}
I hope this will solve your issue.
In the end I found my problem...
I've put the FormView for the insert and the GridView in different UpdatePanels, so unified all in the same UpdatePanel and used this code:
Panel.Visible = GridView.Rows.Count > 0;
in the GridView DataBound event and it worked.
Thank you everyone.
If you simply want to make your panel visible/invisible based on the Gridview Rows, then its as simple as this :
if(GridView.Rows.Count > 0)
PanelId.Visible = true;
else
PanelId.Visible = false;
But make sure you do this code after you have called the Gridview binding function.
Hope this helps.
I wonder how to edit a the visibility of a LinkButton within a GridViewEdit:
I have got a LinkButton named "lbtnActivateConfig" in the ItemTemplate of a GridView in my aspx-File:
<ItemTemplate>
<asp:LinkButton ID="lbtnActivateConfig" runat="server"
OnClick="GridViewDeactivate" Visible="false">Deaktivieren
</asp:LinkButton>
</ItemTemplate>
Now I want to change the visibility of the LinkButton insite this method:
protected void GridViewEdit(object sender, GridViewEditEventArgs e)
{
GridViewRow row = this.ConfigGridView.Rows[e.NewEditIndex];
LinkButton buttonActivate = (LinkButton)ConfigGridView.Rows[row.RowIndex].FindControl("lbtnActivateConfig");
buttonActivate.Visible = true;
}
While debugging it catches the LinkButton an seems to set it's visibility. But the LinkButton is still invisible.
It's the same if I change the button to visible inside the aspx-File and try to change it in the method.
For me it seems that the aspx-File is always executed at last and overwrites the changes in the method. Is this right? How can I change the visibility inside the method? Any ideas?
Thank you and Goodbye!
Try the following:
protected void GridViewEdit(object sender, GridViewEditEventArgs e)
{
GridViewRow row = this.ConfigGridView.Rows[e.NewEditIndex];
(ConfigGridView.Rows[row.RowIndex].FindControl("lbtnActivateConfig")).Visible = true;
}
You don't need to cast it as LinkButton because all controls have the Visible property. And when you set FindControl to a variable, the variable is set by value rather than by reference which means you aren't referring to the actual control.
I have a GridView containing a TextBox in <asp:TemplateField /> and The GridView is residing inside an AJAX Update Panel.
I want to register the TextChanged Event for the textbox inside the GridView but only for the first row inside the Grid.
Is there a way to do it?
I tried binding the OnTextChangedEvent and AutoPostBack = true for TextBox, but it is firing for textbox in each row. How can I limit that TextChanged Event to only the TextBox in first row in the GridView.
Can you please help me.
Thanks and appreciate your feedback.
Perhaps you can use GridView_RowDataBound event and instead of attaching event to TextBox in aspx markup, do it via code behind:
protected void view_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
{
TextBox txtBox = (TextBox)e.Row.FindControl("txtBoxId");
txtBox.TextChanged += new EventHandler(txtBox_TextChanged);
}
}
}
how to find a command field control in the gridview.
in a method not in the row data bound.
so far i have used this coding but i cant find the control.
<asp:CommandField ButtonType="Image" ShowEditButton="True
HeaderText="Enter Leave"
EditImageUrl="~/IMAGES/edit-icon.gif">
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
source code:
ImageButton edit = (ImageButton)EmployeeDetails.FindControl("Image");
edit.Enabled = false;
You can disable column itself with,
GridView1.AutoGenerateEditButton = false;
from code behind pages.
Or you can use ItemTemplate instead of CommandField,
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
And at code behind you can iterate through rows of GridView and disable each LinkButton.
foreach(GridViewRow gvr in GridView1.Rows)
{
LinkButton row = gvr.FindControl("id") as LinkButton;
row.Enabled = false;
}
First Edit :
I tried my second solution and it works. However, make sure your GridView is filled before you use foreach. Otherwise, GridView.Rows.Count would probably be 0.
Second Edit :
This works for CommandField too. Replace 0 with the location of CommandField in your GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Enabled = false;
}
}
You miss to specify the row
Something like :
ImageButton edit = (ImageButton)EmployeeDetails.Rows[0].Cells[0].FindControl("Image");
edit.Enabled = false;
If you want to disable the column that contains the imageButton , you can do :
EmployeeDetails.Columns[0].Visible = false;
Try this:
try to hide controls at DataBound or RowDataBound event of GridView
protected void EmployeeDetails_DataBound(object sender, EventArgs e)
{
ImageButton edit = (ImageButton)EmployeeDetails.Row.Cells[0].FindControl("Image");
edit.Visible = false;
edit.Enabled = false; //OR use this line
}
particular column can be disabled in the following way
EmployeeDetails.Columns[0].Visible = false;
Hope this helps.
I had a similar issue. I simply disabled the view of the Column in BindData() function.
GridView1.Columns[0].Visible = false;
This worked for me, since my first column was Edit column and I have to enable it for specific users only.
Good luck!
Cast it as a DataControlFieldCell and then set Enabled to false.
Where: row.Controls[0] is your CommandField control
foreach (GridViewRow row in ManageDNXGridView.Rows)
{
DataControlFieldCell editable = (DataControlFieldCell)row.Controls[0];
editable.Enabled = false;
}
I am creating a gridView that allows adding new rows by adding the controls necessary for the insert into the FooterTemplate, but when the ObjectDataSource has no records, I add a dummy row as the FooterTemplate is only displayed when there is data.
How can I hide this dummy row? I have tried setting e.row.visible = false on RowDataBound but the row is still visible.
You could handle the gridview's databound event and hide the dummy row. (Don't forget to assign the event property in the aspx code):
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 1)
GridView1.Rows[0].Visible = false;
}
Please try the following
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView1.Rows[0].Visible = false;
}
I think this is what you need:
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="headertext">
<ItemTemplate>
itemtext
</ItemTemplate>
<FooterTemplate>
insert controls
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the codebehind:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["style"] = "display:none";
}
}
But I do not understand why you are adding your "insert controls" to the footer instead of placing them below the grid.
Maybe try:
e.Row.Height = Unit.Pixel(0);
This isnt the right answer but it might work in the meantime until you get the right answer.
Maybe use CSS to set display none?!
This is the incorrect usage of the GridView control. The GridView control has a special InsertRow which is where your controls should go.
GridView has a special property to access Footer Row, named "FooterRow"
Then, you cold try yourGrid.FooterRow.Visible = false;
I did this on a previous job, but since you can add rows, I always had it visible in the footer row. To make it so that the grid shows up, I bound an empty row of the type that is normally bound
dim row as Datarow = table.NewRow()
table.AddRow(row)
gridView.DataSource = table
gridView.Databind()
then it has all the columns and then you need. You can access the footer by pulling this:
'this will get the footer no matter how many rows there are in the grid.
Dim footer as Control = gridView.Controls(0).Controls(gridView.Controls(0).Controls.Count -1)
then to access any of the controls in the footer you would go and do a:
Dim cntl as Control = footer.FindControl(<Insert Control Name Here>)
I'd assume you'd be able to do a:
footer.Visible = false
to make the footer row invisible.
I hope this helps!
Edit I just figured out what you said. I basically delete the row when I add a new one, but to do this you need to check to see if there are any other rows, and if there are, check to see if there are values in it.
To delete the dummy row do something like this:
If mTable.Rows.Count = 1 AndAlso mTable.Rows(0)(<first column to check for null value>) Is DBNull.Value AndAlso mTable.Rows(0)(<second column>) Is DBNull.Value AndAlso mTable.Rows(0)(<thrid column>) Is DBNull.Value Then
mTable.Rows.Remove(mTable.Rows(0))
End If
mTable.Rows.Add(row)
gridView.Datasource = mTable
gridView.Databind()
Why are you not using the EmptyDataTemplate? It seems to work great even though I have only been using it for a couple days...
You should use DataKeyNames in your GridView:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID">
And then retrieve it on your code:
GridView1.DataKeys[0].Value.ToString()
Where "0" is the number of the row you want to get the "FooID"
To make it visible, just use:
Gridview.Rows.Item(i).Attributes.Add("style", "display:block")
And to make it invisible
Gridview.Rows.Item(i).Attributes.Add("style", "display:none")
If you do not want to display data when the column/row is null:
if (!String.IsNullOrEmpty(item.DataName))
{
e.Row.Visible = false;
}
It can easily be done by SQL
USE YourdatabaseName select * from TableName where Column_Name <> ''