Combo Box in GridView

P

Peter

I have a DropDown Listbox in a GridView as a template field, and everything
is working fine with exception when I try to edit the GridView and the
gridview field data is not one of the values in the DropDown Listbox, I get
the following error.

[ArgumentOutOfRangeException: 'DropDownList2' has a SelectedValue which is
invalid because it does not exist in the list of items.

How can I have the inplace editing working in the GridView even when the
gridview data is not one of the values of the DropDown list box?


Thank You


Peter
 
E

Eliyahu Goldin

As the error says, a ddl can't be set in a value not included in the items.
What do you want achieve? Perhaps you need some other control.
 
V

Vince Xu [MSFT]

Hello peter,

Based on my understanding, you bound the "SelectedValue" property of
DropDownList by the datasource, and you encountered this error when you
clicked the "update" button in edit mode of GridView. Because the updated
GridView field data is not one of the values in DropDownList control.
Please feel free to let me know if I've misunderstood anything.
Could you please let me know how did you build the DropDownList? By static
ListItem or by datasource bound? If you bound DropDownList by static
ListItem, I suggest you bind it by a datasource. In this way, the new item
needs to be added in the datasource of DropDownList after you update the
GridView field so that the new item will be as one of the values of
DropDownList.

For example, we can create a DataSource control for DownDropList to bind
the same data table as GridView. After GridView field data is updated, the
related data table will be updated, which will impact the DropDownList.

Assuming that the following is the datasource of GridView:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [tbl]"
DeleteCommand="DELETE FROM [tbl] WHERE [id] = @id"
InsertCommand="INSERT INTO [tbl] ([link], [txt]) VALUES (@link,
@ txt)"
UpdateCommand="UPDATE [tbl] SET [link] = @link WHERE [id] = @id
">
<DeleteParameters>
<asp:parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="link" Type="String" />
<asp:parameter Name="txt" Type="String" />
<asp:parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="link" Type="String" />
<asp:parameter Name="txt" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

Assuming that the field "link" is the value field of the DropDownList , we
can create the SqlDataSource as below:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString
%>"
SelectCommand="SELECT DISTINCT [link] FROM [tbl]">
</asp:SqlDataSource>

Then, we can bind it on DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="link" DataValueField="link"
SelectedValue='<%# Bind("link") %>' >
</asp:DropDownList>

In this way, after you update the GridView field data which is associated
with "link" field, the table "tbt" will be updated. The items of
DropDownList will be updated.

If the above can't help you out. Please post some code snippet.


Sincerely,

Vince Xu

Microsoft Online Support

£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx

£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½
 
P

Peter

The problem is the DropDown List items come from one table and the dataview
rows come from another table.

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [link] FROM [tbl2]">
</asp:SqlDataSource

I can't use the same table because than the DropDown List will not contain
all of available items.

For example if I use State list:

If I use only one table and if tbl1 has one record and in that record the
state is 'CA' and we run the program the DropDown List box will contain
only 1 state instead of all of the states.

Also some other application can enter anything for the State like 'XX' or
'XY' so if the DropDown list is using tbl2 as it's data source it will not
have the 'XX' in the list - and that's when the error appears. I don't want
the error message, I want the user to be able to change it to anything in
the DropDown List box or leave it the way it is.


Thank You for your help!


Vince Xu said:
Hello peter,

Based on my understanding, you bound the "SelectedValue" property of
DropDownList by the datasource, and you encountered this error when you
clicked the "update" button in edit mode of GridView. Because the updated
GridView field data is not one of the values in DropDownList control.
Please feel free to let me know if I've misunderstood anything.
Could you please let me know how did you build the DropDownList? By static
ListItem or by datasource bound? If you bound DropDownList by static
ListItem, I suggest you bind it by a datasource. In this way, the new item
needs to be added in the datasource of DropDownList after you update the
GridView field so that the new item will be as one of the values of
DropDownList.

For example, we can create a DataSource control for DownDropList to bind
the same data table as GridView. After GridView field data is updated, the
related data table will be updated, which will impact the DropDownList.

Assuming that the following is the datasource of GridView:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [tbl]"
DeleteCommand="DELETE FROM [tbl] WHERE [id] = @id"
InsertCommand="INSERT INTO [tbl] ([link], [txt]) VALUES (@link,
@ txt)"
UpdateCommand="UPDATE [tbl] SET [link] = @link WHERE [id] = @id
">
<DeleteParameters>
<asp:parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="link" Type="String" />
<asp:parameter Name="txt" Type="String" />
<asp:parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="link" Type="String" />
<asp:parameter Name="txt" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

