Problems with dropdown list in template field in gridview

2.1k views Asked by At

Okay so I have a gridview as the following html shows:

 <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") %>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
              </ItemTemplate>
          </asp:TemplateField>
        </Columns>
    </asp:GridView>

For filling out the dropdown list for each record in DB I have used the following code (RowDataBound event):

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

For selecting a value from the dropdown list in each row:

   foreach (GridViewRow gr in gridDetaljiNarudzbe.Rows)
{
                        DropDownList drop = gr.FindControl("DropDownList1") as DropDownList;
                     // now selecting a value from dropdownlist 
                 int selectednumber = Convert.ToInt32(drop.Text);
}

Now my problem is, lets say we have two records in the grid, and I would like to pick up the value from the second dropdown list in 2nd row (lets suppose I did that). And when I press the button of the first record in first row, that value which I have picked up from 2nd dropdown list is now inserted into the DB as if I selected something from the first drop list.

Also I would like to know if its possible after that something has been picked up in drop list and inserted into the DB, to disable now that dropdownlist and turn into static text which would show just 5 instead of the dropdownlist?

Can someone help me out with this please? I've tried everything but I haven't succeeded yet. :/

EDIT:

When I select something from the second grid(2nd dropdownlist as u can see the picture) and when I press the button to grade the first product, I get the grade from the 2nd dropdown list which I have selected. Is it any more clear now?

Problem

Thanks!

Edit here is the code for yogi:

   List<hsp_Proizvodi_SprijeciDvaPutaOcijeniti_Result> lista = ServisnaKlasa.SprijeciDvostrukoOcjenjivanje(ProizvodID, Sesija.kupac.KupacID);
                foreach (GridViewRow gr in gridDetaljiNarudzbe.Rows)
                {
                    if (lista.Count == 0)
                    {
                        DropDownList drop = gr.FindControl("DropDownList1") as DropDownList;
                        if (drop.SelectedIndex != 0)
                        {
                            Ocjene o = new Ocjene();
                            o.KupacID = Sesija.kupac.KupacID;
                            o.ProizvodID = ProizvodID;
                            o.Datum = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
                            o.Ocjena = Convert.ToInt32(drop.Text);
                            ServisnaKlasa.OcjenjivanjeProizvoda(o);
                            string poruka = "Proizvod uspješno ocijenjen!";
                            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + poruka + "');", true);
                            drop.SelectedIndex = 0;

                        }
                    }
                    else
                    {
                        string poruka = "Ovaj proizvod ste već ocijenili!";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + poruka + "');", true);
                    }
                }
1

There are 1 answers

11
yogi970 On BEST ANSWER

Follow this approach

  1. add gridview's RowCommand event.
  2. Give a command argument for your link button.like this

    <asp:LinkButton ID="btnOcijeni" title="Ocijeni proizvod" CommandName="OcijeniCommand" CommandArgument='<%#Eval("ProizvodID") + ";" +((GridViewRow) Container).RowIndex%>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
    

(i just added one more field in command argument for rowindex)

  1. now where ever click on the link button the RowCommand event will be called.

    protected void gridDetaljiNarudzbe_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "OcijeniCommand") { arg = e.CommandArgument.ToString().Split(';'); int index = Convert.ToInt32(arg[1].ToString()); //Finding same row dropdown list //This will give you same row dropdownlist not the below row DropDownList drpdwn1= (DropDownList)gridDetaljiNarudzbe.Rows[index].FindControl("DropDownList1");
    //Now for BoundField values GridViewRow row = GridView1.Rows[index]; string NazivText= row.Cells[0].Text;//It will give Naziv's value //same way you can get other field values //And save into db. } }

Edit

For converting the drop down to label

  1. Add a label below your drop down list and make it Visible=false

     <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                    <asp:Label ID="lbl_ForDrpdwnVal" runat="server"  Visible="false"></asp:Label>               
    

  2. Now in the same item command event add the below code after inserting values to db.

                        //Access the new label
                        Label lblfordrpdwn= (Label)gridDetaljiNarudzbe.Rows[index].FindControl("lbl_ForDrpdwnVal");
                        lblfordrpdwn.Text= drpdwn1.SelectedItem.Text;//Setting value from dropdown to lbel
                        lblfordrpdwn.Visible=true;//Showing label
                        drpdwn1.Visible=false;//Hiding the dropdown