Gridview with DropDownList

R

rwiegel

I am trying to use a dropdownlist inside of a gridview. I want the
gridview to display some value as a label until the user clicks the
edit button for that row. Then I want to populate the dropdownlist
with some values from a table. I have tried a few different
approaches and have not been able to get anything to work. Can
someone tell me what the "best" approach is? I can then try it or
post the code I'm having trouble with.

Thanks

Ryan
 
G

Guest

R

rwiegel

OK....Thanks for the reply guys....Here are the problems I'm
having......I tried two things and neither work....I'll post twice so
its easier.....He is the first problem

Here is the gridview code:

<asp:GridView ID="custGrid" runat="server" AutoGenerateColumns="False"
BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" OnRowDataBound="RowDataBound" OnRowEditing="RowEdit"
OnRowCancelingEdit="RowCancel" OnRowUpdating="RowUpdate"
OnRowDeleting="RowDelete" Style="position: static">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:CommandField ButtonType="Button"
ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" />
<asp:BoundField DataField="Email" HeaderText="Email" / <asp:BoundField DataField="Password"
HeaderText="Password" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Company">
<EditItemTemplate>
<asp:DropDownList ID="comp_lst" runat="server"
Text='<%# Bind("Customer") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("Customer") %>'></asp:Label>
</ItemTemplate>
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066"
HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True"
ForeColor="White" />
</asp:GridView>

Then in the C# code I use this:

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList comp =
(DropDownList)e.Row.FindControl("comp_lst");
if (comp != null)
{
comp.Items.Add("test");
comp.SelectedValue = "test";
}
}
}

At this point I'm just trying to add one stupid test value into the
dropdownlist. When I view the page, originally the gridview shows up
fine. When I click the edit button I get this error: "Error loading
customers: 'comp_lst' has a SelectedValue which is invalid because it
does not exist in the list of items. Parameter name: value"
 
R

rwiegel

Here is the second method I tried:

GridView code:

<asp:GridView ID="custGrid" runat="server" AutoGenerateColumns="False"
BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" OnRowEditing="RowEdit" OnRowCancelingEdit="RowCancel"
OnRowUpdating="RowUpdate" OnRowDeleting="RowDelete" Style="position:
static">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:CommandField ButtonType="Button"
ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" />
<asp:BoundField DataField="Email" HeaderText="Email" / <asp:BoundField DataField="Password"
HeaderText="Password" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Company">
<EditItemTemplate>
<asp:DropDownList ID="comp_lst" runat="server"
Text='<%# Bind("Customer") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("Customer") %>'></asp:Label>
</ItemTemplate>
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066"
HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True"
ForeColor="White" />
</asp:GridView>

Here is the C# method:

protected void RowEdit(object sender, GridViewEditEventArgs e)
{
errorLabel.Text = "";
custGrid.EditIndex = e.NewEditIndex;
popGrid();

try
{


((DropDownList)custGrid.Rows[0].FindControl("comp_lst")).Items.Add("TEST");

}
catch (Exception exc)
{
errorLabel.Text = "Error: " + exc.Message + " " +
custGrid.Rows.Count.ToString();
}
finally
{
con.Close();
}

}

The error I get is "Error: Index was out of range. Must be non-
negative and less than the size of the collection. Parameter name:
index 0"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top