Reputation: 489
I have two tables in my database as following:
Suggestions Table: ID, Title, Description, StatusID... etc
SuggestionsStatus Table: ID, Status
I am using GridView to show the Suggestions and in the last column I put a DropDownList that for selecting the status for each Suggestion. Now, when I tried to show the Status in the DropDownList, I got the following error and I don't know why:
An object reference is required for the non-static field, method, or property 'System.Web.UI.WebControls.ListControl.Items.get'
My ASP.NET code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
width="950px" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2"
Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false"
DataTextField="Status" DataValueField="ID" AutoPostBack="true"
OnDataBound="DropDownList_DataBound">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username,
dbo.Divisions.DivisionShortcut
FROM dbo.employee INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode"
FilterExpression="[DivisionShortcut] like '{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
<%--For the DropDownList--%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>
My Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
}
int i = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = i.ToString();
i++;
}
}
protected void DropDownList_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList.Items.Insert(0, new ListItem("--Select--", ""));
}
}
So how I can fix this problem?
Upvotes: 0
Views: 1958
Reputation: 1243
This is all off the top of my head, so the code might not be perfect, but here's what you need to do:
In your RowDataBound, cast an instance of the DropDownList from the row.
DropDownList drp = (DropDownList)e.Row.Cells[7].FindControl("DropDownList");
Then call a databinding function like the one below
Bind your datalist
protected void bindDrop(DropDownList drp)
{
if (!IsPostBack)
{
drp.Items.Insert(0, new ListItem("--Select--", ""));
drp.DataBind()
}
}
That should do it
Upvotes: 2
Reputation: 8882
Based on the exception you seem to be getting you may have to cast the sender in your DropDownList_DataBound event to a DropDownList in order to access its Items collection:
protected void DropDownList_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
((DropDownList)sender).Items.Insert(0, new ListItem("--Select--", ""));
}
}
Upvotes: 3