473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.SharePoint.WebPartPages.WebPart
{
// My controls
protected DataGrid dgDays;;
Label lblMessages;

private const string defaultText = "";

private string text = defaultText;

[Browsable(false),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"),
Description("Text Property")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

protected override void RenderWebPart(HtmlTextWriter output)
{
if (dgDays.Items.Count <= 0 && lblMessages.Text == "")
{
lblMessages.Text = "<b>No days found.</b>";
}
else
{
dgDays.RenderControl(output);
}
lblMessages.RenderControl(output);
}

protected override void CreateChildControls()
{
//base.CreateChildControls ();
dgDays = new DataGrid();
dgDays.BorderWidth = 0;
dgDays.CellPadding = 5;
dgDays.AlternatingItemStyle.CssClass = "ms-alternating";
dgDays.HeaderStyle.CssClass = "ms-alternating";
dgDays.HeaderStyle.Font.Bold = true;
dgDays.AutoGenerateColumns = false;
// Add Edit and Delete Columns
EditCommandColumn colEdit = new EditCommandColumn();
colEdit.ButtonType = ButtonColumnType.LinkButton;
colEdit.EditText = "Edit";
colEdit.CancelText = "Cancel";
colEdit.UpdateText = "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.ItemTemplate =
new DataGridTemplate(ListItemType.Item, "person");
colPerson.EditItemTemplate =
new DataGridTemplate(ListItemType.Item, "person");
colPerson.HeaderTemplate =
new DataGridTemplate(ListItemType.Header, "person");
dgDays.Columns.Add(colPerson);

colPlaceCode.ItemTemplate =
new DataGridTemplate(ListItemType.Item, "place code");
colPlaceCode.EditItemTemplate =
new DataGridTemplate(ListItemType.Item, "place code");
colPlaceCode.HeaderTemplate =
new DataGridTemplate(ListItemType.Header, "place code");
dgDays.Columns.Add(colPlaceCode);

colPlace.ItemTemplate =
new DataGridTemplate(ListItemType.Item, "place");
colPlace.EditItemTemplate =
new DataGridTemplate(ListItemType.EditItem, "place");
colPlace.HeaderTemplate =
new DataGridTemplate(ListItemType.Header, "place");
dgDays.Columns.Add(colPlace);

colDayOfTheWeek.ItemTemplate =
new DataGridTemplate(ListItemType.Item, "day of the week");
colDayOfTheWeek.EditItemTemplate =
new DataGridTemplate(ListItemType.EditItem, "day of the week");
colDayOfTheWeek.HeaderTemplate =
new DataGridTemplate(ListItemType.Header, "day of the week");
dgDays.Columns.Add(colDayOfTheWeek);

colPartOfTheDay.ItemTemplate =
new DataGridTemplate(ListItemType.Item, "part of the day");
colPartOfTheDay.EditItemTemplate =
new DataGridTemplate(ListItemType.EditItem, "part of the day");
colPartOfTheDay.HeaderTemplate =
new DataGridTemplate(ListItemType.Header, "part of the day");
dgDays.Columns.Add(colPartOfTheDay);

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

// Add event handler
dgDays.ItemCommand +=
new DataGridCommandEventHandler(dgDays_ItemCommand);

lblMessages = new Label();

this.Controls.Add(dgDays);
this.Controls.Add(lblMessages);

BindGrid();
}

protected void BindGrid()
{
SqlConnection cnn = new SqlConnection
(ConfigurationSettings.AppSettings["sqlConnectionString"]);

SqlDataAdapter da = new SqlDataAdapter("get_days_byid", cnn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@user_id", SqlDbType.VarChar,20);
da.SelectCommand.Parameters["@user_id"].Value =
HttpContext.Current.User.Identity.Name;
DataTable dt = new DataTable();

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

dgDays.DataSource = dt;
dgDays.DataBind();
}
catch(Exception ex)
{
lblMessages.Text = ex.Message;
}
finally
{
cnn.Close();
}
}

protected void dgDays_ItemCommand(Object source, DataGridCommandEventArgs
e)
{
switch (e.CommandName.ToLower())
{
case "edit":
dgDays.EditItemIndex = e.Item.ItemIndex;
break;
case "cancel":
dgDays.EditItemIndex = -1;
break;
}

BindGrid();
}
}

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

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

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

switch (columnName.ToLower())
{
case "person" :
switch (itemType)
{
case (ListItemType.Header) :
l = new Literal();
l.Text = "Person";
container.Controls.Add(l);
break;
case (ListItemType.Item) :
lbl = new Label();
lbl.DataBinding += new EventHandler(lblPerson_DataBinding);
container.Controls.Add(lbl);
break;
}
break;

case "place" :
switch (itemType)
{
case (ListItemType.Header) :
l = new Literal();
l.Text = "Place";
container.Controls.Add(l);
break;
case (ListItemType.Item) :
lbl = new Label();
lbl.DataBinding += new EventHandler(lblPlace_DataBinding);
container.Controls.Add(lbl);
break;

case (ListItemType.EditItem) :
ddl = new DropDownList();
ddl.DataBinding += new EventHandler(ddlPlace_DataBinding);
container.Controls.Add(ddl);
break;
}
break;

case "part of the day" :
switch (itemType)
{
case (ListItemType.Header) :
l = new Literal();
l.Text = "Part of the Day";
container.Controls.Add(l);
break;
case (ListItemType.Item) :
lbl = new Label();
lbl.DataBinding +=
new EventHandler(lblPartOfTheDay_DataBinding);
container.Controls.Add(lbl);
break;

case (ListItemType.EditItem) :
ddl = new DropDownList();
ddl.DataBinding +=
new EventHandler(ddlPartOfTheDay_DataBinding);
container.Controls.Add(ddl);
break;
}
break;

case "day of the week" :
switch (itemType)
{
case (ListItemType.Header) :
l = new Literal();
l.Text = "Day of the Week";
container.Controls.Add(l);
break;
case (ListItemType.Item) :
lbl = new Label();
lbl.DataBinding +=
new EventHandler(lblDayOfTheWeek_DataBinding);
container.Controls.Add(lbl);
break;

case (ListItemType.EditItem) :
ddl = new DropDownList();
ddl.DataBinding +=
new EventHandler(ddlDayOfTheWeek_DataBinding);
container.Controls.Add(ddl);
break;
}
break;

case "place code" :
switch (itemType)
{
case (ListItemType.Header) :
l = new Literal();
l.Text = "Place Code";
container.Controls.Add(l);
break;
case (ListItemType.Item) :
lbl = new Label();
lbl.ID = "place code";
lbl.DataBinding +=
new EventHandler(lblPlaceCode_DataBinding);
container.Controls.Add(lbl);
break;
}
break;
}
}

