DropDownList in DataGrid EditItemTemplate Return Error

R

rsw8n

I have a dropdown list in the edititem template of a data grid. I
have no problems populating the dropdown box when I click edit.
However, if I edit and select a different item in the dropdown list I
always get the top value in the list returned in my UpdateCommand
event. Below is my code:

private void BindCurrentActivityGrid(string strVendorid)
{
DataSet ds;

SqlParameter [] param = new SqlParameter[1];
SqlParameter spparm = new SqlParameter();

spparm.ParameterName = "@vendorid";
spparm.Value = strVendorid;
spparm.SqlDbType = SqlDbType.Char;

param[0] = spparm;

ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspCurrentActivities_GetByVendorId",
param);

DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dgCurrentActivities.DataSource = dv;
dgCurrentActivities.DataBind();
}

private void UpdateCurrentActivityGrid(string strValue,string
iStatus)
{
SqlParameter [] param = new SqlParameter[2];
SqlParameter spparm = new SqlParameter();

spparm.ParameterName = "@ponumber";
spparm.Value = strValue;
spparm.SqlDbType = SqlDbType.VarChar;
param[0] = spparm;

SqlParameter spparm1 = new SqlParameter();
spparm1.Value = Convert.ToInt32(iStatus);
spparm1.ParameterName = "@status";
spparm1.SqlDbType = SqlDbType.SmallInt;
param[1] = spparm1;

SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspPOStatus_Update",param);

}
#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.dgCurrentActivities.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnCancel);
this.dgCurrentActivities.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnEdit);
this.dgCurrentActivities.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdate);
this.dgCurrentActivities.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgItemDataBound);
this.dgCurrentActivities.SelectedIndexChanged += new
System.EventHandler(this.Changed);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void GetStatusCodes()
{

SqlParameter [] param = new SqlParameter[1];
SqlParameter spparm = new SqlParameter();

dsStatusCodes = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspStatusCode_GetAll");

}

private void OnEdit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dl = new DropDownList();
ListItemType lit = new ListItemType();

lit = e.Item.ItemType;

GetStatusCodes();
// dl = (DropDownList)(e.Item.Cells[3].FindControl("ddStatus"));
// dl.DataSource = dsStatusCodes;
// dl.DataTextField = "POStatusCdDescription";
// dl.DataValueField = "POStatusId";


dgCurrentActivities.EditItemIndex = e.Item.ItemIndex;
BindCurrentActivityGrid(strVendor);
}

private void OnUpdate(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dl = new DropDownList();
string strStatus;
string strPO;
ListItemType lit = new ListItemType();

lit = e.Item.ItemType;

if (lit == ListItemType.EditItem)
{
strPO = dgCurrentActivities.DataKeys[e.Item.ItemIndex].ToString();
dl = (DropDownList)(e.Item.FindControl("ddStatus"));
strStatus = dl.SelectedItem.Text.ToString();
}

dgCurrentActivities.EditItemIndex = -1;
BindCurrentActivityGrid(strVendor);

}

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

private void dgItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{ DropDownList dl = new DropDownList();
ListItemType lit = new ListItemType();
lit = e.Item.ItemType;

if (lit == ListItemType.EditItem)
{
dl = (DropDownList)(e.Item.FindControl("ddStatus"));
dl.DataTextField = "POStatusCdDescription";
dl.DataValueField = "POStatusId";
dl.DataSource = dsStatusCodes;
dl.DataBind();
}
}

private void Changed(object sender, System.EventArgs e)
{
string strtest;

strtest = e.ToString();
}

The above Changed function is supposed to be called when the
SelectedIndexChanged event is fired in the datagrid.
Below is my asp.net stuff:

<asp:DataGrid id="dgCurrentActivities" runat="server"
AutoGenerateColumns="False" DataKeyField="ponumber">
<Columns>
<asp:HyperLinkColumn Target="_self"
DataNavigateUrlField="ponumber"
DataNavigateUrlFormatString="PODetail.aspx?{0}"
DataTextField="ponumber" HeaderText="PO
#"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="lotnumber" HeaderText="Lot #"
ReadOnly =true></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="Description" ReadOnly =true></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label id=lblStatusCodes runat="server" Width="65px"
Text='<%#DataBinder.Eval(Container.DataItem, "status") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddStatus"
runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>

