?
Solved

How to hide dropdownlist item inside a Datagridview

Posted on 2009-02-23
6
Medium Priority
?
947 Views
Last Modified: 2013-11-19
Hi guys,
I have a web application with a DataGridView. I have the DataGridView bounded to a SQl Server 2005 Database. One of my columns is a DropDownList.
I want to be able to show/hide items in the dropdownlists based on the role of the user.
My problem right now is how do I get to identify and access my DropDownList so I can apply the condition of show/hide the item(s).

Can anybody help me with this?

Thanks,

Al
0
Comment
Question by:agRegalado
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:Allamz
ID: 23711933
Hello AI,

is this dropdown in the DataGridView ?

Regards,

Mohamed Allam
Senior Solution Developer
0
 

Author Comment

by:agRegalado
ID: 23712045
Yes Mohamed, the dropdownlist is in one of the columns of my datagridview, every row show the dropdownlist when editing, besides the last row (I use it to add an item to grid), shows the dropdownlist all the time.
The dropdown basically shows the user roles but nobody except the administrator can assign all the possible roles, so I want to delete/hide some roles if the user is not administrator.

Thanks,

Al
0
 
LVL 6

Expert Comment

by:Allamz
ID: 23712069
ai,

I've added a sample below on how to access the items in the datagridview
please let me know if you need further assistance.

Regards,

Mohamed Allam
Senior Solution Developer
private void dgSearch_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
   
oreach(DataGridItem i in dgSearch.Items)
{
Label lblStatus=(Label)(i.FindControl("Label1"));
if(lblStatus.Text!="New")
{
HyperLink hypid=(HyperLink)(i.FindControl("Hyperlink1"));
hypid.Enabled=false;
}
} 
 
LinkButton lblDelete=(LinkButton)e.Item.FindControl("lblDelete"); 
if(e.Item.FindControl("Label1")!=null)
{
Label lbl=(Label)e.Item.FindControl("Label1");
if(lbl.Text=="New")
{ 
lbl.ForeColor=Color.Blue; 
lblDelete.Enabled =true;
 
// for(int i=0;i<=5;i++)
// {
// e.Item.Cells[i].BackColor=Color.White;
// }
 
}
if(lbl.Text=="Recommended")
{
lblDelete.Enabled =false;
lbl.ForeColor=Color.DarkOrange;
// for(int i=0;i<=5;i++)
// {
// e.Item.Cells[i].BackColor=Color.Yellow;
// }
}
if((lbl.Text=="Rejected"))
{
lblDelete.Enabled=false;
lbl.ForeColor =Color.Red;
// for(int i=0;i<=5;i++)
// {
// e.Item.Cells[i].BackColor=Color.Red;
// }
}
if(lbl.Text=="Approved")
{
lblDelete.Enabled =false;
lbl.ForeColor =Color.Green;
// for(int i=0;i<=5;i++)
// {
// e.Item.Cells[i].BackColor=Color.LightSeaGreen;
// e.Item.Cells[i].Enabled=false;
// }
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
LinkButton Link = (LinkButton)e.Item.FindControl("lblDelete");
if(Link.Enabled)
{
if (Link != null)
{
Link.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record ?')");
}
}
}
}
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
Allamz earned 1500 total points
ID: 23712108
0
 

Author Comment

by:agRegalado
ID: 23712200
Mohamed, thanks for your prompt response. Are you using a Datagridview or some other control? I cannot find an ItemDataBound event for my DatagridView. The one I am trying to use is RowDataBound. Is this the same thing?

0
 

Author Closing Comment

by:agRegalado
ID: 31550098
Thanks very much Mohamed,
I was able to identify the dropdowlist. I needed to place code in two different events though, but it is doing what I wanted. I am giving you a code snippet of what I did.

Thanks very much,
Al
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
if (e.Row.RowType == DataControlRowType.Footer)
            {
                GridViewRow gvr = GridView1.FooterRow;
                DropDownList ddlFTRole = e.Row.FindControl("ddlFTRole") as DropDownList;
                ddlFTRole.Items[0].Enabled = false;

            }

}
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (GridView1.EditIndex == e.Row.RowIndex)
            {
                DropDownList ddlRole=e.Row.FindControl("ddlRole") as DropDownList;
                ddlRole.Items[0].Enabled = false;
            }
        }
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question