I read some of the others threads and didnt work out for me =\
I have a GridView
with a DropDownList
in one field. I'd like to know How may I set a DataSource
for that ? I'm not using Templates neither ItemTemplate or EditItemTemplate I don't know how it work exactly , so i'm not using it yet.
So far I have only created the GridView And filled the fields with data but I don't know how to do the same for the DropDownList
. SOmething is missing I guess, it is giving me an error ("The Reference of the Object was not set as an instance of an object"
)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList Drop_Prioridades = (DropDownList)e.Row.FindControl("Drop_Prioridades");
Drop_Prioridades.DataTextField = "BAIXA";
Drop_Prioridades.DataValueField = "1";
Drop_Prioridades.DataTextField = "MEDIA";
Drop_Prioridades.DataValueField = "2";
Drop_Prioridades.DataTextField = "ALTA";
Drop_Prioridades.DataValueField = "3";
Drop_Prioridades.DataBind();
}
I Also tried this / Same error =\
DataSet ds = func.LoadPriority();
foreach (DataRow row in ds.Tables[0].Rows)
{
ListItem item = new ListItem();
item.Text = row["prioridade"].ToString();
item.Value = row["id"].ToString();
DropDownList ddlPrioridades = (DropDownList)e.Row.FindControl("Drop_Prioridades");
ddlPrioridades.Items.Add(item);
}
And Tried this too...
HTML:
<columns>
<asp:TemplateField HeaderText="PRIORIDADE" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:DropDownList ID="Drop_Prioridades" Width="120px" runat="server" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Code Behind:
DataSet ds = func.CarregaPrioridade();
DropDownList ddlist = (DropDownList)e.Row.FindControl("Drop_Prioridades");
ddlist.DataSource = ds;
ddlist.DataTextField = "prioridade";
ddlist.DataValueField = "id";
Bind Row data bound event in the markup, as below:
<asp:GridView ID="grvGrid" runat="server" OnRowDataBound="grvGrid_RowBound">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Width="7%">
<ItemTemplate>
<asp:DropDownList ID="ddlList" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView?
In the code behind:
protected void grvGrid_RowBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddlList= (DropDownList )e.Row.FindControl("ddlList");
ddlList.DataSource = _dSource;
ddlList.DataTextField = "text";
ddlList.DataValueField = "value";
ddlList.DataBind();
}
OR
If you dropdown is going to have the same options for each row, you dont need to bind it during RowDataBound Event.. You can add item s to the dropdown list in markup as below:
<asp:DropDownList id="ddlList"runat="server">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
</asp:DropDownList>
ddlList.DataTextField = "Text";
If this line gives you an error, then make sure that in your datasource or dataset you have same column name.
if its textname, then you should assign ddlList.DataTextField = "textname";
Just a thought!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With