|
|
|
start date: Tue, 21 Aug 2007 08:54:33 -0700,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
unknown
|
|
2
Braulio Diez
|
|
3
David Wier
|
|
4
unknown
|
|
5
unknown
|
Gridview with DropDownList
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
Date:Tue, 21 Aug 2007 08:54:33 -0700
Author:
|
RE: Gridview with DropDownList
Hello,
If you are using ASP .net 2.0, it's easy, create two item templates one for
edit and the other for editing, in the other hand and the dropdown in the
editing one, about how this works:
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=169
http://www.bdiez.com/tips/GridView_1.htm
Another thing a bit more difficult is to use cascading combos.
Good Luck
Braulio
/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
"rwiegel@iastate.edu" wrote:
> 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
>
>
Date:Tue, 21 Aug 2007 09:06:02 -0700
Author:
|
Re: Gridview with DropDownList
Here's a code sample from ASPNet101.com:
http://www.aspnet101.com/aspnet101/aspnet/codesample.aspx?code=EditDDL_GV
David Wier
http://aspnet101.com
http://iWritePro.com
wrote in message
news:1187711673.030132.10920@x40g2000prg.googlegroups.com...
>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
>
Date:Tue, 21 Aug 2007 11:40:50 -0500
Author:
|
Re: Gridview with DropDownList
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>
</asp:TemplateField>
<asp:BoundField DataField="Phone" HeaderText="Phone" /
>
</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"
Date:Thu, 23 Aug 2007 08:08:45 -0700
Author:
|
Re: Gridview with DropDownList
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>
</asp:TemplateField>
<asp:BoundField DataField="Phone" HeaderText="Phone" /
>
</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"
Date:Thu, 23 Aug 2007 08:13:36 -0700
Author:
|
|
|