Any insight would be greatly appreciated.

rsw8n
 
M

Michael Tkachev

You should create property for example Global class that can you access for
receive Connection String:

public class Global

{

....

public static string ConnectionString

{
get
{
return ConfigurationSettings.AppSettings["connStr"];
}
}

}

private void BindCurrentActivityGrid(string strVendorid)
{
SqlDataAdapter cmd = new
SqlDataAdapter("uspCurrentActivities_GetByVendorId", new
SqlConnection(Global.ConnectionString))
cmd.SelectCommand.CommadType = CommadType.StorageProcedure;
cmd.SelectCommand.Parameters.Add("@vendorid", strVendorid);

DataSet ds = new DataSet();
cmd.Fill(ds);

dg.DataSourse = ds.Tables[0].DefaultView;
dg.DataBind();
}

private void OnEdit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dg.EditItemIndex = e.Item.ItemIndex;
BindCurrentActivityGrid("");

DropDownList dl =
((DropDownList)dgdUsers.Items[e.Item.ItemIndex].Cells[2].FindControl("ddStat
us"));
dl.DataSourse = GetStatus();
dl.DataBind();
dl.Items.FinfByValue("...").Selected = true;
}

private DateView GetStatus()
{
SqlDataAdapter cmd = new SqlDataAdapter("...", new
SqlConnection(Global.ConnectionString))
cmd.SelectCommand.CommadType = CommadType.StorageProcedure;

DataSet ds = new DataSet();
cmd.Fill(ds);

return ds.Tables[0].DefaultView;
}

<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "status") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddStatus" runat="server"
DataTextField="POStatusCdDescription"
DataValueField="POStatusId"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

The Best Regards,
Web Developer
Michael Tkachev

rsw8n said:
I have a dropdown list in the edititem template of a data grid. I
have no problems populating the dropdown box when I click edit.
However, if I edit and select a different item in the dropdown list I
always get the top value in the list returned in my UpdateCommand
event. Below is my code:

private void BindCurrentActivityGrid(string strVendorid)
{
DataSet ds;

SqlParameter [] param = new SqlParameter[1];
SqlParameter spparm = new SqlParameter();

spparm.ParameterName = "@vendorid";
spparm.Value = strVendorid;
spparm.SqlDbType = SqlDbType.Char;

param[0] = spparm;

ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspCurrentActivities_GetByVendorId",
param);

DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dgCurrentActivities.DataSource = dv;
dgCurrentActivities.DataBind();
}

private void UpdateCurrentActivityGrid(string strValue,string
iStatus)
{
SqlParameter [] param = new SqlParameter[2];
SqlParameter spparm = new SqlParameter();

spparm.ParameterName = "@ponumber";
spparm.Value = strValue;
spparm.SqlDbType = SqlDbType.VarChar;
param[0] = spparm;

SqlParameter spparm1 = new SqlParameter();
spparm1.Value = Convert.ToInt32(iStatus);
spparm1.ParameterName = "@status";
spparm1.SqlDbType = SqlDbType.SmallInt;
param[1] = spparm1;

SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspPOStatus_Update",param);

}
#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.dgCurrentActivities.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnCancel);
this.dgCurrentActivities.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnEdit);
this.dgCurrentActivities.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdate);
this.dgCurrentActivities.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgItemDataBound);
this.dgCurrentActivities.SelectedIndexChanged += new
System.EventHandler(this.Changed);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void GetStatusCodes()
{

SqlParameter [] param = new SqlParameter[1];
SqlParameter spparm = new SqlParameter();

dsStatusCodes = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspStatusCode_GetAll");

}

private void OnEdit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dl = new DropDownList();
ListItemType lit = new ListItemType();

lit = e.Item.ItemType;

