473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

editable datagrid dropdownlist problem in webpart

Hello and apologies in advance for the amount of code in this post. I've
also sent this message to the Sharepoint group, but thought that ASP.NET
developers may also be able to help, even though it's a Sharepoint WebPart.
I'm trying to do something fairly simple, create a datagrid that displays
where
and when a person works and allows them to change some of the information
via DropDownLists.
When the user clicks to edit a row, three of the columns appear as
DropDownLists and should show the current value.
For the 'day of the week' and 'part of the day', selecting the current value
is easy as it's the same as the value shown by the label, i.e. the
DataTextField and DataValueField are the same for those DropDownLists.
However, the 'place' DropDownList has a DataTextField of 'place' and the
DataValueField is 'place code'. I can't select an item in the DropDownList
by using what is in the label, as the label displays the 'place'.
To get around this, I added an extra column for 'place code' and hoped I
could set the SelectedValue of the DropDownList equal to the value of this
label when I'm binding the DropDownList, but I can't seem to get this
working.
Ideally, I would like to do this without the extra 'place code' column,
which I intended to make invisible, but anything is better than what I have
now!

Any help would be greatly appreciated.

Here's the code.... I've tried to make it a little more generic and removed
the namespace declaration etc.
--------------------------------------------
public class EditDays : Microsoft.Share Point.WebPartPa ges.WebPart
{
// My controls
protected DataGrid dgDays;;
Label lblMessages;

private const string defaultText = "";

private string text = defaultText;

[Browsable(false ),
Category("Misce llaneous"),
DefaultValue(de faultText),
WebPartStorage( Storage.Persona l),
FriendlyName("T ext"),
Description("Te xt Property")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

protected override void RenderWebPart(H tmlTextWriter output)
{
if (dgDays.Items.C ount <= 0 && lblMessages.Tex t == "")
{
lblMessages.Tex t = "<b>No days found.</b>";
}
else
{
dgDays.RenderCo ntrol(output);
}
lblMessages.Ren derControl(outp ut);
}

protected override void CreateChildCont rols()
{
//base.CreateChil dControls ();
dgDays = new DataGrid();
dgDays.BorderWi dth = 0;
dgDays.CellPadd ing = 5;
dgDays.Alternat ingItemStyle.Cs sClass = "ms-alternating";
dgDays.HeaderSt yle.CssClass = "ms-alternating";
dgDays.HeaderSt yle.Font.Bold = true;
dgDays.AutoGene rateColumns = false;
// Add Edit and Delete Columns
EditCommandColu mn colEdit = new EditCommandColu mn();
colEdit.ButtonT ype = ButtonColumnTyp e.LinkButton;
colEdit.EditTex t = "Edit";
colEdit.CancelT ext = "Cancel";
colEdit.UpdateT ext = "Update";

// Add the columns
TemplateColumn colPerson = new TemplateColumn( );
TemplateColumn colPlace = new TemplateColumn( );
TemplateColumn colDayOfTheWeek = new TemplateColumn( );
TemplateColumn colPartOfTheDay = new TemplateColumn( );
TemplateColumn colPlaceCode = new TemplateColumn( );

colPerson.ItemT emplate =
new DataGridTemplat e(ListItemType. Item, "person");
colPerson.EditI temTemplate =
new DataGridTemplat e(ListItemType. Item, "person");
colPerson.Heade rTemplate =
new DataGridTemplat e(ListItemType. Header, "person");
dgDays.Columns. Add(colPerson);

colPlaceCode.It emTemplate =
new DataGridTemplat e(ListItemType. Item, "place code");
colPlaceCode.Ed itItemTemplate =
new DataGridTemplat e(ListItemType. Item, "place code");
colPlaceCode.He aderTemplate =
new DataGridTemplat e(ListItemType. Header, "place code");
dgDays.Columns. Add(colPlaceCod e);

colPlace.ItemTe mplate =
new DataGridTemplat e(ListItemType. Item, "place");
colPlace.EditIt emTemplate =
new DataGridTemplat e(ListItemType. EditItem, "place");
colPlace.Header Template =
new DataGridTemplat e(ListItemType. Header, "place");
dgDays.Columns. Add(colPlace);

colDayOfTheWeek .ItemTemplate =
new DataGridTemplat e(ListItemType. Item, "day of the week");
colDayOfTheWeek .EditItemTempla te =
new DataGridTemplat e(ListItemType. EditItem, "day of the week");
colDayOfTheWeek .HeaderTemplate =
new DataGridTemplat e(ListItemType. Header, "day of the week");
dgDays.Columns. Add(colDayOfThe Week);

colPartOfTheDay .ItemTemplate =
new DataGridTemplat e(ListItemType. Item, "part of the day");
colPartOfTheDay .EditItemTempla te =
new DataGridTemplat e(ListItemType. EditItem, "part of the day");
colPartOfTheDay .HeaderTemplate =
new DataGridTemplat e(ListItemType. Header, "part of the day");
dgDays.Columns. Add(colPartOfTh eDay);

// Add edit and delete columns last so that they appear at the end
dgDays.Columns. Add(colEdit);

// Add event handler
dgDays.ItemComm and +=
new DataGridCommand EventHandler(dg Days_ItemComman d);

lblMessages = new Label();

this.Controls.A dd(dgDays);
this.Controls.A dd(lblMessages) ;

BindGrid();
}

protected void BindGrid()
{
SqlConnection cnn = new SqlConnection
(ConfigurationS ettings.AppSett ings["sqlConnectionS tring"]);

SqlDataAdapter da = new SqlDataAdapter( "get_days_byid" , cnn);
da.SelectComman d.CommandType = CommandType.Sto redProcedure;
da.SelectComman d.Parameters.Ad d("@user_id", SqlDbType.VarCh ar,20);
da.SelectComman d.Parameters["@user_id"].Value =
HttpContext.Cur rent.User.Ident ity.Name;
DataTable dt = new DataTable();

try
{
cnn.Open();
da.Fill(dt);

dgDays.DataSour ce = dt;
dgDays.DataBind ();
}
catch(Exception ex)
{
lblMessages.Tex t = ex.Message;
}
finally
{
cnn.Close();
}
}

protected void dgDays_ItemComm and(Object source, DataGridCommand EventArgs
e)
{
switch (e.CommandName. ToLower())
{
case "edit":
dgDays.EditItem Index = e.Item.ItemInde x;
break;
case "cancel":
dgDays.EditItem Index = -1;
break;
}

BindGrid();
}
}

// Class used to add template columns to datagrid
public class DataGridTemplat e : ITemplate
{
private string columnName;
private ListItemType itemType;

public DataGridTemplat e(ListItemType ItemType, string ColumnName)
{
columnName = ColumnName;
itemType = ItemType;
}

public void InstantiateIn(C ontrol container)
{
DropDownList ddl;
Literal l;
Label lbl;

switch (columnName.ToL ower())
{
case "person" :
switch (itemType)
{
case (ListItemType.H eader) :
l = new Literal();
l.Text = "Person";
container.Contr ols.Add(l);
break;
case (ListItemType.I tem) :
lbl = new Label();
lbl.DataBinding += new EventHandler(lb lPerson_DataBin ding);
container.Contr ols.Add(lbl);
break;
}
break;

case "place" :
switch (itemType)
{
case (ListItemType.H eader) :
l = new Literal();
l.Text = "Place";
container.Contr ols.Add(l);
break;
case (ListItemType.I tem) :
lbl = new Label();
lbl.DataBinding += new EventHandler(lb lPlace_DataBind ing);
container.Contr ols.Add(lbl);
break;

case (ListItemType.E ditItem) :
ddl = new DropDownList();
ddl.DataBinding += new EventHandler(dd lPlace_DataBind ing);
container.Contr ols.Add(ddl);
break;
}
break;

case "part of the day" :
switch (itemType)
{
case (ListItemType.H eader) :
l = new Literal();
l.Text = "Part of the Day";
container.Contr ols.Add(l);
break;
case (ListItemType.I tem) :
lbl = new Label();
lbl.DataBinding +=
new EventHandler(lb lPartOfTheDay_D ataBinding);
container.Contr ols.Add(lbl);
break;

case (ListItemType.E ditItem) :
ddl = new DropDownList();
ddl.DataBinding +=
new EventHandler(dd lPartOfTheDay_D ataBinding);
container.Contr ols.Add(ddl);
break;
}
break;

case "day of the week" :
switch (itemType)
{
case (ListItemType.H eader) :
l = new Literal();
l.Text = "Day of the Week";
container.Contr ols.Add(l);
break;
case (ListItemType.I tem) :
lbl = new Label();
lbl.DataBinding +=
new EventHandler(lb lDayOfTheWeek_D ataBinding);
container.Contr ols.Add(lbl);
break;

case (ListItemType.E ditItem) :
ddl = new DropDownList();
ddl.DataBinding +=
new EventHandler(dd lDayOfTheWeek_D ataBinding);
container.Contr ols.Add(ddl);
break;
}
break;

case "place code" :
switch (itemType)
{
case (ListItemType.H eader) :
l = new Literal();
l.Text = "Place Code";
container.Contr ols.Add(l);
break;
case (ListItemType.I tem) :
lbl = new Label();
lbl.ID = "place code";
lbl.DataBinding +=
new EventHandler(lb lPlaceCode_Data Binding);
container.Contr ols.Add(lbl);
break;
}
break;
}
}

protected void lblDayOfTheWeek _DataBinding(Ob ject source, System.EventArg s
e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingConta iner;
lbl.Text = (string) DataBinder.Eval (container.Data Item, "day of the
week");
}

protected void lblPlace_DataBi nding(Object source, System.EventArg s e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingConta iner;
lbl.Text = (string) DataBinder.Eval (container.Data Item, "place");
}

protected void lblPartOfTheDay _DataBinding(Ob ject source, System.EventArg s
e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingConta iner;
lbl.Text = (string) DataBinder.Eval (container.Data Item, "part of the
day");
}

protected void ddlDayOfTheWeek _DataBinding(Ob ject source, System.EventArg s
e)
{
DropDownList ddl = (DropDownList) source;
DataGridItem container = (DataGridItem) ddl.NamingConta iner;
ddl.Items.Add(n ew ListItem("Mon", "Mon"));
ddl.Items.Add(n ew ListItem("Tue", "Tue"));
ddl.Items.Add(n ew ListItem("Wed", "Wed"));
ddl.Items.Add(n ew ListItem("Thu", "Thu"));
ddl.Items.Add(n ew ListItem("Fri", "Fri"));
ddl.Items.Add(n ew ListItem("Sat", "Sat"));
ddl.Items.Add(n ew ListItem("Sun", "Sun"));
ddl.SelectedVal ue =
(string) DataBinder.Eval (container.Data Item, "day of the week");
}

protected void ddlPlace_DataBi nding(Object source, System.EventArg s e)
{
DropDownList ddl = (DropDownList) source;
DataGridItem container = (DataGridItem) ddl.NamingConta iner;
ddl.DataTextFie ld = "place name";
ddl.DataValueFi eld = "place code";
ddl.DataSource = GetLookupTable( "get_places ");
//
}

protected void ddlPartOfTheDay _DataBinding(Ob ject source, System.EventArg s
e)
{
DropDownList ddl = (DropDownList) source;
DataGridItem container = (DataGridItem) ddl.NamingConta iner;
ddl.Items.Add(n ew ListItem("All Day","All Day"));
ddl.Items.Add(n ew ListItem("Morni ng","Morning")) ;
ddl.Items.Add(n ew ListItem("After noon","Afternoo n"));
ddl.SelectedVal ue =
(string) DataBinder.Eval (container.Data Item, "part of the day");
}

protected void lblPerson_DataB inding(Object source, System.EventArg s e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingConta iner;
lbl.Text = (string) DataBinder.Eval (container.Data Item, "person");
}

protected void lblPlaceCode_Da taBinding(Objec t source, System.EventArg s e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingConta iner;
lbl.Text = (string) DataBinder.Eval (container.Data Item, "place code");
}

protected SqlDataReader GetLookupTable( string StoredProcedure )
{
SqlConnection cnn = new SqlConnection
(ConfigurationS ettings.AppSett ings["sqlConnectionS tring"]);
SqlCommand cmd = new SqlCommand(Stor edProcedure, cnn);
cmd.CommandType = CommandType.Sto redProcedure;

try
{
cnn.Open();
SqlDataReader dr = cmd.ExecuteRead er();
return dr;
}
catch(Exception ex)
{
return null;
}
}
}
}

Nov 19 '05 #1
0 1832

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

Similar topics

0
4050
by: Faith | last post by:
I found code on www.codeproject.com. Here is a sample it is called DataGridColumnDropDown. I modified it to use the text box when I wanted it to depended on a certain search criteria. Public Class ExtendedSearchFieldValueColumnStyle Inherits DataGridTextBoxColumn Public WithEvents ColumnComboBox As ComboBox Public WithEvents ColumnTextBox As TextBox
3
4935
by: Kevin Pedersen | last post by:
Hello, I am using an editable datagrid. After I save the changes the datagrid shows the old values. I've read the posts about the Page_Load and not binding the datagrid each time. The SQL that is being sent to the database is correct and the changes are eventually being made. If I refresh the page after the update then the new values appear. I noticed that when I put a breakpoint in my update handler everything works fine. When I take...
2
1614
by: Stephan Bour | last post by:
Hi, I¹ve tried to implement a solution to populate a dropdownlist inside an editable datagrid. The code is simple enough and an example can be found here: <http://www.4guysfromrolla.com/webtech/050801-1.shtml> The problem I'm having is that the entire datagrid disappears after I click the Edit link. Could someone tell me what I'm doing wrong? Much appreciated, Stephan.
0
272
by: anon | last post by:
Hi All, I have an editable datagrid in which one of the fields becomes a Drop dowl list in edit mode....i followed the example in the $ guysfrom rolla article http://www.4guysfromrolla.com/webtech/050801-1.2.shtml however one of them is not really populated from the database, and hence no need of a dataview...i would like to assign an array or arraylist alone to it, however it gives all sorts of errors....the code is as follows
5
1210
by: jack | last post by:
Hello, I have an Editable Datalist with a DropDownList. I can get the Get value, but can't Set the Current Value set to the value of the current Edited Item. Thanks, Jack
4
3253
by: Carlo Marchesoni | last post by:
I have an editable Datagrid and some columns (4 and 5) are TemplateColumns (because I have DropDownLists there). Everything works fine except that if I want to retrieve the values (that I can see visually) from the DataGrid with the following loop the Values of the TemplateColumns are returned as blank - what am I doing wrong ?: foreach (DataGridItem dgi in dgrDu01.Items) { string myvalue1 = dgi.Cells.Text; //ok etc.. string myvalue =...
2
5040
by: zambizzi | last post by:
....I can't seem to get my hands on a control I'm loading in an editable datagrid. Here's my datagrid control: <asp:datagrid id="GLRulesGrid" runat="server" autogeneratecolumns="False" oneditcommand="GLRulesGrid_Edit"
0
1402
by: shamila.thakur | last post by:
I have an editable datagrid one of itz column is a dropdownlist <asp:TemplateColumn> <ItemTemplate> <asp:Label ID=lblAreaWebsiteID Runat=server text='<%# DataBinder.Eval(Container.DataItem,"WebsiteName")%>'></asp:Label> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlWebsiteName_edit" Runat=server
2
1732
by: Frank | last post by:
Hello, I am developing with VS.Net 2003 on the asp 1.1 platform. I am a few days new to using a datagrid. I found a nice tutorial and had no problems using an editable datagrid with textboxes and an additional buttoncolumn where I used a delete button to delete a specific row. Once I had finished that, I trnsformed the grid tso it will display a dropdownlist indtead of a textbox. After much grappling, I pretty much have
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6679
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.