Conditionally Showing DropDown List in DataGrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • msrahul
    New Member
    • Feb 2008
    • 6

    Conditionally Showing DropDown List in DataGrid

    HI ALL
    Plz Help Me.
    I have a datagrid with a few colums. One column is conaining 1's or 0's.
    Its like a flag column. It either has 0 or 1. Now what i need is that if any row has 1 in this column than another column named "Status" (of that row) should have a dropdown list conatining 4 items.
    This column can have values Checked,NotChec ked,InProcess,P ending in the rows for which the flag column has 0's. But when the Flag value is 1 then this column should have a drop down list in that paricular cell (with Checked,NotChec ked,InProcess,P ending as the items)
    plz note That I DONT WANT ANY EDIT or SELECT BUTTON IN ANY ROW.
    I mean the dropdown should appear automatically and not by pressing of any button..
    ITS URGENT. PLZ HELP ANYBODY
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by msrahul
    HI ALL
    Plz Help Me.
    I have a datagrid with a few colums. One column is conaining 1's or 0's.
    Its like a flag column. It either has 0 or 1. Now what i need is that if any row has 1 in this column than another column named "Status" (of that row) should have a dropdown list conatining 4 items.
    This column can have values Checked,NotChec ked,InProcess,P ending in the rows for which the flag column has 0's. But when the Flag value is 1 then this column should have a drop down list in that paricular cell (with Checked,NotChec ked,InProcess,P ending as the items)
    plz note That I DONT WANT ANY EDIT or SELECT BUTTON IN ANY ROW.
    I mean the dropdown should appear automatically and not by pressing of any button..
    ITS URGENT. PLZ HELP ANYBODY
    You are going to need to loop through the rows of your datagrid and determine whether you need to show a label or a drop down list in the Status column. Here is how to do it.

    Code:
     for(int i = 0; i < datagrid.Items.Count; i++) 
    {
    ddl = (DropDownList)datagrid.Items[i].FindControl("ddlStatus");
    ddl.DataSource = dataset;
    ddl.DataTextField = "status_description";
    ddl.DataValueField = "status_description_id";
    ddl.DataBind();
     
    lbl = (Label)datagrid.Items[i].FindControl("lblStatus");
    lbl.Visible = true;
     
    if(datagrid.Items[i].Cells[3].Text == "1") 
    //3 would be the column number of the Status column
    {
    lbl.Visible = false;
    }
    else
    {
    ddl.Visible = false;
    }
    }
    Hopefully this helps you. If you have another specific question after trying this code let me know.

    Nathan
    Last edited by nateraaaa; Mar 4 '08, 03:18 PM. Reason: Added code tags around code example

    Comment

    • msrahul
      New Member
      • Feb 2008
      • 6

      #3
      Thank you so much for replying...
      I was able to do it in some what the same way....


      <ItemTemplate >
      <asp:Label ID="lblWithDrp " runat="server" Visible = <%#Convert.ToIn t32(DataBinder. Eval(Container. DataItem,"Hidde nColumn"))==0%> Text ="LABEL"> </asp:Label>
      <asp:DropDownLi st ID="drpInGv" runat="server" Visible = <%#Convert.ToIn t32(DataBinder. Eval(Container. DataItem,"Hidde nColumn"))==1%>

      </asp:DropDownLis t>
      </ItemTemplate>

      Comment

      Working...