By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

DataGrid Runtime DropDown - ViewState Issue

P: n/a

I am having some issues with a runtime dropdownlist in a datagrid. The
issue is that I cannot get ViewState to fill the selected index of a runtime
dropdown properly on postback. I do not want to use template columns as they
seem to be a little difficult to create at runtime. Any assistance would be
very greatly appreciated.

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// display all LooId values with related value in LookupValue for display
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// set the text of the cell based on the dataset
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
int lovId = Convert.ToInt32(dr[lp.ColumnName]);
e.Item.Cells[lp.ColumnIndex].Text = GetLookupValue(lovId);
}
}
else if (e.Item.ItemType == ListItemType.EditItem)
{
// change all LooId columns from textbox to dropdownlist for edit
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// create and populate dropdownlist
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
string controlName = "ddl_" + dr[dr.Table.PrimaryKey[0]] + "_" +
lp.ColumnIndex;
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
// select current value based on dataset
ddl.SelectedValue = dr[lp.ColumnName].ToString();
// replace default textbox control with new dropdownlist
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
ViewState[controlName] = e.Item.Cells[lp.ColumnIndex].Controls[0].UniqueID;
}
}
}
private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Header) ||
(e.Item.ItemType == ListItemType.Footer) ||
(e.Item.ItemType == ListItemType.Separator))
return;
if (e.CommandName != "Update")
return;
// update dataset for all LooId columns with dropdownlist selectedvalue
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// compare dropdownlist selectedvalue to dataset, update if changed
int verId = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
DataRow dr = PageDataSet.Version.FindByVerId(verId);
string controlName = "ddl_" + verId + "_" + lp.ColumnIndex;
string uniqueName2 = ViewState[controlName].ToString();
// error: selecteditems are not correct from viewstate
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
DropDownList ctl1 = (DropDownList)e.Item.Cells[lp.ColumnIndex].Controls[0];
DropDownList ctl2 = (DropDownList)e.Item.FindControl(controlName);
DropDownList ctl3 = (DropDownList)this.FindControl(uniqueName2);
if (ddl.SelectedItem == null)
{
if (dr[lp.ColumnName].GetType() != typeof(DBNull))
dr[lp.ColumnName] = DBNull.Value;
else
continue;
}
else
{
if (dr[lp.ColumnName].GetType() == typeof(DBNull))
dr[lp.ColumnName] = ddl.SelectedValue;
else if (dr[lp.ColumnName].ToString() != ddl.SelectedValue)
dr[lp.ColumnName] = ddl.SelectedValue;
else
continue;
}
}
}

Steve Pierce
Sterling Computer Consultants
st****@sterlingmi.com
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Steve,

I think you might misunderstand something. In a datagrid, a datagrid item
becomes an so-called EditItem only when you assign it as EditItem, e.g. in
Edit event:

DataGrid.EditItemIndex = e.Item.ItemIndex
Datagrid.DataSource = data_source_object
Datagrid.DataBind()

Then the item, index as e.Item.ItemIndex becomes EditItem.

In ItemDataBound event, there are only two kinds of data-bound items,
ListItemType.Item and ListItemType.AlternatingItem.
HTH

Elton Wang
el********@hotmail.com
"Steve Pierce" wrote:

I am having some issues with a runtime dropdownlist in a datagrid. The
issue is that I cannot get ViewState to fill the selected index of a runtime
dropdown properly on postback. I do not want to use template columns as they
seem to be a little difficult to create at runtime. Any assistance would be
very greatly appreciated.

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// display all LooId values with related value in LookupValue for display
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// set the text of the cell based on the dataset
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
int lovId = Convert.ToInt32(dr[lp.ColumnName]);
e.Item.Cells[lp.ColumnIndex].Text = GetLookupValue(lovId);
}
}
else if (e.Item.ItemType == ListItemType.EditItem)
{
// change all LooId columns from textbox to dropdownlist for edit
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// create and populate dropdownlist
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
string controlName = "ddl_" + dr[dr.Table.PrimaryKey[0]] + "_" +
lp.ColumnIndex;
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
// select current value based on dataset
ddl.SelectedValue = dr[lp.ColumnName].ToString();
// replace default textbox control with new dropdownlist
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
ViewState[controlName] = e.Item.Cells[lp.ColumnIndex].Controls[0].UniqueID;
}
}
}
private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Header) ||
(e.Item.ItemType == ListItemType.Footer) ||
(e.Item.ItemType == ListItemType.Separator))
return;
if (e.CommandName != "Update")
return;
// update dataset for all LooId columns with dropdownlist selectedvalue
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// compare dropdownlist selectedvalue to dataset, update if changed
int verId = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
DataRow dr = PageDataSet.Version.FindByVerId(verId);
string controlName = "ddl_" + verId + "_" + lp.ColumnIndex;
string uniqueName2 = ViewState[controlName].ToString();
// error: selecteditems are not correct from viewstate
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
DropDownList ctl1 = (DropDownList)e.Item.Cells[lp.ColumnIndex].Controls[0];
DropDownList ctl2 = (DropDownList)e.Item.FindControl(controlName);
DropDownList ctl3 = (DropDownList)this.FindControl(uniqueName2);
if (ddl.SelectedItem == null)
{
if (dr[lp.ColumnName].GetType() != typeof(DBNull))
dr[lp.ColumnName] = DBNull.Value;
else
continue;
}
else
{
if (dr[lp.ColumnName].GetType() == typeof(DBNull))
dr[lp.ColumnName] = ddl.SelectedValue;
else if (dr[lp.ColumnName].ToString() != ddl.SelectedValue)
dr[lp.ColumnName] = ddl.SelectedValue;
else
continue;
}
}
}