Assuming that the field "link" is the value field of the DropDownList ,
we
can create the SqlDataSource as below:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString
%>"
SelectCommand="SELECT DISTINCT [link] FROM [tbl]">
</asp:SqlDataSource>

Then, we can bind it on DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="link" DataValueField="link"
SelectedValue='<%# Bind("link") %>' >
</asp:DropDownList>

In this way, after you update the GridView field data which is associated
with "link" field, the table "tbt" will be updated. The items of
DropDownList will be updated.

If the above can't help you out. Please post some code snippet.


Sincerely,

Vince Xu

Microsoft Online Support

£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working
with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx

£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½
 
V

Vince Xu [MSFT]

Hello Peter,

Based on my understanding, you used two different data sources for
DropDownList and GridView. After the data is updated, the datasource of
DropDownList was not updated. So the error appears. You want to let user to
choose an available item again in this scenario. If I have misunderstood
you, please feel free to let me know.

To resolve this problem, I think there are two approaches we can use.


1. You can add a new item which the user typed into the datasource of
DropDownList in OnRowUpdated event of GridView. In this way, if the data is
not one of the values in DropDownList, the data will be added into tbl2 and
DropDownList will be updated.
protected void GridView1_RowUpdated(object sender,
GridViewUpdatedEventArgs e)
{
//Add a new item which the user typed into tbl2. You can use
e.NewValues[index] to get the data in related GridView field.
}

2.If you want the user choose another available item in DropDownList if the
value isn't one of the values in DropDownList control, rather than add the
new item which the user typed in GridView Field into the DropDownList, you
can add an additional item(say "Please select an item") in DropDownList so
that it can select this item when no items is matching.

When can use a method to bind on SelectedBalue property in this way:
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="true" DataSourceID="SqlDataSource2"
DataTextField="linkstr" DataValueField="linkstr" SelectedValue='<%#
bindstr(Eval("link").ToString()) %>' >
<asp:ListItem Value="null">=== Please selecte an
item ===</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString
%>"
SelectCommand="SELECT [linkstr] FROM [tbl2]">
</asp:SqlDataSource>

Code behind:
public string bindstr(string value)
{
//Check if the value is existing in tbl2.
if ("the value is existing in tbl2")
{
return value;
}
return "null";
}

Sincerely,

Vince Xu

Microsoft Online Support
 
S

snehal katkar

I want data like(party name from party table) in combo box

please tell me the code or solution



Peter wrote:

The problem is the DropDown List items come from one table and the dataview
12-Feb-09

The problem is the DropDown List items come from one table and the datavie
rows come from another table

<asp:SqlDataSource ID="SqlDataSource2" runat="server
ConnectionString="<%$ ConnectionStrings

Previous Posts In This Thread:

Combo Box in GridView
I have a DropDown Listbox in a GridView as a template field, and everythin
is working fine with exception when I try to edit the GridView and th
gridview field data is not one of the values in the D

As the error says, a ddl can't be set in a value not included in the items.
As the error says, a ddl cannot be set in a value not included in the items
What do you want achieve? Perhaps you need some other control

-
Eliyahu Goldin
Software Develope
Microsoft MVP [ASP.NE

Hello peter,Based on my understanding, you bound the "SelectedValue" property
Hello peter

Based on my understanding, you bound the "SelectedValue" property o
DropDownList by the datasource, and you encountered this error when yo
clicked the "update" button in edit mode of G

The problem is the DropDown List items come from one table and the dataview
The problem is the DropDown List items come from one table and the datavie
rows come from another table

<asp:SqlDataSource ID="SqlDataSource2" runat="server
ConnectionString="<%$ ConnectionStrings

Hello Peter,Based on my understanding, you used two different data sources for
Hello Peter

Based on my understanding, you used two different data sources fo
DropDownList and GridView. After the data is updated, the datasource o
DropDownList was not updated. So the error appe

EggHeadCafe - Software Developer Portal of Choice
ASP.NET DataGrid Paging at the Server
http://www.eggheadcafe.com/tutorial...6e-b400bd59855b/aspnet-datagrid-paging-a.aspx
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top