protected void lblDayOfTheWeek_DataBinding(Object source, System.EventArgs
e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingContainer;
lbl.Text = (string) DataBinder.Eval(container.DataItem, "day of the
week");
}

protected void lblPlace_DataBinding(Object source, System.EventArgs e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingContainer;
lbl.Text = (string) DataBinder.Eval(container.DataItem, "place");
}

protected void lblPartOfTheDay_DataBinding(Object source, System.EventArgs
e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingContainer;
lbl.Text = (string) DataBinder.Eval(container.DataItem, "part of the
day");
}

protected void ddlDayOfTheWeek_DataBinding(Object source, System.EventArgs
e)
{
DropDownList ddl = (DropDownList) source;
DataGridItem container = (DataGridItem) ddl.NamingContainer;
ddl.Items.Add(new ListItem("Mon","Mon"));
ddl.Items.Add(new ListItem("Tue","Tue"));
ddl.Items.Add(new ListItem("Wed","Wed"));
ddl.Items.Add(new ListItem("Thu","Thu"));
ddl.Items.Add(new ListItem("Fri","Fri"));
ddl.Items.Add(new ListItem("Sat","Sat"));
ddl.Items.Add(new ListItem("Sun","Sun"));
ddl.SelectedValue =
(string) DataBinder.Eval(container.DataItem, "day of the week");
}

protected void ddlPlace_DataBinding(Object source, System.EventArgs e)
{
DropDownList ddl = (DropDownList) source;
DataGridItem container = (DataGridItem) ddl.NamingContainer;
ddl.DataTextField = "place name";
ddl.DataValueField = "place code";
ddl.DataSource = GetLookupTable("get_places");
//
}

protected void ddlPartOfTheDay_DataBinding(Object source, System.EventArgs
e)
{
DropDownList ddl = (DropDownList) source;
DataGridItem container = (DataGridItem) ddl.NamingContainer;
ddl.Items.Add(new ListItem("All Day","All Day"));
ddl.Items.Add(new ListItem("Morning","Morning"));
ddl.Items.Add(new ListItem("Afternoon","Afternoon"));
ddl.SelectedValue =
(string) DataBinder.Eval(container.DataItem, "part of the day");
}

protected void lblPerson_DataBinding(Object source, System.EventArgs e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingContainer;
lbl.Text = (string) DataBinder.Eval(container.DataItem, "person");
}

protected void lblPlaceCode_DataBinding(Object source, System.EventArgs e)
{
Label lbl = (Label) source;
DataGridItem container = (DataGridItem) lbl.NamingContainer;
lbl.Text = (string) DataBinder.Eval(container.DataItem, "place code");
}

protected SqlDataReader GetLookupTable(string StoredProcedure)
{
SqlConnection cnn = new SqlConnection
(ConfigurationSettings.AppSettings["sqlConnectionString"]);
SqlCommand cmd = new SqlCommand(StoredProcedure, cnn);
cmd.CommandType = CommandType.StoredProcedure;

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

Nov 19 '05 #1
0 1807

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

Similar topics

0
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...
3
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...
2
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:...
0
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...
5
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
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...
2
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"...
0
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='<%#...
2
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.