0

So i have this gridview as shown below:

<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>

I was wondering is there any way I can access this dropdown list and populate it with data. I have tried the following codes, but none of them work, all returned error "object reference not set to an intstance":

      DropDownList drop =  gridDetaljiNarudzbe.FindControl("DropDownList1") as DropDownList;

Then I would do the following: drop.Items.Add(new ListItem("test"));

I have also tried with the RowDataBound event, but it also haven't worked...

DropDownList droplist = e.Row.FindControl("DropDownList1") as DropDownList;

then populating the grid with the following code just for sake of testing if it works:

drop.Items.Add(new ListItem("test"));

But none of these worked... I would also like to know how to get a value from this dropdown and insert it into the DB when someone picks up something from it. Can someone help me out with this please?

deostroll
  • 11,661
  • 21
  • 90
  • 161
perkes456
  • 1,163
  • 4
  • 25
  • 49

1 Answers1

2

This should do it

foreach (GridViewRow gr in gridDetaljiNarudzbe.Rows)
{
    DropDownList drop =  gr.FindControl("DropDownList1") as DropDownList;
    drop.Items.Add(new ListItem("test"));
}

or if you want to do it in RowDataBound event

protected void gridDetaljiNarudzbe_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList drop =  e.Row.FindControl("DropDownList1") as DropDownList;
        drop.Items.Add(new ListItem("test"));
    }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • Thank you it works!!! Do I repeat the same procedure when I want to pick up value from the dropdown list and insert it into the database? – perkes456 Nov 17 '14 at 11:22
  • Yes, you can use the first code to pick up the value from the dropdown list. – ekad Nov 17 '14 at 11:24
  • I'm trying now to pick up a value from a TextBox field, and when I try to get it using the same line of code it says: input text was not in correct format... Any ideas what could it be? – perkes456 Nov 17 '14 at 12:11
  • I believe that error is out of this question's context, but it's been asked before on SO, for example [here](http://stackoverflow.com/q/19049906/1905949) – ekad Nov 17 '14 at 12:20