German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Embed or Bind a DropDownList in a Gridview

Written by omerkamal on Dec 28, 2006
This code will explain how can we use DropDownlist in a Gridview

Introducation:

Some time there are situations when we need to restrict our user to update the database. For this embiding a Dropdownlist box inside a gridview is a best solution.

Explanation:

 

Let us see how does our Gridview in  .aspx Page look like.

Our .aspx Page; 

 

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns=false
OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton=true />
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="message" HeaderText="message" SortExpression="message" />
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("username")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"
DataSourceID="SqlDataSource1" DataValueField=username AutoPostBack="True" SelectedValue='<%# Bind("username")%>'>
<asp:ListItem Value="">Select an Item</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

 Our Dropdownlist is embeded in the "ItemTemplete" of the Gridview. One more thing before going next, Always use Bind() with the Controles in both ItemTemplate and EditItemTemplate. For this Demo we are using SqlDataSource Object as DataSource. Now let us see how does "SqlDataSource" looke like.

 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PersonalLocal %>"
SelectCommand="SELECT * FROM [ChatMessages]"
UpdateCommand="UPDATE [ChatMessages] SET [username] = @username, [message] = @message WHERE id=@id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="message" Type="String" />
<asp:Parameter Name="online" Type="Boolean" />
<asp:Parameter Name="id" Type="Int64" />
<asp:Parameter Name="username" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>

You noticed that in the Gridview Code we defined an event called "RowUpdating" In the code behind we will show the event code. Now, let us see how does the code behind look like.


Code behind for the Row Update event;

 

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string struName = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue;
e.NewValues["username"] = struName;
e.Cancel = false;

}

Visitors/Readers Comments
(for questions please use The Forum)



Kip

I understand the code yes but simply do not understand your intetion. Did you honestly explain any thing to the newbies

 

30/12/2006 03:27:47 UTC

omerkamal

Hi kip,

Please, help us  improving.

30/12/2006 16:39:52 UTC

Rahuk
Can u  tel me how to do multiplication n gridview.which has 3 column named price(cming from
database)qty(user input textbox) and total(last column which display multiplication). when page get load all field cming come frm database if user want to select  or buy more no of item then he change qty and click on update button(which is below the gridview not autogenerate button we add this manually)then price and qty get multiplied and result displayed in last column(total)
Can anybody help me plzzz to solve this problem. i m new in asp.net.
waiting for u r answerr..

Thanksssssssss in advance..........

02/02/2007 23:35:05 UTC

Chinni

Omerkamal,

I had a GridView and there is a dropdownlist in the GridView. Here is my problem when i was clicking on the EDIT button it is displaying an error.

Parameter name: value 'ddlConfiguration' has a SelectedValue which is invalid because it does not exist in the list of items.
Can u plz help me in this?

<asp:TemplateField HeaderText="Configuration">
<edititemtemplate>
<asp:DropDownList ID="ddlConfiguration" runat="server" DataSourceID="ConfigNameDS"
DataTextField="fldConfigurationName" DataValueField="fldConfigurationID"
SelectedValue='<%# Bind("fldConfigurationName") %>'
AppendDataBoundItems="True" AutoPostBack="true">
</asp:DropDownList>
</edititemtemplate>
<ItemTemplate>
<asp:Label ID="lblConfiguration" runat="server" Text='<%#Bind("fldConfigurationName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

11/07/2007 15:38:08 UTC

kamal

That means that the value which is suppoed to be selcted does not exsit in the ConfigNameDS Database or you are accessing different Fields of the Database.

The Values must lie in both the databases. The Database you attaching to the Gridview and the one you attaching to the DropDownList. Moreover, your Configuration Field must represent the same field which you bouding to the DropDownList. 

12/07/2007 03:09:58 UTC

vipula

am doing one project in that create 2 textboxs enter one textbox a number then show the 2nd textbox result  multiplication of that number with 7

20/09/2007 00:40:44 UTC

Priya

Hi,

I had a GridView and there is a dropdownlist in the GridView. Here is my problem when i was clicking on the EDIT button it is displaying an error.

Parameter name: value 'ddlConfiguration' has a SelectedValue which is invalid because it does not exist in the list of items.
Can u plz help me in this?

<asp:TemplateField HeaderText="Configuration">
<edititemtemplate>
<asp:DropDownList ID="ddlConfiguration" runat="server" DataSourceID="sql1"
DataTextField="fldConfigurationName" DataValueField="fldConfigurationID"
SelectedValue='<%# Bind("fldConfigurationName") %>'
AppendDataBoundItems="True" AutoPostBack="true">
</asp:DropDownList>
</edititemtemplate>
<ItemTemplate>
<asp:Label ID="lblConfiguration" runat="server" Text='<%#Bind("fldConfigurationName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
But I used 2 datasource. firstone(sql1) is for bind the value another one(sql2) is for to fill the items in dropdownlist.
which one I use in dropdownlist datasourceid=""
If is correct why error popsup.Reply please

18/10/2007 03:16:05 UTC