1

I have a problem, I would like after the value from the dropdown list is picked up, inserted into the db I would like it to hide the dropdown list and just show the grade of the product that the user rated, as on the two following pictures:

This is the first picture showing how the user needs to insert the grade into the db of the product:

enter image description here

The result after should be as following:

enter image description here

The dropdownlist should now be invisible to the user which rated the product. I have tried using the RowDataBound event and the following code:

  if (e.Row.RowType == DataControlRowType.DataRow)
            {
        hsp_Narudzbe_Detalji_Result k = (hsp_Narudzbe_Detalji_Result)e.Row.DataItem;
                if (k.Ocjena!=null)
                {
                    e.Row.Cells[4].Text = k.ocjena;
                }
            }

But it doesn't works, it shows the grade just once, and when I press the button for grading the product, the dropdown list is back... :/

Can someone help me out with this?

Edit (aspx code of the page):

<asp:GridView ID="gridDetaljiNarudzbe" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" runat="server" OnRowCommand="gridDetaljiNarudzbe_RowCommand" OnPageIndexChanging="gridDetaljiNarudzbe_PageIndexChanging" OnRowDataBound="gridDetaljiNarudzbe_RowDataBound">
        <Columns>
           <asp:BoundField DataField="Naziv" HeaderText="Naziv" />
           <asp:BoundField DataField="Sifra" HeaderText="Šifra" />
           <asp:BoundField DataField="Cijena" HeaderText="Cijena" />
           <asp:BoundField DataField="Kolicina" HeaderText="Količina" />
                <asp:TemplateField HeaderText="Ocjena">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
          <asp:TemplateField>
              <ItemTemplate> 
                   <asp:LinkButton ID="btnOcijeni" title="Ocijeni proizvod" CommandName="OcijeniCommand" CommandArgument='<%#Eval("ProizvodID") + ";" +((GridViewRow) Container).RowIndex%>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
              </ItemTemplate>
          </asp:TemplateField>
        </Columns>
    </asp:GridView>

The grades are loaded like this:

 if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList drop = e.Row.FindControl("DropDownList1") as DropDownList;
                drop.Items.Add(new ListItem(""));
                drop.Items.Add(new ListItem("1"));
                drop.Items.Add(new ListItem("2"));
                drop.Items.Add(new ListItem("3"));
                drop.Items.Add(new ListItem("4"));
                drop.Items.Add(new ListItem("5"));
            }
perkes456
  • 1,163
  • 4
  • 25
  • 49

2 Answers2

2

Try something like this,

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl = e.Row.Cells[4].FindControl("DropDownList2") as DropDownList;
        if (ddl != null)
        {
            // if (your_condition == true)
            //{
                    ddl .Visible = false;

            //}

        }
    }
}
Manish Goswami
  • 863
  • 10
  • 27
  • Unfortunately I don't think thats going to work, as I don't load the grade (from 1 to 5) from DB itself, I actually load them from rowdatabound event like in the example I shown in the first post that I now edited. Can you see it in the first post? – perkes456 Nov 19 '14 at 13:00
2

Hi Make your item template like below :

<ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                 <asp:Label ID="gvlblddlVal" runat="server" Text='<%#((YourEntityClassName)Container.DataItem).ddlVal %>'></asp:Label>
            </ItemTemplate>

After that

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
    DropDownList ddl = e.Row.Cells[4].FindControl("DropDownList2") as DropDownList;
    Label lblddl = e.Row.Cells[4].FindControl("gvlblddlVal") as Label;
    if (!string.IsNullOrEmpty(lblddl.Text))
    {            
                ddl.Visible = false;
                lblddl.Visible = true;           
    }
    else
    {
                ddl.Visible = true;
                lblddl.Visible =false; 
     }
  }
}

Hope it helps

Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49
  • Edit: It doesn't shows my grid now at all when I have no grades?? What could it be ? – perkes456 Nov 19 '14 at 13:17
  • Please explain your question,Is your grid is not visible or cell content is not visible when you use edit template. – Neeraj Dubey Nov 19 '14 at 13:21
  • Yes when I edited the template as u'v shown me and entered the code u posted grid is not visible now at all... This happens if the products aren't graded yet, when they are graded, its visible... – perkes456 Nov 19 '14 at 13:21
  • What are the steps performed by which grid is invisible. – Neeraj Dubey Nov 19 '14 at 13:23
  • Well I had grades for all products entered into the db, and it was showing fine just the label as desired, I deleted the grades from db to test if the dropdownlist will show, and thats when the whole grid dissapeared... – perkes456 Nov 19 '14 at 13:24
  • Have you use try catch block.If not please use and see the error.I think label is not handle the scenario if value is blank. – Neeraj Dubey Nov 19 '14 at 13:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65213/discussion-between-perkes456-and-neeraj-dubey). – perkes456 Nov 19 '14 at 13:28