Hiding dropdownlist in templatefield in gridview asp.net C# after insert into the database

2k views Asked by At

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"));
            }
2

There are 2 answers

1
Manish Goswami On BEST ANSWER

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;

            //}

        }
    }
}
7
Neeraj On

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