Steve Pierce
Sterling Computer Consultants
st****@sterlingmi.com

Nov 19 '05 #2

P: n/a
Daniel Roth responded to my message, the selected value in viewstate can be
pulled with the addition of the following line:
ddl.SelectedValue = Request.Form.Get((string)ViewState[controlName]);

below is the corrected code called from ItemCommand e.CommandName ==
"Update":

foreach (LookupProperties lp in GetLookupCollection().Items)
{
int verId = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
DataRow dr = PageDataSet.Version.FindByVerId(verId);
string controlName = "ddl_" + verId + "_" + lp.ColumnIndex;
// create and populate dropdownlist
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
else if (lov.LovLolId == lp.LolId && lov.LovId < 0)
ddl.Items.Add(new ListItem("", lov.LovId.ToString()));
// replace default textbox control with new dropdownlist
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
// select current value based on viewstate
ddl.SelectedValue = Request.Form.Get((string)ViewState[controlName]);
// compare dropdownlist selectedvalue to dataset, update if changed
if (Convert.ToInt32(ddl.SelectedValue) < 0)
{
if (dr[lp.ColumnName].GetType() != typeof(DBNull))
dr[lp.ColumnName] = DBNull.Value;
}
else
{
if (dr[lp.ColumnName].GetType() == typeof(DBNull))
dr[lp.ColumnName] = ddl.SelectedValue;
else if (dr[lp.ColumnName].ToString() != ddl.SelectedValue)
dr[lp.ColumnName] = ddl.SelectedValue;
}
}

"Steve Pierce" <st****@sterlingmi.com> wrote in message
news:Ow*************@TK2MSFTNGP15.phx.gbl...

I am having some issues with a runtime dropdownlist in a datagrid. The
issue is that I cannot get ViewState to fill the selected index of a runtime dropdown properly on postback. I do not want to use template columns as they seem to be a little difficult to create at runtime. Any assistance would be very greatly appreciated.

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// display all LooId values with related value in LookupValue for display
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// set the text of the cell based on the dataset
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
int lovId = Convert.ToInt32(dr[lp.ColumnName]);
e.Item.Cells[lp.ColumnIndex].Text = GetLookupValue(lovId);
}
}
else if (e.Item.ItemType == ListItemType.EditItem)
{
// change all LooId columns from textbox to dropdownlist for edit
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// create and populate dropdownlist
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
string controlName = "ddl_" + dr[dr.Table.PrimaryKey[0]] + "_" +
lp.ColumnIndex;
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
// select current value based on dataset
ddl.SelectedValue = dr[lp.ColumnName].ToString();
// replace default textbox control with new dropdownlist
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
ViewState[controlName] = e.Item.Cells[lp.ColumnIndex].Controls[0].UniqueID; }
}
}
private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Header) ||
(e.Item.ItemType == ListItemType.Footer) ||
(e.Item.ItemType == ListItemType.Separator))
return;
if (e.CommandName != "Update")
return;
// update dataset for all LooId columns with dropdownlist selectedvalue
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// compare dropdownlist selectedvalue to dataset, update if changed
int verId = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
DataRow dr = PageDataSet.Version.FindByVerId(verId);
string controlName = "ddl_" + verId + "_" + lp.ColumnIndex;
string uniqueName2 = ViewState[controlName].ToString();
// error: selecteditems are not correct from viewstate
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
DropDownList ctl1 = (DropDownList)e.Item.Cells[lp.ColumnIndex].Controls[0]; DropDownList ctl2 = (DropDownList)e.Item.FindControl(controlName);
DropDownList ctl3 = (DropDownList)this.FindControl(uniqueName2);
if (ddl.SelectedItem == null)
{
if (dr[lp.ColumnName].GetType() != typeof(DBNull))
dr[lp.ColumnName] = DBNull.Value;
else
continue;
}
else
{
if (dr[lp.ColumnName].GetType() == typeof(DBNull))
dr[lp.ColumnName] = ddl.SelectedValue;
else if (dr[lp.ColumnName].ToString() != ddl.SelectedValue)
dr[lp.ColumnName] = ddl.SelectedValue;
else
continue;
}
}
}

Steve Pierce
Sterling Computer Consultants
st****@sterlingmi.com


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.