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:
<aspataGrid 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>
<aspropDownList id="ddStatus"
runat="server"></aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</aspataGrid>
Any insight would be greatly appreciated.
rsw8n
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:
<aspataGrid 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>
<aspropDownList id="ddStatus"
runat="server"></aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</aspataGrid>
Any insight would be greatly appreciated.
rsw8n