473,473 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Edit DataGrid Dropdown cell

ASP.NET 2003

In the DataGrid how do I select current cell value in the dropdown box when
I click on the edit link, currently when I click on the edit link in the
DataGrid the dropdown box appears in the cell, but allways the first item in
the dropdown box is shown not the current cell value?

How do I make the current value in the cell automaticaly be selected in the
dropdown box.
// Code Behind

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DataGridDemo
{
/// <summary>
/// Summary description for WebForm4.
/// </summary>
public class Dropdown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}

private void BindData()
{
// Select from the authors table of the pubs database
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Employees",
sqlConnection1);
sqlCmd.CommandType = CommandType.Text;

sqlConnection1.Open();
// Set the data source for this datagrid to the datareader
// returned from the ExecuteReader method
DataGrid1.DataSource = sqlCmd.ExecuteReader();

// Bind the datagrid to the datasource
DataGrid1.DataBind();
sqlConnection1.Close();
}

public ICollection LoadNames()
{
// A new connection must be created because the one
// used for binding the DataGrid is still in use.
SqlConnection sqlNewConnection = new SqlConnection();
sqlNewConnection.ConnectionString = sqlConnection1.ConnectionString;

SqlDataAdapter da = new SqlDataAdapter("SELECT FirstName FROM Employees",
sqlNewConnection);
DataSet ds = new DataSet();
da.Fill(ds);

return ds.Tables[0].DefaultView;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=DEVCON4SVR;packet
size=4096;user id=sa;data source=DEVCON4SVR;pers" +
"ist security info=False;initial catalog=Northwind";
this.sqlConnection1.InfoMessage += new
System.Data.SqlClient.SqlInfoMessageEventHandler(t his.sqlConnection1_InfoMessage);
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.OnCancel);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.OnEdit);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.OnUpdate);
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void OnEdit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.DataSetIndex;
BindData();
}

private void OnUpdate(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// Find the drop down list
DropDownList DropList = (DropDownList)e.Item.FindControl("dlNames");
string strTest = DropList.SelectedItem.Text;

// Process updated data here

DataGrid1.EditItemIndex = -1;
BindData();
}

private void OnCancel(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}

private void sqlConnection1_InfoMessage(object sender,
System.Data.SqlClient.SqlInfoMessageEventArgs e)
{

}

private void DataGrid1_SelectedIndexChanged(object sender,
System.EventArgs e)
{

}
}
}

// ASPX page

<%@ Page language="c#" Codebehind="Dropdown.aspx.cs" AutoEventWireup="false"
Inherits="DataGridDemo.Dropdown" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm3" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 11px; POSITION:
absolute; TOP: 45px" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="First Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.FirstName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist ID="dlNames" DataSource='<%# LoadNames() %>'
DataTextField="FirstName" runat="server">
</asp:dropdownlist>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.city") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.city") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>&nbsp;</form>
</body>
</HTML>
May 25 '06 #1
2 2831
Hi Peter,

Thank you for post!

You will have to manually set the DropDownList's SelectedIndex property
according to the current record's FirstName field value, you can do this in
DataGrid's ItemDataBound event.

Here's the sample code:

--------------
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem )
{
string currentName = DataBinder.Eval(e.Item.DataItem, "FirstName")
as string ;
DropDownList ddl = e.Item.FindControl("dlNames") as DropDownList;
for (int i = 0; i < ddl.Items.Count; i++)
{
ListItem item = ddl.Items[i];
if (item.Text == currentName)
{
ddl.SelectedIndex = i;
break;
}
}
}
}
--------

Hope this helps. If there's anything unclear, please feel free to post here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

May 25 '06 #2
Thank You very much, this is what I needed !
"Walter Wang [MSFT]" <wa****@online.microsoft.com> wrote in message
news:WH**************@TK2MSFTNGXA01.phx.gbl...
Hi Peter,

Thank you for post!

You will have to manually set the DropDownList's SelectedIndex property
according to the current record's FirstName field value, you can do this
in
DataGrid's ItemDataBound event.

Here's the sample code:

--------------
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem )
{
string currentName = DataBinder.Eval(e.Item.DataItem, "FirstName")
as string ;
DropDownList ddl = e.Item.FindControl("dlNames") as DropDownList;
for (int i = 0; i < ddl.Items.Count; i++)
{
ListItem item = ddl.Items[i];
if (item.Text == currentName)
{
ddl.SelectedIndex = i;
break;
}
}
}
}
--------

Hope this helps. If there's anything unclear, please feel free to post
here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

May 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tamir Khason | last post by:
How to prevent the selected cell from being editable (visual) at DataGrid? Once click on cell (even readonly) there are cursor inside it and select text appears. How to prevent it Thankx
0
by: Alex | last post by:
I have a column that shows a string based on a join with an other table that has an index to string mapping. The main table is referencing the "helper" table. While in the edit mode of the...
2
by: Dave | last post by:
Hi, I'm building a maintenance form for a table and some of the fields are textboxes (i.e name) and some should be dropdowns (i.e country of origin) When a user clicks 'Edit' in the...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
3
by: Schultz | last post by:
is there an easy to follow, source code in one document, article that explains how to create an edit all DataGrid control? and, I have a DataGrid setup where the client enters information for...
3
by: Engineerik | last post by:
The vb6 datagrid had a "button" property which I used to display a dropdown icon in a cell. I created a popup menu that would display the list of valid choices for that cell when the user clicked...
4
by: Peter | last post by:
(VS 2003) I have a DataGrid and accept button. My problem is when a user starts to edit one of the values in the grid and does not either press Enter or move of the current cell and presses the...
0
by: anjupt | last post by:
Hi, I can add values to a dropdown from 1 to 100 using a while loop in page load.ie Dim i = 1 While i <= 100 Me.DropDownList2.Items.Add(i) ' i = i +...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.