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; 

 

OnRowUpdating="GridView1_RowUpdating" >









DataSourceID="SqlDataSource1" DataValueField=username AutoPostBack="True" SelectedValue='<%# Bind("username")%>'>
Select an Item




 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.

 

SelectCommand="SELECT * FROM [ChatMessages]"
UpdateCommand="UPDATE [ChatMessages] SET [username] = @username, [message] = @message WHERE id=@id">









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

Tamby bazdokh

how i can display dropdownlist after clicked in edit button \\ i want call this finction to display DDL

 

<%

@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<

<

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">html xmlns="http://www.w3.org/1999/xhtml" >head runat="server">

 

<

<title>Untitled Pagetitle>head>body>

 

<form id="form1" runat="server">

 

<div>

 

<asp:GridView ID="GridView1" runat="server" BackColor="White" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" BorderColor="White" BorderStyle="Ridge" AutoGenerateEditButton="false" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" AutoGenerateColumns="false">

 

<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />

 

<RowStyle BackColor="#DEDFDE" ForeColor="Black" />

 

<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />

 

<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />

 

<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />

 

 

 

<Columns>

 

 

<asp:TemplateField HeaderText="Edit"> <ItemTemplate>

 

<asp:Button ID="Button2" CommandName="Edit" Runat="Server" Text="Edit" Font-Size="8pt" Width="45px"/>

 

ItemTemplate>

 

<EditItemTemplate>

 

 

<asp:Button ID="Button3" CommandName="Update" Runat="Server" Text="Update" Font-Size="8pt" Width="45px"/> <asp:Button ID="Button4" CommandName="Cancel" Runat="Server" Text="Cancel" Font-Size="8pt" Width="45px"/>

 

EditItemTemplate>

 

asp:TemplateField>

 

<asp:ButtonField HeaderText="ID" DataTextField="ID" CommandName="Select" SortExpression="ID"/>

 

<asp:BoundField ReadOnly="True" HeaderText="ID" DataField="ID" SortExpression="ID"/>

 

<asp:BoundField HeaderText="Name" DataField="EName"/>

 

<asp:BoundField HeaderText="PhoneNo" DataField="PhoneNo"/>

 

<asp:TemplateField HeaderText="Type">

 

<ItemTemplate>

 

<asp:Label ID="NType" Text='<%# Eval("NType") %>' Runat="Server"/>

 

ItemTemplate>

 

 

<EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" Width="125px">

 

asp:DropDownList>

 

EditItemTemplate>

 

asp:TemplateField>

 

 

Columns>

 

 

asp:GridView> div>

 

<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>

 

<asp:TextBox ID="TextBox2" runat="server">asp:TextBox>

 

<asp:DropDownList ID="DropDownList1" runat="server" Width="125px">

 

asp:DropDownList>

 

<asp:Button ID="Button1" runat="server" Text="ADD" Width="111px" />

 

how i can display dropdownlist after clicked in edit button \\ i want call this finction to display DDL

Private

 

 

 

 

myConnection =

myCommand =

myConnection.Open()

myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

DropDownList1.DataSource = myDataReader

DropDownList1.DataValueField =

 

DropDownList1.DataTextField =

 

DropDownList1.DataBind()

myDataReader.Close()

myConnection.Close()

 

 

Sub PopulateDDL()Dim myConnection As SqlConnectionDim myCommand As SqlCommandDim myDataReader As SqlDataReaderDim con As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnString").ConnectionStringNew SqlConnection(con)New SqlCommand("select * from NUserType", myConnection)"ID""NType"End Subform>body>html>

15/11/2007 05:47:12 UTC

ritesh
I want to bind a Button with Gridview just like same above but on click of this button i have to populate other Gridviwe with some detail so can any one suggestme how to write click on the click of the Button?

15/01/2008 04:44:34 UTC

ashish patel

i want to add combo box in gride and when i select one value in that como it display on gride

and attrective disigen tool in dot net

26/03/2008 11:38:24 UTC

John
Great post!! I'd been looking for ideas on the GridView1_RowUpdate code.

29/05/2008 14:26:54 UTC

sp

hw can i  bind a grid row to a textbox in another form i.e when the link in the grid is clicked the corresponding record in the row is displayed in the textbox in another form 

24/07/2008 04:26:10 UTC

JoshF
Thank You!  This was a big help. :-)

18/09/2008 18:54:06 UTC

Mark N
Sheer genius!  The most straightforward example of how to do this I have found...

02/10/2008 00:45:09 UTC

David
Thank you very much!!!!!!

05/12/2008 14:04:11 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)