GetStatusCodes();
// dl = (DropDownList)(e.Item.Cells[3].FindControl("ddStatus"));
// dl.DataSource = dsStatusCodes;
// dl.DataTextField = "POStatusCdDescription";
// dl.DataValueField = "POStatusId";


dgCurrentActivities.EditItemIndex = e.Item.ItemIndex;
BindCurrentActivityGrid(strVendor);
}

private void OnUpdate(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dl = new DropDownList();
string strStatus;
string strPO;
ListItemType lit = new ListItemType();

lit = e.Item.ItemType;

if (lit == ListItemType.EditItem)
{
strPO = dgCurrentActivities.DataKeys[e.Item.ItemIndex].ToString();
dl = (DropDownList)(e.Item.FindControl("ddStatus"));
strStatus = dl.SelectedItem.Text.ToString();
}

dgCurrentActivities.EditItemIndex = -1;
BindCurrentActivityGrid(strVendor);

}

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

private void dgItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{ DropDownList dl = new DropDownList();
ListItemType lit = new ListItemType();
lit = e.Item.ItemType;

if (lit == ListItemType.EditItem)
{
dl = (DropDownList)(e.Item.FindControl("ddStatus"));
dl.DataTextField = "POStatusCdDescription";
dl.DataValueField = "POStatusId";
dl.DataSource = dsStatusCodes;
dl.DataBind();
}
}

private void Changed(object sender, System.EventArgs e)
{
string strtest;

strtest = e.ToString();
}

The above Changed function is supposed to be called when the
SelectedIndexChanged event is fired in the datagrid.
Below is my asp.net stuff:

<asp:DataGrid id="dgCurrentActivities" runat="server"
AutoGenerateColumns="False" DataKeyField="ponumber">
<Columns>
<asp:HyperLinkColumn Target="_self"
DataNavigateUrlField="ponumber"
DataNavigateUrlFormatString="PODetail.aspx?{0}"
DataTextField="ponumber" HeaderText="PO
#"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="lotnumber" HeaderText="Lot #"
ReadOnly =true></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="Description" ReadOnly =true></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label id=lblStatusCodes runat="server" Width="65px"
Text='<%#DataBinder.Eval(Container.DataItem, "status") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddStatus"
runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>

Any insight would be greatly appreciated.

rsw8n
 
R

rsw8n

Michael,

Thanks for your reply however it does not address my issue. I did
change my data access to methods similar to yours just incase the
Application block I am using was causing some unknown issue but I
achieved the same results.

Let me explain my issue again. I can populate the dropdown box in a
edittemplate correctly. I can even have the dropdown preselect the
correct value returned from my database. However, I cannot capture
the selected value when I edit the column. It always updated the
column to the first item in the list regardless of what was originally
returned from the database and what I select when it is in edit mode.
Steps I have taken are:

1. To add an event handler in the itemcreated event of the data grid
as follows:
DropDownList dl = new DropDownList();

if (e.Item.ItemType == ListItemType.EditItem)
{
dl = (DropDownList)e.Item.FindControl("ddStatus");
dl.SelectedIndexChanged += new System.EventHandler(this.Changed);
}

2. I have placed breaks on the dropdownlist databind method in the
itemdatabound event and it does not break during the update. I was
suspecting the dropdown was being redatabound (new word) before the
update but it does not appear to be.

3. In the update event I made a for loop that will loop through each
item in the dropdownlist and set break points in the for loop to read
each individual value in the dropdown list. Neither of them ever has
a selected value set to true. I hoped step one above would allow me
to capture the value but the selectedindexchanged event never fires.
(I have breaks in there that are not within any conditions.).

I have my edit and update event functions set to rebind the datagrid
in the last step of the event. I also bind the datagrid if it is not
a postback.

I have a dropdown list in the edititem template of a data grid. I
have no problems populating the dropdown box when I click edit.
However, if I edit and select a different item in the dropdown list I
always get the top value in the list returned in my UpdateCommand
event. Below is my code:

