I am using Gridview.In this i have 2 columns i.e. department and emailID.In this gridview department is bind from database and showed in linkbutton in gridview.
I want to do that when i clicked on thst dept linkbutton it make visible the column emailID.
How can i do this?Plaese guide me..
Thanks in advance.
Here is My Grid:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDept" runat="server" Text='<%#Bind("Department")%>' OnClick="lnkbtnTitle_Click" ></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="50%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="pnlN24" runat="server" Visible="false">
<asp:Label ID="lblTotal" runat="server" Width="30" Text="abc"></asp:Label>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to handle the events
of GridView
control especially RowCommand
.
public class Demo
{
public string Dept { get; set; }
public string Email { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Demo> list = new List<Demo>()
{
new Demo() { Dept="A", Email="a@a.com" },
new Demo() { Dept="B", Email="b@b.com" },
};
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmd")
{
GridViewRow row = (e.CommandSource as LinkButton).NamingContainer as GridViewRow;
Label email = row.Cells[1].FindControl("email") as Label;
email.Visible = true;
}
}
Understanding that you will make visible all the cells in the column for all the rows, you can handle the OnClick event of the dept button. For example.
<asp:LinkButton ID="lnkDept" OnClick="LinkButton_Click" runat="server" Text="Department" />
Code behind:
protected void LinkButton_Click(Object sender, EventArgs e)
{
gridView1.Columns[1].Visible=true;
}
Where gridView.Column[1]
is your email column.
its not possible to make cell visible when column's visibility property set to false. so for showing only adjacent cell visible .
you can use the only one column of type template field and then make a table in that template field and set the td of email to visible false and on row command of grid as the previous answers making that lable visible you should make your td visible .
protected void lnkbtnTitle_Click(object sender, EventArgs e)
{
GridViewRow gvrow = ((LinkButton)sender).Parent.Parent as GridViewRow;
Panel pnlN24 = (Panel)gvrow.FindControl("pnlN24");
pnlN24.Visible = true;
}
It is very simple , the above code will work.
We have to find in which row the LinkButton is Clicked , which we can get from the
the following code. Once you get the GridViewRow then find the control in that row and make it Visible
GridViewRow gvrow = ((LinkButton)sender).Parent.Parent as GridViewRow;
As we know that
Control <--- Cell <-- GridViewRow <-- GridView
For Eg:
LinkButton <--- Cell <-- GridViewRow <-- GridView
GridViewRow is Parent to Cell and Cell is Parent to the Control inside the Cell (Panel)
function validateColors(id) {
var grid = document.getElementById("GridView1");
var label = grid.rows[id].cells[6].children[2];
grid.rows[id].cells[6].children[2].style.visibility = "visible";
grid.rows[id].cells[6].children[2].style.visibility = "hidden";
}