2

I have a Datagrid which looks as under

<asp:GridView ID="dgTask" runat="server" Width="100%" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="TaskID" HeaderText="TaskID" ItemStyle-Width="1%" />
        <asp:BoundField DataField="TaskName" HeaderText="Task Name" ItemStyle-HorizontalAlign="left"
            ItemStyle-Width="10%" />
        <asp:BoundField DataField="PriorityName" HeaderText="Priority" ItemStyle-HorizontalAlign="center"
            ItemStyle-Width="10%" />
        <asp:BoundField DataField="StatusName" HeaderText="Status" ItemStyle-HorizontalAlign="center"
            ItemStyle-Width="10%" />
        <asp:TemplateField HeaderText="Edit Task" ItemStyle-Width="10%">
            <ItemTemplate>
                <asp:LinkButton ID="lnkBtnEdit" runat="Server" Text="Edit" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

As can be figure out that, there is a link button control inside the grid. Our requirement is that, when the user double click on this link button, then for that row it should change to a dropdown list.

How to do so? Googling does not help much.

Please help

Data Population in grid

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var records = GetData();
        dgTask.DataSource = records;
        dgTask.DataBind();
    }

    private List<TaskEntity> GetData()
    {
        List<TaskEntity> lstTaskEntity = new List<TaskEntity>();
        Enumerable.Range(1, 10)
            .ToList()
            .ForEach(
                        i => lstTaskEntity.Add(new TaskEntity
                        {
                            TaskID = i
                         ,
                            TaskName = string.Concat("TaskName", i)
                         ,
                            PriorityName = string.Concat("PriorityName", i)
                         ,
                            StatusName = string.Concat("StatusName", i)
                        }));
        return lstTaskEntity;
    }
}

public class TaskEntity
{
    public int TaskID { get; set; }
    public string TaskName { get; set; }  
    public string PriorityName { get; set; }  
    public string StatusName { get; set; }   
}

Thanks in advance

2
  • I am not the downvoter but i was with you until double click on this link button
    – V4Vendetta
    Jan 3, 2012 at 10:23
  • Hi but that is the requirement which we need to do..since i am unable to proceed so i am seeking for help Jan 3, 2012 at 10:26

3 Answers 3

2
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function Test(object) {

            var splitComponents = object.split(",");
            var ddlId = splitComponents[0];
            var lnlBtnId = splitComponents[1];

            document.getElementById(ddlId).style.display = "inline";
            document.getElementById(lnlBtnId).style.display = "none";

            return false;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="dgTask" runat="server" Width="100%" 
            AutoGenerateColumns="False" onrowdatabound="dgTask_RowDataBound" 
            >
            <Columns>
                <asp:BoundField DataField="TaskID" HeaderText="TaskID" ItemStyle-Width="1%" />
                <asp:BoundField DataField="TaskName" HeaderText="Task Name" ItemStyle-HorizontalAlign="left"
                    ItemStyle-Width="10%" />
                <asp:BoundField DataField="PriorityName" HeaderText="Priority" ItemStyle-HorizontalAlign="center"
                    ItemStyle-Width="10%" />
                <asp:BoundField DataField="StatusName" HeaderText="Status" ItemStyle-HorizontalAlign="center"
                    ItemStyle-Width="10%" />
                <asp:TemplateField HeaderText="Edit Task" ItemStyle-Width="10%">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkBtnEdit" runat="Server" Text="Edit" CommandArgument ='<%# Eval("TaskID") %>' OnClick="Button2_Click"/> 
                        <asp:DropDownList ID = "ddlDropDown" runat="server"></asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

RowDataBound

protected void dgTask_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = (LinkButton)e.Row.Cells[4].FindControl("lnkBtnEdit");

            DropDownList ddl = (DropDownList)e.Row.Cells[4].FindControl("ddlDropDown");
            ddl.DataSource = //give datasource;
            ddl.DataBind();

            ddl.Attributes.Add("style", "display:none");
            btn.Attributes.Add("onclick", "return Test('" + ddl.ClientID + "," + btn.ClientID + "');");
        }
    }
0

Refer following link ,You can use TemplateField along with EditItem template to achieve this http://www.asp.net/web-forms/tutorials/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs

0

Try This:

For this u have to add LinkButton and DropDownList to itemtemplate and control visibility of both

   protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton b = (LinkButton)sender;
        b.Visible = false;
        GridViewRow r = (GridViewRow)b.NamingContainer;
        ((DropDownList)(GridView1.Rows[r.RowIndex].Cells[0].FindControl("DropDownList1"))).Visible = true;
    }

Cell[0]----------->Here change the cell number where your controls are

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.