private void BindCurrentActivityGrid(string strVendorid)
{
DataSet ds;

SqlParameter [] param = new SqlParameter[1];
SqlParameter spparm = new SqlParameter();

spparm.ParameterName = "@vendorid";
spparm.Value = strVendorid;
spparm.SqlDbType = SqlDbType.Char;

param[0] = spparm;

ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspCurrentActivities_GetByVendorId",
param);

DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dgCurrentActivities.DataSource = dv;
dgCurrentActivities.DataBind();
}

private void UpdateCurrentActivityGrid(string strValue,string
iStatus)
{
SqlParameter [] param = new SqlParameter[2];
SqlParameter spparm = new SqlParameter();

spparm.ParameterName = "@ponumber";
spparm.Value = strValue;
spparm.SqlDbType = SqlDbType.VarChar;
param[0] = spparm;

SqlParameter spparm1 = new SqlParameter();
spparm1.Value = Convert.ToInt32(iStatus);
spparm1.ParameterName = "@status";
spparm1.SqlDbType = SqlDbType.SmallInt;
param[1] = spparm1;

SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspPOStatus_Update",param);

}
#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.dgCurrentActivities.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnCancel);
this.dgCurrentActivities.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnEdit);
this.dgCurrentActivities.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdate);
this.dgCurrentActivities.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgItemDataBound);
this.dgCurrentActivities.SelectedIndexChanged += new
System.EventHandler(this.Changed);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void GetStatusCodes()
{

SqlParameter [] param = new SqlParameter[1];
SqlParameter spparm = new SqlParameter();

dsStatusCodes = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["connStr"],
CommandType.StoredProcedure,
"uspStatusCode_GetAll");

}

private void OnEdit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dl = new DropDownList();
ListItemType lit = new ListItemType();

lit = e.Item.ItemType;

GetStatusCodes();
// dl = (DropDownList)(e.Item.Cells[3].FindControl("ddStatus"));
// dl.DataSource = dsStatusCodes;
// dl.DataTextField = "POStatusCdDescription";
// dl.DataValueField = "POStatusId";


dgCurrentActivities.EditItemIndex = e.Item.ItemIndex;
BindCurrentActivityGrid(strVendor);
}

private void OnUpdate(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dl = new DropDownList();
string strStatus;
string strPO;
ListItemType lit = new ListItemType();

lit = e.Item.ItemType;

if (lit == ListItemType.EditItem)
{
strPO = dgCurrentActivities.DataKeys[e.Item.ItemIndex].ToString();
dl = (DropDownList)(e.Item.FindControl("ddStatus"));
strStatus = dl.SelectedItem.Text.ToString();
}

dgCurrentActivities.EditItemIndex = -1;
BindCurrentActivityGrid(strVendor);

}

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

private void dgItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{ DropDownList dl = new DropDownList();
ListItemType lit = new ListItemType();
lit = e.Item.ItemType;

if (lit == ListItemType.EditItem)
{
dl = (DropDownList)(e.Item.FindControl("ddStatus"));
dl.DataTextField = "POStatusCdDescription";
dl.DataValueField = "POStatusId";
dl.DataSource = dsStatusCodes;
dl.DataBind();
}
}

private void Changed(object sender, System.EventArgs e)
{
string strtest;

strtest = e.ToString();
}

The above Changed function is supposed to be called when the
SelectedIndexChanged event is fired in the datagrid.
Below is my asp.net stuff:

<asp:DataGrid id="dgCurrentActivities" runat="server"
AutoGenerateColumns="False" DataKeyField="ponumber">
<Columns>
<asp:HyperLinkColumn Target="_self"
DataNavigateUrlField="ponumber"
DataNavigateUrlFormatString="PODetail.aspx?{0}"
DataTextField="ponumber" HeaderText="PO
#"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="lotnumber" HeaderText="Lot #"
ReadOnly =true></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="Description" ReadOnly =true></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label id=lblStatusCodes runat="server" Width="65px"
Text='<%#DataBinder.Eval(Container.DataItem, "status") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddStatus"
runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>

Any insight would be greatly appreciated.

rsw8n
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top